ssm框架搭建(三)-controller层-springMVC

mvc层,控制视图层。主要负责与页面的交互。
最后的文件结构图
ssm框架搭建(三)-controller层-springMVC_第1张图片
首先是Controller层:UserController.java

package com.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.entity.User;
import com.ssm.service.UserService;

/**
 * @author zx
 * @Date 2017年10月11日 上午9:25:39
 * @Class UserController.java
 */

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/userName", method = RequestMethod.GET)
    public String list(Model model) {
        return "userName";
    }

    @RequestMapping(value = "/{userId}/query", method = RequestMethod.GET, produces = {
            "application/json;charset=utf-8" })
    @ResponseBody
    public User queryUser(@PathVariable("userId") int userId) {
        User user = userService.queryUserByUserId(userId);
        return user;
    }
}

springMVC默认只配置对jsp的视图解析配置,这里是一个html解析器,并继承 InternalResourceView 后重写 checkResource,这是我在网上找的,原文在http://blog.csdn.net/cai_chinasoft/article/details/51611596,有兴趣可以去看看。HtmlResourceView.java

package com.ssm.utils;

import java.io.File;
import java.util.Locale;

import org.springframework.web.servlet.view.InternalResourceView;

/**  
* @author zx 
* @Date 2017年10月11日 上午10:57:33  
* @Class 
* 重写 解析器
*/
public class HtmlResourceView extends InternalResourceView{

    @Override
    public boolean checkResource(Locale locale) throws Exception {
        // TODO Auto-generated method stub
        String url = this.getServletContext().getRealPath("/") + getUrl();
         File file = new File(url);  
         return file.exists();// 判断该页面是否存在  
    }
}

spring配置:spring-mvc.xml(这里我只配置了html的,jsp的网上找找或者刚才那个网址里面有)


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    
    <mvc:annotation-driven />

    
    
    <mvc:default-servlet-handler />

    
    <bean id="htmlviewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="com.ssm.utils.HtmlResourceView" />
        <property name="order" value="0" />
        <property name="prefix" value="/" />
        <property name="suffix" value=".html" />
        <property name="contentType" value="text/html;charset=UTF-8">property>
    bean>

    
    <context:component-scan base-package="com.ssm.controller" />
beans>

最后就是界面:userName.html(使用了bootstrap,jquery)


<html>

    <head>
        <meta charset="UTF-8">
        <title>title>
        <link href="https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet">
    head>

    <body>
        <div class="container">
            <div class="row clearfix">
                <div class="col-md-12 column">
                        <div class="form-group">
                            <label>userIdlabel><input type="text" class="form-control" id="userId" />
                        div>
                        <div class="checkbox">
                        div> <button type="button" class="btn btn-primary" onclick="submit()">Submitbutton>
                div>
            div>
        div>
        <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js">script>
        <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.js">script>
        <script type="text/javascript" src="https://cdn.bootcss.com/bootstrap/4.0.0-beta/js/bootstrap.min.js">script>
        <script type="text/javascript">
            function submit(){
                var userId = $("#userId").val();
                if(userId == ''){
                    alert("userId不能为空")
                }else{
                    $.get("./"+userId+"/query",function(result){
                    alert("用户名为:"+result.userName);
                })
                }
            }
        script>
    body>

html>

我访问的url为:http://localhost:8080/SSM_TEST/user/userName
源码下载地址:源码

你可能感兴趣的:(ssm框架)