, g8 B' m6 u! T, K+ Y
Spring Web MVC是一种基于java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的。 另外还有一种基于组件的、事件驱动的Web框架在此就不介绍了,如Tapestry、JSF等。 Spring Web MVC也是服务到工作者模式的实现,但进行可优化。前端控制器是DispatcherServlet;应用控制器其实拆为处理器映射器(Handler Mapping)进行处理器管理和视图解析器(View Resolver)进行视图管理;页面控制器/动作/处理器为Controller接口(仅包含ModelAndView handleRequest(request, response) 方法)的实现(也可以是任何的POJO类);支持本地化(Locale)解析、主题(Theme)解析及文件上传等;提供了非常灵活的数据验证、格式化和数据绑定机制;提供了强大的约定大于配置(惯例优先原则)的契约式编程支持。 二:Spring Web MVC能帮我们做什么 √让我们能非常简单的设计出干净的Web层和薄薄的Web层; √进行更简洁的Web层的开发; √天生与Spring框架集成(如IoC容器、AOP等); √提供强大的约定大于配置的契约式编程支持; √能简单的进行Web层的单元测试; √支持灵活的URL到页面控制器的映射; √非常容易与其他视图技术集成,如Velocity、FreeMarker等等,因为模型数据不放在特定的API里,而是放在一个Model里(Map数据结构实现,因此很容易被其他框架使用); √非常灵活的数据验证、格式化和数据绑定机制,能使用任何对象进行数据绑定,不必实现特定框架的API; √提供一套强大的JSP标签库,简化JSP开发; √支持灵活的本地化、主题等解析; √更加简单的异常处理; √对静态资源的支持; √支持Restful风格。 三:Spring Web MVC架构 Spring Web MVC框架也是一个基于请求驱动的Web框架,并且也使用了前端控制器模式来进行设计,再根据请求映射规则分发给相应的页面控制器(动作/处理器)进行处理。首先让我们整体看一下Spring Web MVC处理请求的流程:
" I: J" A/ i3 u7 M
四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>
5 |/ y' g( F# \3 L# ]+ ` - <web-app version="2.4" 2 w& Q/ X# u7 T" u, G
- xmlns="http://java.sun.com/xml/ns/j2ee"
4 [9 f/ `. Z9 W - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
! s) ^) R# l7 L' Q; a" U3 J6 d4 t - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
! u: h9 H x! e. o9 k3 d+ K - http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
* ]/ @! }/ X: J* q% l, W5 [ - <servlet>
4 g% K# W8 _4 Q# w* M% u( ? - <servlet-name>springmvc</servlet-name>7 B& C2 j9 c6 J
- <servlet-class>
6 z6 { C$ n- d, G$ @# e - org.springframework.web.servlet.DispatcherServlet. _& _9 X* ]4 \: W/ Q" R8 N
- </servlet-class>8 ^, b D4 c& ]% e2 Q" p
- <init-param>
- N; D% u$ n# G$ U5 Y$ h" _ - <param-name>contextConfigLocation</param-name>3 h3 _+ ]/ [- E3 o$ R% B
- <param-value>classpath:applicationContext.xml</param-value>
; G* q# K5 l: `2 d; m - </init-param>1 a2 l( `7 X' q+ L) r! T5 P% [
- <load-on-startup>1</load-on-startup>1 t' c3 j- O4 ^8 e, P
- </servlet> O, I$ N- _5 {6 s
- <servlet-mapping>
3 ?, K' J: t# y, W - <servlet-name>springmvc</servlet-name>0 _6 S# P3 G0 A1 ?1 V" |
- <url-pattern>*.do</url-pattern>
* J; o( ]" a1 A: |0 J" | - </servlet-mapping>
- Z1 x/ l; M1 C4 p# V/ V9 G; n - <filter>
( M! S" j/ h0 ^ - <filter-name>CharacterEncodingFilter</filter-name>
1 @$ g" w, w; {6 i% ? - <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>% B* C! P' C* |) { k
- <init-param>
5 K o b: X% k( j, a Q+ |5 k& V - <param-name>encoding</param-name>
3 k9 d' @, {8 `4 q! v5 `* u( y - <param-value>utf-8</param-value>
( z; F5 C- Q E" n9 [" M - </init-param>
2 `3 ?4 d6 p4 b) M# C - </filter>7 t# X3 S3 T9 v1 O$ y& X$ s. O
- <filter-mapping>
; R1 q/ Q! d, v! c5 j* A+ k - <filter-name>CharacterEncodingFilter</filter-name>
" l( S7 `( d V: j# a - <url-pattern>/*</url-pattern>; }+ N9 h/ g2 P' y* O0 {% A
- </filter-mapping># ]8 o' z( L. m
- <welcome-file-list>
A3 ?4 M0 E& z! T* D+ ]: U - <welcome-file>index.jsp</welcome-file>
* C, t! i; T( d5 H0 m# \ d: L6 u - </welcome-file-list>* N- d# O: `# s( z$ Z
- </web-app>
% e- p- l. }" U: {
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>. G- d, R3 j* }
- <beans xmlns="http://www.springframework.org/schema/beans"
' `( q1 l, r4 q* n+ N - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
) A! V' {% Y* | - xmlns:tx="http://www.springframework.org/schema/tx" ; I8 h! p, z( j7 l
- xmlns:aop="http://www.springframework.org/schema/aop"3 z; ^7 {+ t4 V n" A+ y
- xmlns:context="http://www.springframework.org/schema/context" # r/ H) @- l w+ |
- xmlns:jee="http://www.springframework.org/schema/jee"% s6 O# D" u9 q$ l4 z$ t& R
- xsi:schemaLocation="1 A3 M9 x$ m: d: G4 p; l4 {
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd: y/ \$ f/ { M1 A7 v. |
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd4 k& Q" m g3 M" @8 e8 e; r
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd8 }3 D& Z% A) m2 _3 X
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd7 J1 K. T l( M0 F \. i
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">& l# c! n- q+ n" N6 u" n* B5 R
- <!-- 定义映射处理器,指定请求和controller对应关系 -->
9 r# v9 p+ I' }) e8 S - <bean id="handlerMapping" ) y8 Z, H, L- r) E$ A V
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
" t7 \$ N* ?* P" n0 o( u/ e) u - <property name="mappings">
! q. z: u: r3 j4 I0 E4 [5 r - <props>3 g# ]; S' F! ~: i; T9 J: ~" K
- <prop key="welcome.do">welcomeController</prop>- M; ` x( P% m& z) A2 Z, |
- <prop key="login.do">loginController</prop>% \% `! m! t: E$ ~0 z0 c
- <prop key="toLogin.do">toLoginController</prop>
! V, K" ]9 K/ c& {( ^ - </props>* i0 o5 _) O! c" p( T+ t) _
- </property>
2 W5 Z) ?, g, c' m - </bean>/ K* D8 {9 T& X# ^
- <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->( y3 y8 W' b; p3 E c- i$ i3 W
- <bean id="viewResolver"
S% W- l1 g. {: |. E( j - class="org.springframework.web.servlet.view.InternalResourceViewResolver">' w* _4 j k2 d; k: a- ]
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
, c+ l0 p7 N- p" i4 K6 ~. | - <!-- 配置视图后缀 -->
. N% A) i- N, Z. O) M6 l, } - <property name="suffix" value=".jsp"></property>$ U' p+ ~# v5 S- z5 b! T4 X5 ~
- <!-- 配置视图前缀 --> , H7 z0 i! J6 f) ~/ r8 d" }. ]
- <property name="prefix" value="WEB-INF/jsp/"></property>/ K. u/ N B+ E6 y% U. x8 ]" X
- </bean>* g! p m" J+ Y7 \0 r/ s
- <!-- 定义Controller组件,等价于原来的Action -->4 T, C9 r3 l$ ]3 l# f( @
- <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>6 Z$ }5 [! U+ A! L. m5 `
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController"> ) x9 T9 I" ]' v8 M# {
- <property name="commandClass"
/ O* H4 h/ ]* c. t4 e! U& l - value="itstyle.entity.User">: K, ^: e4 V- i/ [( W5 N5 l9 D
- </property>
; F# ^7 h P; O& f9 o4 l - </bean>7 t6 P9 r# o: v& [
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>
1 x) b4 F( \0 F1 R1 v - </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。5 C" A# o6 E/ F. N, e* Y, G
. |5 r) M% l G# M" e
|