SpringMvc与Struts2的区别
原来为 table,需要自己转换一下{"cells":[{"mergeHeight":1,"mergeWidth":4,"verticalAlign":"middle","backColor":"rgb(217, 217, 217)","value":"SpringMVC与Struts2区别","inlineStyles":{"bold":[{"from":0,"to":19,"value":true}],"font-family":[{"from":9,"to":10,"value":"SimSun"},{"from":17,"to":19,"value":"SimSun"}]}},{"mergePointDX":1,"mergePointDY":0,"value":""},{"mergePointDX":2,"mergePointDY":0,"value":""},{"mergePointDX":3,"mergePointDY":0,"value":""},{"verticalAlign":"top","value":"对比项目","inlineStyles":{"bold":[{"from":0,"to":4,"value":true}],"font-family":[{"from":0,"to":4,"value":"SimSun"}]}},{"verticalAlign":"top","value":"SrpingMVC","inlineStyles":{"bold":[{"from":0,"to":9,"value":true}]}},{"verticalAlign":"top","value":"Struts2","inlineStyles":{"bold":[{"from":0,"to":7,"value":true}]}},{"verticalAlign":"top","value":"优势","inlineStyles":{"bold":[{"from":0,"to":2,"value":true}],"font-family":[{"from":0,"to":2,"value":"SimSun"}]}},{"verticalAlign":"top","value":"国内市场情况","inlineStyles":{"font-family":[{"from":0,"to":6,"value":"SimSun"}]}},{"verticalAlign":"top","value":"有大量用户,一般新项目启动都会选用springmvc","inlineStyles":{"font-family":[{"from":0,"to":17,"value":"SimSun"}]}},{"verticalAlign":"top","value":"有部分老用户,老项目组,由于习惯了,一直在使用。","inlineStyles":{"font-family":[{"from":0,"to":24,"value":"SimSun"}]}},{"verticalAlign":"top","value":"国内情况,springmvc的使用率已经超过Struts2","inlineStyles":{"font-family":[{"from":0,"to":5,"value":"SimSun"},{"from":14,"to":22,"value":"SimSun"}]}},{"verticalAlign":"top","value":"框架入口","inlineStyles":{"font-family":[{"from":0,"to":4,"value":"SimSun"}]}},{"verticalAlign":"top","value":"基于servlet","inlineStyles":{"font-family":[{"from":0,"to":2,"value":"SimSun"}]}},{"verticalAlign":"top","value":"基于filter","inlineStyles":{"font-family":[{"from":0,"to":2,"value":"SimSun"}]}},{"verticalAlign":"top","value":"本质上没太大优势之分,只是配置方式不一样","inlineStyles":{"font-family":[{"from":0,"to":20,"value":"SimSun"}]}},{"verticalAlign":"top","value":"框架设计思想","inlineStyles":{"font-family":[{"from":0,"to":6,"value":"SimSun"}]}},{"verticalAlign":"top","value":"控制器基于方法级别的拦截,处理器设计为单实例","inlineStyles":{"font-family":[{"from":0,"to":22,"value":"SimSun"}]}},{"verticalAlign":"top","value":"控制器基于类级别的拦截, 处理器设计为多实例","inlineStyles":{"font-family":[{"from":0,"to":12,"value":"SimSun"},{"from":13,"to":22,"value":"SimSun"}]}},{"verticalAlign":"top","value":"由于设计本身原因,造成了Struts2,通常来讲只能设计为多实例模式,相比于springmvc设计为单实例模式,Struts2会消耗更多的服务器内存。","inlineStyles":{"font-family":[{"from":0,"to":12,"value":"SimSun"},{"from":19,"to":38,"value":"SimSun"},{"from":47,"to":56,"value":"SimSun"},{"from":63,"to":75,"value":"SimSun"}]}},{"verticalAlign":"top","value":"参数传递","inlineStyles":{"font-family":[{"from":0,"to":4,"value":"SimSun"}]}},{"verticalAlign":"top","value":"参数通过方法入参传递","inlineStyles":{"font-family":[{"from":0,"to":10,"value":"SimSun"}]}},{"verticalAlign":"top","value":"参数通过类的成员变量传递","inlineStyles":{"font-family":[{"from":0,"to":12,"value":"SimSun"}]}},{"verticalAlign":"top","value":"Struts2通过成员变量传递参数,导致了参数线程不安全,有可能引发并发的问题。","inlineStyles":{"font-family":[{"from":7,"to":40,"value":"SimSun"}]}},{"verticalAlign":"top","value":"与spring整合","inlineStyles":{"font-family":[{"from":0,"to":1,"value":"SimSun"},{"from":7,"to":9,"value":"SimSun"}]}},{"verticalAlign":"top","value":"与spring同一家公司,可以与spring无缝整合","inlineStyles":{"font-family":[{"from":0,"to":1,"value":"SimSun"},{"from":1,"to":7,"value":"Verdana"},{"from":7,"to":16,"value":"SimSun"},{"from":16,"to":22,"value":"Verdana"},{"from":22,"to":26,"value":"SimSun"}]}},{"verticalAlign":"top","value":"需要整合包","inlineStyles":{"font-family":[{"from":0,"to":5,"value":"SimSun"}]}},{"verticalAlign":"top","value":"Springmvc可以更轻松与spring整合","inlineStyles":{"font-family":[{"from":9,"to":15,"value":"SimSun"},{"from":21,"to":23,"value":"SimSun"}]}}],"heights":[27.7,18.1,26.85,26.85,26.85,26.85,26.85],"widths":[113,113,113,280]}
SpringMvc处理流程

