Spring:基于注解的Spring MVC

什么是Spring MVC

Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据、业务与展现进行分离。从这样一个角度来说,Spring MVC和Structs、Structs2非常类似。Spring MVC的设计是围绕DispatcherServlet展开的,DispatcherServlet负责将请求派发到特定的handler。通过可配置的hander mappings、view resolution、locale以及theme resolution来处理请求并且转到对应的视图。Spring MVC请求处理的整体流程如图:

Spring MVC有基于注解版与基础.xml版的两种用法,不过现在的企业级开发基本都使用的是注解版,没别的原因,就是方便而已。因此后面的代码示例,都是基于注解版本的,想了解基于.xml版本的Spring MVC的朋友可以自行上网查询。

Spring MVC环境搭建

要开始本文后面的内容,自然要搭建一个Spring MVC的环境,那么首先建立一个Java Web的工程,我建立的工程名字叫做SpringMVC,要搭建一个基础功能的Spring MVC环境,必须引入的jar包是beans、context、core、expression、web、webmvc以及commons-logging。

然后,对web.xml添加一些内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
    <display-name></display-name>   
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
 
      <!-- 该监听器将在Web容器启动时激活Spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
    <!-- 处理由JavaBeans,Introspector的使用而引起的缓冲泄露,建议配置此监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
 
      <!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:springmvc-servlet.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

两个listener不是必须的,但是servlet是必须的,url-pattern用于开发者选择哪些路径是需要让Spring MVC来处理的。接着在classpath下按照我们约定的名字springmvc-servlet.xml写一个xml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 
 
http://www.springframework.org/schema/mvc
 
 
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
 
 
http://www.springframework.org/schema/context
 
 
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
 
    <context:annotation-config />   
    <context:component-scan base-package="com.xrq.controller"/>
 
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        <!-- WebRoot到一指定文件夹文件路径 --> 
        <property name="prefix" value="/" /> 
        <!-- 视图名称后缀  --> 
        <property name="suffix" value=".jsp" /> 
    </bean
</beans>

另外,由于使用了Spring,所以Tomcat启动的时候默认会去WEB-INF下找applicationContext.xml,所以放一个空的applicationContext.xml到WEB-INF下:

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
 
</beans>

写一个Java POJO,用于处理具体的请求:

1
2
3
4
5
6
7
8
9
10
11
@Controller
@RequestMapping(value = "/test")
public class TestController
{
    @RequestMapping
    public String dispatchTest()
    {
        System.out.println("Enter TestController.dispatchTest");
        return "test";
    }
}

注意,这里有一个@Controller,这个注解和@Service注解意思差不多,都表示该类是一个Spring Bean,这样就不需要再在Spring文件中为这个类定义Bean了。

另外,由于我前面在springmvc-servlet.xml中配置了prefix和suffix,因此return的时候就可以方便一些,不需要写前缀和后缀,Spring MVC默认会转发到(请求转发是Spring MVC默认的页面跳转方式)”/test.jsp”路径下。

最后别忘了,因为在web.xml中设置了启动激活Spring,因此还需要写一个applicationContext.xml(Spring文件的默认名字),当然,里面除了基本的声明,什么实际内容都不需要。最终,WebRoot文件夹应该是这么一个结构:

最后,启动容器,访问”localhost:8080/SpringMVC/test”,容器就会把该请求转发到”localhost:8080/SpringMVC/test.jsp”页面下了。

@RequestMapping注解

Spring MVC中最重要的注解应该就是@RequestMapping了,它是用于处理请求映射的。继续看刚才的TestController:

1
2
3
4
5
6
7
8
9
10
11
@Controller
@RequestMapping(value = "/test")
public class TestController
{
    @RequestMapping
    public String dispatchTest()
    {
        System.out.println("Enter TestController.dispatchTest()");
        return "test";
    }
}

类上的RequestMapping是用于第一层匹配的。”localhost:8080/SpringMVC/test”和”localhost:8080/SpringMVC/add”,value是test,自然走的是前者。

