1.
commons-logging-1.1.3.jar
jstl.jar
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-context-support-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-jdbc-3.2.0.RELEASE.jar
spring-orm-3.2.0.RELEASE.jar
spring-test-3.2.0.RELEASE.jar
spring-web-3.2.0.RELEASE.jar
spring-webmvc-3.2.0.RELEASE.jar
standard.jar
2.
<!-- springMVC前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc加载的配置文件(处理器映射,适配器) -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
3.
<!-- 处理器映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
4.springmvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
5.非注解的映射器
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
另一个映射器
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
<!-- 简单的URL映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/items1.action">itemsController</prop>
<prop key="/items2.action">itemsController</prop>
</props>
</property>
</bean>
1.多个映射器可以并存。
6.非注解的适配器和映射器
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
1.要求编写handler实现controller接口
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
//调用service查找数据库,查询商品列表,这里使用静态数据模拟
List<Items> items=new ArrayList<>();
Items i1=new Items();
i1.setName("香蕉");
i1.setPrice(12.6f);
i1.setDatail("好吃营养");
i1.setCreatetime(new Date());
Items i2=new Items();
i2.setName("苹果");
i2.setPrice(1.6f);
i2.setDatail("好吃营养");
i2.setCreatetime(new Date());
items.add(i1);
items.add(i2);
//返回ModelAndView
ModelAndView andView=new ModelAndView();
andView.addObject("itemsList", items);
//指定视图
andView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return andView;
}
另一个适配器
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
1.要求编写handler实现httpRequestHandler接口
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
//调用service查找数据库,查询商品列表,这里使用静态数据模拟
List<Items> items=new ArrayList<>();
Items i1=new Items();
i1.setName("香蕉");
i1.setPrice(12.6f);
i1.setDatail("好吃营养");
i1.setCreatetime(new Date());
Items i2=new Items();
i2.setName("苹果");
i2.setPrice(1.6f);
i2.setDatail("好吃营养");
i2.setCreatetime(new Date());
items.add(i1);
items.add(i2);
//设置模型数据
request.setAttribute("itemsList", items);
//设置转发视图
request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request, response);
//此方法可以通过修改response,设置响应数据格式,比如:josn
}
7.注解适配器和映射器
1.在spring3.1前使用 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping 注解映射器
2.在spring3.1后使用 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 注解映射器
3.在spring3.1前使用 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器
4.在spring3.1后使用 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器
5.
<!-- 代替注解适配器和映射器配置,
默认加载很多绑定方法:json转换解析器 。
实际开发使用。
-->
<mvc:annotation-driven></mvc:annotation-driven>
6.注解开发handler
/**
* @描述:注解 实现controller接口的处理器
* @version 创建时间: 2016-10-31:下午5:37:58
* @E-mail 邮箱: ***@126.com
* @author 作者: ***
*/
@Controller
public class ItemsController3{
/**
*
* @version 创建时间: 2016-11-1:下午3:22:38
* @author 作者: ***
* @描述:将url与方法写成一样。
* @requestmapping实现对方法和url进行映射(注解映射器),一个方法一个url.
*/
@RequestMapping("/queryItems")
public ModelAndView queryItems()throws Exception{
List<Items> items=new ArrayList<>();
Items i1=new Items();
i1.setName("香蕉");
i1.setPrice(12.6f);
i1.setDatail("好吃营养");
i1.setCreatetime(new Date());
Items i2=new Items();
i2.setName("苹果");
i2.setPrice(1.6f);
i2.setDatail("好吃营养");
i2.setCreatetime(new Date());
items.add(i1);
items.add(i2);
//返回ModelAndView
ModelAndView andView=new ModelAndView();
andView.addObject("itemsList", items);
//指定视图
andView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return andView;
}
}
7.配置handler
<!-- 对于注解的handler可以单个配置
实际开发中使用组件扫描:
-->
<bean class="cn.lanz.ssm.controller.ItemsController3"/>
<context:component-scan base-package="扫描包">
</context:component-scan>
8.源码分析:
通过前端控制器源码分析springMVC的执行过程
1.前端控制器接收请求。
调用doDispatch
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
2.前端控制器去调用映射器查找handler.
返回执行器的链接
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
for (HandlerMapping hm : this.handlerMappings) {
if (logger.isTraceEnabled()) {
logger.trace(
"Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
}
HandlerExecutionChain handler = hm.getHandler(request);
if (handler != null) {
return handler;
}
}
return null;
}
3.调用适配器执行handler得到执行结果modelandview
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
4.视图渲染
视图解析
// We need to resolve the view name.
view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request);
9.视图解析器前缀、后缀。
//指定视图
andView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
//在视图里已经配置好前缀和后缀
//andView.setViewName("items/itemsList");
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp"></property>
《!--后缀--》
<property name="suffix" value=".jsp"></property>
</bean>