Spring入门
1 导入jar包

2 创建ItemController类
ItemController是一个普通的java类,不需要实现任何接口,只需要在类上添加@Controller注解即可。@RequestMapping注解指定请求的url,其中“.action”可以加也可以不加。在ModelAndView对象中,将视图设置为“/WEB-INF/jsp/itemList.jsp”
@Controllerpublic class ItemController {@RequestMapping("itemList")public ModelAndView itemList(){List<Item> itemList = Arrays.asList(new Item(1, "冰箱1", 20.2, new Date(), "很冷"),new Item(2, "冰箱2", 20.2, new Date(), "很冷"),new Item(3, "冰箱3", 20.2, new Date(), "很冷"),new Item(4, "冰箱5", 20.2, new Date(), "很冷"));ModelAndView mav = new ModelAndView();mav.addObject("itemList", itemList);mav.setViewName("itemList");return mav ;}}
3 创建springmvc.xml
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><context:component-scanbase-package="cn.itcast.springmvc.controller"/></beans>
4 配置前端控制器
<!-- 核心控制器的配置 --><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 加载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>
SpringMvc架构
1 框架架构

2 架构流程
1、 用户发送请求至前端控制器DispatcherServlet
2、 DispatcherServlet收到请求调用HandlerMapping处理器映射器。
3、 处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。
4、 DispatcherServlet通过HandlerAdapter处理器适配器调用处理器
5、 执行处理器(Controller,也叫后端控制器)。
6、 Controller执行完成返回ModelAndView
7、 HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet
8、 DispatcherServlet将ModelAndView传给ViewReslover视图解析器
9、 ViewReslover解析后返回具体View
10、 DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)。
11、DispatcherServlet响应用户
3 组件说明
以下组件通常使用框架提供实现:
用户请求到达前端控制器,它就相当于mvc模式中的c,dispatcherServlet是整个流程控制的中心,由它调用其它组件处理用户的请求,dispatcherServlet的存在降低了组件之间的耦合性。
HandlerMapping负责根据用户请求找到Handler即处理器,springmvc提供了不同的映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等。
Handler 是继DispatcherServlet前端控制器的后端控制器,在DispatcherServlet的控制下Handler对具体的用户请求进行处理。
由于Handler涉及到具体的用户业务请求,所以一般情况需要程序员根据业务需求开发Handler。
通过HandlerAdapter对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行。
View Resolver负责将处理结果生成View视图,View Resolver首先根据逻辑视图名解析成物理视图名即具体的页面地址,再生成View视图对象,最后对View进行渲染将处理结果通过页面展示给用户。
springmvc框架提供了很多的View视图类型的支持,包括:jstlView、freemarkerView、pdfView等。我们最常用的视图就是jsp。
一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面。
说明:在springmvc的各个组件中,处理器映射器、处理器适配器、视图解析器称为springmvc的三大组件。
需要用户开放的组件有handler、view
4 框架默认加载组件

5 注解映射器和适配器
1) 组件扫描器
使用组件扫描器省去在spring容器配置每个controller类的繁琐。使用<context:component-scan>自动扫描标记@controller的控制器类,配置如下:
<!-- 扫描controller注解,多个包中间使用半角逗号分隔 --><context:component-scanbase-package="com.carven.springmvc.controller.first"/>
2) RequestMappingHandlerMapping
注解式处理器映射器,对类中标记@ResquestMapping的方法进行映射,根据ResquestMapping定义的url匹配ResquestMapping标记的方法,匹配成功返回HandlerMethod对象给前端控制器,HandlerMethod对象中封装url对应的方法Method。
从spring3.1版本开始,废除了DefaultAnnotationHandlerMapping的使用,推荐使用RequestMappingHandlerMapping完成注解式处理器映射。
配置如下:
<!--注解映射器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
注解描述:
@RequestMapping:定义请求url到处理器功能方法的映射
3) RequestMappingHandlerAdapter
注解式处理器适配器,对标记@ResquestMapping的方法进行适配。
从spring3.1版本开始,废除了AnnotationMethodHandlerAdapter的使用,推荐使用RequestMappingHandlerAdapter完成注解式处理器适配。
配置如下:
<!--注解适配器 --><beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
4) mvc:annotation-driven
springmvc使用mvc:annotation-driven自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter,可用在springmvc.xml配置文件中使用mvc:annotation-driven替代注解处理器和适配器的配置。
6 视图解析器
在springmvc.xml文件配置如下:
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyname="viewClass"value="org.springframework.web.servlet.view.JstlView"/><propertyname="prefix"value="/WEB-INF/jsp/"/><propertyname="suffix"value=".jsp"/></bean>
InternalResourceViewResolver:支持JSP视图解析
viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,所以classpath中必须包含jstl的相关jar 包。此属性可以不设置,默认为JstlView。
prefix 和suffix:查找视图页面的前缀和后缀,最终视图的址为:
前缀+逻辑视图名+后缀,逻辑视图名需要在controller中返回ModelAndView指定,比如逻辑视图名为hello,则最终返回的jsp视图地址 “WEB-INF/jsp/hello.jsp”