接着看,比如我在TestController中又定义了三个方法,此时类上不使用RequestMapping注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@RequestMapping(value = "/add")
public String dispatchAddTest()
{
    System.out.println("Enter TestControll.dispatchAddTest()");
    return "test";
}
 
@RequestMapping(value = "/add/add")
public String dispatchAddAddTest()
{
    System.out.println("Enter TestControll.dispatchAddAddTest()");
    return "test";
}
 
@RequestMapping(value = "/del")
public String dispatchDelTest()
{
    System.out.println("Enter TestControll.dispatchDelTest()");
    return "test";
}

那么这三个方法分别匹配的路径是:

1
2
3
"localhost:8080/SpringMVC/add"
"localhost:8080/SpringMVC/add/add"
"localhost:8080/SpringMVC/del"

关于路径匹配,再提一点,假如在类上和方法上都加了RequestMapping,那么将会以类路径为基准,再向方法路径做匹配,比如:

1
2
3
4
5
6
7
8
9
10
11
@Controller
@RequestMapping(value = "/test/")
public class TestController
{
    @RequestMapping(value = "common")
    public String root()
    {
        System.out.println("Enter TestController.root()!");
        return "result";
    }
}

这种写法,匹配的应当是:

1
2
3
4
5
"localhost:8080/SpringMVC/test/common"
"localhost:8080/SpringMVC/test/common/"
"localhost:8080/SpringMVC/test/common.html"
"localhost:8080/SpringMVC/test/common.jsp"
"localhost:8080/SpringMVC/test/common.vm"

类似这种的路径,如果还想往”localhost:8080/SpringMVC/test/common/”再添加内容,那么root()这个方法就无法匹配到了,必须再添加方法。多说一句,”/”一直是一个容易弄混的东西,我自己试验的时候发现,RequestMapping里面的value属性,只要路径不存在多级的关系,加不加”/”是没有什么影响的

另外,@RequestMapping还可以匹配请求类型,到底是GET还是POST还是其他的,这么做:

1
2
3
4
5
6
@RequestMapping(method = RequestMethod.POST)
public String dispatchTest()
{
    System.out.println("Enter TestController.dispatchTest()");
    return "test";
}

这样就指定了该方法只匹配”localhost:8080/SpringMVC/test”且请求方式为POST的请求。

前面页面跳转的方式都是转发(dispatch)的方式,转发在我看来未必是一种很好的方式,典型的就是处理表单的时候会有表单重复提交的问题,那么如何使用重定向(redirect)的方式进行页面跳转?可以这么写Controller的方法,差别在于return部分:

1
2
3
4
5
6
@RequestMapping
public String dispatchTest(Test test)
{
    System.out.println("Enter TestController.dispatchTest(), test: " + test);
    return "redirect:/test.jsp";
}

最后,@RequestMapping中还有params、headers、consumes等几个属性,不过这几个都不重要,也不常用,就不讲了。

参数匹配

处理url也好、表单提交也好,参数匹配是非常重要的一个内容,万幸,Spring MVC对参数请求的支持做得非常好—-它会自动根据url或者表单中参数的名字和方法中同名形参进行匹配并赋值

举一个例子:

1
2
3
4
5
6
7
8
9
10
11
12
@Controller
@RequestMapping(value = "/test")
public class TestController
{
    @RequestMapping
    public String dispatchTest(String testId, String ttestId)
    {
        System.out.println("Enter TestController.dispatchTest(), testId = " + testId +
                ", ttestId = " + ttestId);
        return "test";
    }
}

此时,我访问”localhost:8080/SpringMVC/test?testId=1&ttestId=2″,控制台打印出:

1
Enter TestController.dispatchTest(), testId = 2, ttestId = 3

不仅如此,方法中还可以放入一个实体类对象:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Test
{
    private String tid;
    private String nid;
    private String bid;
 
    public void setTid(String tid)
    {
        this.tid = tid;
    }
 
    public void setNid(String nid)
    {
        this.nid = nid;
    }
 
    public void setBid(String bid)
    {
        this.bid = bid;
    }
 
    public String toString()
    {
        return "tid = " + tid + ", nid = " + nid + ", bid = " + bid;
    }
}

