②springMVC入门

1        

1.1      需求

以案例作为驱动。

springmvc和mybaits使用一个案例(商品订单管理)。

 

功能需求:商品列表查询

 

1.2      环境准备

数据库环境:mysql5.1

 

java环境:

jdk1.7.0_72

myeclipse10

springmvc版本:spring3.2

需要spring3.2所有jar(一定包括spring-webmvc-3.2.0.RELEASE.jar)

 

 

1.3      配置前端控制器

在web.xml中配置前端控制器。

 1  <!-- springMVC前端控制器 -->
 2   <servlet>
 3       <servlet-name>springmvc</servlet-name>
 4       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 5   <!-- contextConfigLocation配置springmvc加载配置文件(配置处理器映射器、适配器等等)
 6       如果不配置contextConfigLocation,默认加载 /WEB-INF/servlet名称-servlet.xml(springmvc-servlet.xml)
 7       -->
 8       <init-param>
 9           <param-name>contextConfigLocation</param-name>
10           <param-value>classpath:springmvc.xml</param-value>
11       </init-param>
12   </servlet>
13   
14   <servlet-mapping>
15       <servlet-name>springmvc</servlet-name>
16       <!-- 
17           第一种:*.action,访问以.action结尾 由DispatchServlet进行解析
18           第二张:/.所有访问的地址都由 DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析
19               使用此种方法可以实现RESTful风格的url
20           第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时,
21               仍然有DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错
22        -->
23       <url-pattern>*.action</url-pattern>
24   </servlet-mapping>

spirngMVC.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
15 
16 
17     <!-- 处理器适配器
18         所有处理器适配器都实现了HandlerAdapter接口
19      -->
20     <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
21     
22     <!-- 配置Handler -->
23     <bean id="itemsController1" name="/queryItems.action" class="cn.itcast.ssm.controller.ItemsController1"/>
24 
25     <!-- 配置处理器映射器
26         将bean的name作为url进行查找,需要在配置Handler时,指定beanname(就是url)
27      -->
28      <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> -->
29      
30      <!-- 简单的url映射 -->
31      <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
32          <property name="mappings">
33              <props>
34                  <prop key="/queryItems1.action">itemsController1</prop>
35                  <prop key="/queryItems2.action">itemsController1</prop>
36              </props>
37          </property>
38      </bean>
39      
40      
41      
42      <!-- 视图解析器
43      解析jsp,默认使用jstl标签,classpath下得有jstl的包 
44      -->
45      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
46 </beans>

 

1.4      配置处理器适配器

在classpath下的springmvc.xml中配置处理器适配器

1 <!-- 处理器适配器
2         所有处理器适配器都实现了HandlerAdapter接口
3      -->
4     <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

通过查看原代码:

1 public class SimpleControllerHandlerAdapter implements HandlerAdapter {
2 
3     public boolean supports(Object handler) {
4         return (handler instanceof Controller);
5     }

此适配器能执行实现 Controller接口的Handler。

 1 public interface Controller {
 2 
 3     /**
 4      * Process the request and return a ModelAndView object which the DispatcherServlet
 5      * will render. A {@code null} return value is not an error: It indicates that
 6      * this object completed request processing itself, thus there is no ModelAndView
 7      * to render.
 8      * @param request current HTTP request
 9      * @param response current HTTP response
10      * @return a ModelAndView to render, or {@code null} if handled directly
11      * @throws Exception in case of errors
12      */
13     ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;
14 
15 }

 

1.5      开发Handler

需要实现 controller接口,才能由org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter适配器执行。

 1 public class ItemsController1 implements Controller {
 2 
 3     @Override
 4     public ModelAndView handleRequest(HttpServletRequest request,
 5             HttpServletResponse response) throws Exception {
 6 
 7         // 调用service查找 数据库,查询商品列表,这里使用静态数据模拟
 8         List<Items> itemsList = new ArrayList<Items>();
 9         // 向list中填充静态数据
10 
11         Items items_1 = new Items();
12         items_1.setName("联想笔记本");
13         items_1.setPrice(6000f);
14         items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
15 
16         Items items_2 = new Items();
17         items_2.setName("苹果手机");
18         items_2.setPrice(5000f);
19         items_2.setDetail("iphone6苹果手机!");
20 
21         itemsList.add(items_1);
22         itemsList.add(items_2);
23 
24         //返回ModelAndView
25         ModelAndView modelAndView = new ModelAndView();
26         //相当 于request的setAttribut,在jsp页面中通过itemsList取数据
27         modelAndView.addObject("itemlist", itemsList);
28         
29         //指定视图
30         modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
31 
32         return modelAndView;
33     }
34 
35 }

1.6      视图编写

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>商品查询列表</title>
10 </head>
11 <body>
12     <form action="${pageContext.request.contextPath}/item/queryItem.action" method="post">
13         查询条件:
14         <table width="100%" border=1>
15             <tr>
16                 <td><input type="submit" value="查询" /></td>
17             </tr>
18         </table>
19         商品列表:
20         <table width="100%" border="1">
21             <tr>
22                 <td>商品名称</td>
23                 <td>商品价格</td>
24                 <td>生产日期</td>
25                 <td>商品描述</td>
26                 <td>操作</td>
27             </tr>
28         
29             <c:forEach items="${itemlist}" var="item">
30                 <tr>
31                     <td>${item.name}</td>
32                     <td>${item.price }</td>
33                     <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
34                     <td>${item.detail }</td>
35                     <td><a href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td>
36                 </tr>
37             </c:forEach>
38         </table>
39     </form>
40 </body>
41 </html>

1.7      配置Handler

将编写Handler在spring容器加载。

1 <!-- 配置Handler -->
2    <bean id="itemsController1" name="/queryItems.action" class="cn.itcast.ssm.controller.ItemsController1"/>

1.8      配置处理器映射器

在classpath下的springmvc.xml中配置处理器映射器

 1     <!-- 配置处理器映射器
 2         将bean的name作为url进行查找,需要在配置Handler时,指定beanname(就是url)
 3      -->
 4      <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> -->
 5      
 6      <!-- 简单的url映射 -->
 7      <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
 8          <property name="mappings">
 9              <props>
10                  <prop key="/queryItems1.action">itemsController1</prop>
11                  <prop key="/queryItems2.action">itemsController1</prop>
12              </props>
13          </property>
14      </bean>

1.9      配置视图解析器

需要配置解析jsp的视图解析器。

1 <!-- 视图解析器
2      解析jsp,默认使用jstl标签,classpath下得有jstl的包 
3      -->
4      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

1.10部署调试

 

访问地址:http://localhost:8080/springmvcfirst1208/queryItems.action

 

处理器映射器根据url找不到Handler,报下边的错误。说明url错误。

 

处理器映射器根据url找到了Handler,转发的jsp页面找到,报下边的错误,说明jsp页面地址错误了。

 

2       非注解的处理器映射器和适配器

2.1      非注解的处理器映射器

 

处理器映射器:

org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

另一个映射器:

org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

 

多个映射器可以并存,前端控制器判断url能让哪些映射器映射,就让正确的映射器处理。

 

 

2.2      非注解的处理器适配器

 

org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter

要求编写的Handler实现 Controller接口。

 

org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter

要求编写的Handler实现 HttpRequestHandler接口。

 

      //使用此方法可以通过修改response,设置响应的数据格式,比如响应json数据

 

/*

      response.setCharacterEncoding("utf-8");

      response.setContentType("application/json;charset=utf-8");

      response.getWriter().write("json串");*/

 

3       DispatcherSerlvet.properties

前端控制器从上边的文件中加载处理映射器、适配器、视图解析器等组件,如果不在springmvc.xml中配置,使用默认加载的。

 

4       注解的处理器映射器和适配器

注解spirngmvc.xml完全配置

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
15 
16     <!-- 注解配置 处理器适配器-->
17     <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
18     <!-- 注解配置 处理器映射器-->
19     <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>  -->
20      
21      <!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
22     mvc:annotation-driven默认加载很多的参数绑定方法,
23     比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
24     实际开发时使用mvc:annotation-driven
25      -->
26      
27     <mvc:annotation-driven></mvc:annotation-driven>
28      
29      
30     <!-- 对于注解的Handler可以单个配置
31     实际开发中建议使用组件扫描
32      -->
33     <!-- <bean class="cn.itcast.ssm.controller.ItemsController3" /> -->
34     <!-- 可以扫描controller、service、...
35     这里让扫描controller,指定controller的包
36      -->
37     <context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>
38 
39      
40      
41      <!-- 视图解析器
42      解析jsp,默认使用jstl标签,classpath下得有jstl的包 
43      
44      视图解析器配置前缀和后缀:
45      -->
46      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
47         <property name="prefix" value="/WEB-INF/jsp"></property>
48         <property name="suffix" value=".jsp"></property>
49      </bean>
50 </beans>
View Code

 

在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映射器。

 

在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器。

 

在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器。

 

在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器。

 

4.1      配置注解映射器和适配器。

 

<!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置

   mvc:annotation-driven默认加载很多的参数绑定方法,

   比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter

   实际开发时使用mvc:annotation-driven

    -->

   <!-- <mvc:annotation-driven></mvc:annotation-driven> -->

 

4.2      开发注解Handler

 

使用注解的映射器和注解的适配器。(注解的映射器和注解的适配器必须配对使用)

 

//使用Controller标识 它是一个控制器

 1 @org.springframework.stereotype.Controller
 2 public class ItemsController3 {
 3 
 4     @RequestMapping("/queryItems")
 5     public ModelAndView handleRequest() throws Exception {
 6 
 7         // 调用service查找 数据库,查询商品列表,这里使用静态数据模拟
 8         List<Items> itemsList = new ArrayList<Items>();
 9         // 向list中填充静态数据
10 
11         Items items_1 = new Items();
12         items_1.setName("联想笔记本");
13         items_1.setPrice(6000f);
14         items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
15 
16         Items items_2 = new Items();
17         items_2.setName("苹果手机");
18         items_2.setPrice(5000f);
19         items_2.setDetail("iphone6苹果手机!");
20 
21         itemsList.add(items_1);
22         itemsList.add(items_2);
23 
24         //返回ModelAndView
25         ModelAndView modelAndView = new ModelAndView();
26         //相当 于request的setAttribut,在jsp页面中通过itemsList取数据
27         modelAndView.addObject("itemlist", itemsList);
28         
29         //指定视图
30          //下边的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为
31         //modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
32         //上边的路径配置可以不在程序中指定jsp的前缀和jsp路径的后缀
33         
34         
35         modelAndView.setViewName("/items/itemsList");
36 
37         return modelAndView;
38     }
39 
40 }

 

 

4.3      在spring容器中加载Handler

 1    <!-- 对于注解的Handler可以单个配置
 2 
 3    实际开发中建议使用组件扫描
 4 
 5     -->
 6 
 7    <!-- <bean class="cn.itcast.ssm.controller.ItemsController3" /> -->
 8 
 9    <!-- 可以扫描controller、service、...
10 
11    这里让扫描controller,指定controller的包
12 
13     -->
14 
15    <context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>

 

4.4      部署调试

访问:http://localhost:8080/springmvcfirst1208/queryItems.action

 

5       源码分析(重点)

通过前端控制器源码分析springmvc的执行过程。

 

第一步:前端控制器接收请求

调用doDiapatch

 

 

第二步:前端控制器调用处理器映射器查找 Handler

 

 

第三步:调用处理器适配器执行Handler,得到执行结果ModelAndView

 

第四步:视图渲染,将model数据填充到request域。

视图解析,得到view:

 

调用view的渲染方法,将model数据填充到request域

 

渲染方法:

 

 

6       入门程序小结

 

通过入门程序理解springmvc前端控制器、处理器映射器、处理器适配器、视图解析器用法。

 

前端控制器配置:

第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析

 

第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析

  使用此种方式可以实现 RESTful风格的url

 

处理器映射器:

非注解处理器映射器(了解)

注解的处理器映射器(掌握)

         对标记@Controller类中标识有@RequestMapping的方法进行映射。在@RequestMapping里边定义映射的url。使用注解的映射器不用在xml中配置url和Handler的映射关系。

 

处理器适配器:

非注解处理器适配器(了解)

注解的处理器适配器(掌握)

         注解处理器适配器和注解的处理器映射器是配对使用。理解为不能使用非注解映射器进行映射。

 

<mvc:annotation-driven></mvc:annotation-driven>可以代替下边的配置:

 

   <!--注解映射器 -->

   <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

   <!--注解适配器 -->

   <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

 

实际开发使用:mvc:annotation-driven

视图解析器配置前缀和后缀:

 

程序中不用指定前缀和后缀:

 

 

posted @ 2016-11-03 21:56  海天依色  阅读(417)  评论(0编辑  收藏  举报