注意,实体类对象中如果私有属性不打算对外提供,getter可以没有,但是为了Spring MVC可以将对应的属性根据属性名称进行匹配并赋值,setter必须要有。把TestController稍作改造,传入一个对象:

1
2
3
4
5
6
@RequestMapping
public String dispatchTest(Test test)
{
    System.out.println("Enter TestController.dispatchTest(), test: " + test);
    return "test";
}

此时我访问”http://localhost:8080/SpringMVC/test?tid=0&bid=1&nid=2″,控制台上打印出:

1
Enter TestController.dispatchTest(), test: tid = 0, nid = 2, bid = 1

看到,参数完全匹配。

不仅如此,再多试验一点:

1
2
3
4
5
6
7
@RequestMapping
public String dispatchTest(Test test1, Test test2, String tid, String nid)
{
    System.out.println("Enter TestController.dispatchTest(), test1:" + test1 +
            "; test2:" + test2 + "; tid:" + tid + "; nid:" + nid);
    return "test";
}

访问一样地址”http://localhost:8080/SpringMVC/test?tid=0&bid=1&nid=2″,结果是:

1
Enter TestController.dispatchTest(), test1:tid = 0, nid = 2, bid = 1; test2:tid = 0, nid = 2, bid = 1; tid:0; nid:2

结论就是:

  • 假如方法的参数是普通的字符串,只要字符串名字有和请求参数中的key完全匹配的,Spring MVC就会将完全匹配的自动赋值
  • 假如方法的参数是实体类,只要实体类中的参数有和请求参数中的key完全匹配的,Spring MVC就会将完全匹配的自动赋值

对于url如此,应用到表单中也是一样的,有兴趣的可以自己试验一下。

31

Model

上一篇文章《Spring:基于注解的Spring MVC(上)》,讲了Spring MVC环境搭建、@RequestMapping以及参数绑定,这是Spring MVC中最基础也是最重要的内容,本篇文章继续讲讲Spring MVC中其余的知识点,先从Model开始。

前一篇文章比较详细地解读了数据从页面请求到服务器后台的一些细节,那么下一个要解决的问题就是数据如何从后台再次传回前台,答案就是这里要说的Model,关于Model在写例子之前我特别先说明三点:

1、Model本身是一个接口,其实现类为ExtendedModelMap,除了使用Model之外还可以使用ModelAndView、ModelMap这些,不过要是没有特殊需求,使用Model比较简单,我个人也比较喜欢使用Model

2、Model的生命周期是Request,也就是说要通过Model传值只能使用转发而不能使用重定向

3、为什么要使用Model而不是用Request,最主要的原因就是减少代码的侵入性或者说代码的耦合度也行。因为Model是Spring的组件,Request是J2EE的组件,使用Model而不去使用Request可以减少对J2EE的依赖,也便于调试

OK,接下来看例子,总体的代码还是按照上一篇文章的来,先看后台的代码:

1
2
3
4
5
6
7
8
9
10
11
@Controller
@RequestMapping(value = "/test")
public class TestController
{
    @RequestMapping
    public String dispatchTest(Test test, Model model)
    {
        model.addAttribute("modelKey", "modelValue");
        return "test";
    }
}

就往Model里面塞一个Key-Value,然后转发到test.jsp下,test.jsp页面要取Model的值,可以通过JSTL(EL表达式也可以)获取,反正直接在jsp页面上通过”<% … %>”写Java脚本是行不通的。test.jsp页面这么写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
      <head>
        <base href="<%=basePath%>">
 
        <title>test页面</title>
 
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">   
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
 
      <body>
          <c:out value="${modelKey}" />
      </body>
</html>

OK,然后访问一下”http://localhost:8080/SpringMVC/test”这个地址,页面上”modelValue”这几个字符就出来了。

之前说过了,Model的生命周期是Request,那么如果页面是重定向到test.jsp上面去,肯定是取不到”modelValue”的,可以自己试一下,因此重定向过去的话,要在后台把数据设置到session中。

test.jsp页面不变,Controller可以这么改:

1
2
3
4
5
6
7
8
9
10
11
12
@Controller
@RequestMapping(value = "/test")
public class TestController
{
    @RequestMapping
    public String dispatchTest(Test test, HttpSession session)
    {
        session.setAttribute("modelKey", "modelValue");
        return "redirect:/test.jsp";
        //return "test";
    }
}

可以试一下,再访问一下”http://localhost:8080/SpringMVC/test”这个地址,”modelValue”这几个字符在页面上就出来了。

在Spring MVC中,Request、Response、Session、InputStream、OutputStream这些对象是自动注入的,但是就像之前说的,为了减少代码的侵入性与耦合度,能不使用尽量还是不使用这些J2EE的对象的好。

拦截器(Interceptor)

SpringMVC中的拦截器相当于J2EE中的过滤器,是非常重要和相当有用的,它的主要作用就是拦截用户的请求并进行相应的处理的,比如通过它来进行权限验证,或者是来判断用户是否登陆。

在SpringMVC中使用拦截器的方法比较简单,首先实现HandlerInterceptor接口,实现afterCompletion、postHandle、preHandle三个抽象方法,这里定义两个Interceptor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class TestInterceptor1 implements HandlerInterceptor
{
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception
    {
        System.out.println("TestInterceptor1.afterCompletion()");
    }
 
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception
    {
        System.out.println("TestInterceptor1.postHandle()");
    }
 
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2) throws Exception
    {
        System.out.println("TestInterceptor1.preHandle()");
        return true;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class TestInterceptor2 implements HandlerInterceptor
{
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception
    {
        System.out.println("TestInterceptor2.afterCompletion()");
    }
 
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception
    {
        System.out.println("TestInterceptor2.postHandle()");
    }
 
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2) throws Exception
    {
        System.out.println("TestInterceptor2.preHandle()");
        return true;
    }
}

说明一下三个方法的作用:

1、afterCompletion:在整个视图渲染完毕之后执行方法里面的内容,主要用于释放一些资源

2、postHandle:在Controller执行之后,视图渲染之前执行方法里面的内容,也就是说postHandle方法可以对Model进行操作

3、preHandle:在Controller执行之前,执行方法里面的内容,注意该方法是有返回值的,当方法返回false时整个请求就结束了

然后在springmvc-servlet.xml里面增加拦截器的配置:

1
2
3
4
5
6
7
8
9
10
11
<!-- 配置拦截器 -->
<mvc:interceptors>
   <mvc:interceptor>
       <mvc:mapping path="/test" />
        <bean class="com.xrq.interceptor.TestInterceptor2" />
    </mvc:interceptor>
    <mvc:interceptor>
        <mvc:mapping path="/test" />
        <bean class="com.xrq.interceptor.TestInterceptor1" />
    </mvc:interceptor>
</mvc:interceptors>

假如有多个拦截器的话,”<mvc:interceptor>…</mvc:interceptor>”定义的顺序就是拦截器执行的顺序。

下面继续访问”http://localhost:8080/SpringMVC/test”,代码执行的结果是:

1
2
3
4
5
6
TestInterceptor2.preHandle()
TestInterceptor1.preHandle()
TestInterceptor1.postHandle()
TestInterceptor2.postHandle()
TestInterceptor1.afterCompletion()
TestInterceptor2.afterCompletion()

也许有些朋友对这个执行结果不是很理解,我其实是懂的,但确实一下子也说不清楚。

如果不是很理解的朋友,可以去看一下Java设计模式里面的责任链模式,拦截器的这种调用方法实际上是一种链式的调用法,TestInterceptor2调用TestInterceptor1,TestInterceptor1方法走了才会回到TestInterceptor2的方法里面。

原文地址:https://www.cnblogs.com/pureEve/p/6546249.html