( J4 A/ M. K. H1 g
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处理请求的流程:
$ N4 _0 _* e* @
四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>6 w! t, F" Z: w* n* ~ u; Q
- <web-app version="2.4" ' [2 y% H5 }0 Q" x( @. t% X
- xmlns="http://java.sun.com/xml/ns/j2ee" 5 M+ N7 Y" M) l( z
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" " I* |3 q/ s. W7 l: p
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee , J) B5 l. K4 t5 Y/ S* d# N
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">6 p9 ^; y7 u* N9 |
- <servlet>
" |# y# A# W$ L) _6 H* _2 [ - <servlet-name>springmvc</servlet-name>7 r" V$ z3 Y# A8 X
- <servlet-class>
% k# }3 w& l3 `9 R2 P$ S, g - org.springframework.web.servlet.DispatcherServlet
" z# e2 F Z2 O# ~& V9 K% r - </servlet-class>
" s/ d8 d$ k# j6 ~6 L9 B - <init-param>
j: A* V: G2 E9 j9 v - <param-name>contextConfigLocation</param-name>5 c6 e- ~1 U8 k- n; v1 Z
- <param-value>classpath:applicationContext.xml</param-value>/ M( d& a* x: a6 s0 R
- </init-param>
) p& H& Z4 i4 R# J - <load-on-startup>1</load-on-startup>
: z) a e- f. G( @5 e9 b! O/ Y - </servlet>7 I1 e2 d+ J( N/ }2 M# t. p
- <servlet-mapping>+ J* G5 Q2 F) }8 g1 ]3 {
- <servlet-name>springmvc</servlet-name>) c5 l0 z, z5 K: ~0 U' ]0 n
- <url-pattern>*.do</url-pattern>
: ~- {' K, H/ y! i) s" } w - </servlet-mapping>7 y d9 V* J3 Y
- <filter>( E0 g! W9 K' r! k# M
- <filter-name>CharacterEncodingFilter</filter-name>% | Q; L/ S8 Q6 `. }
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>, j/ I) |2 m+ h) u7 Y1 R8 H& L9 y
- <init-param>8 {5 b5 y% m$ K |
- <param-name>encoding</param-name>
+ I, @7 u, @2 {7 I8 F: D - <param-value>utf-8</param-value>3 [# p; D2 U$ ~! T* i
- </init-param>
8 u; _3 S( y$ C# Z( X7 l5 G - </filter>
* d" H( ~% m( s# m - <filter-mapping>
' p q n" D* }/ H( ]* |- D - <filter-name>CharacterEncodingFilter</filter-name>
5 {0 N( H t; r( S - <url-pattern>/*</url-pattern>% d8 R: _5 M( P+ g- t: t/ S9 M
- </filter-mapping>
7 h/ p. j! f2 E0 H% E - <welcome-file-list>% s- C/ D$ n9 ~( k& _
- <welcome-file>index.jsp</welcome-file>/ y) f* b. B n6 _- U
- </welcome-file-list>
6 b f5 e5 E4 ?( A# c4 i: p - </web-app>
# _1 `7 \/ f5 u; u/ D% q9 u
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>
1 J8 u7 n3 k$ d( d - <beans xmlns="http://www.springframework.org/schema/beans"
3 x6 E; C; g1 g( i - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"1 W1 O/ J, ~+ k1 _0 s: L
- xmlns:tx="http://www.springframework.org/schema/tx" 6 F* c, L# }5 }$ M: t$ x* K# k* ?6 I
- xmlns:aop="http://www.springframework.org/schema/aop"
' e4 M5 u+ \$ h# k - xmlns:context="http://www.springframework.org/schema/context" ) Z5 [& h4 t7 j4 Q8 d& k
- xmlns:jee="http://www.springframework.org/schema/jee"
2 w# j+ b6 A" b u& R7 W - xsi:schemaLocation="
0 U7 e* ^+ [+ s. M5 a - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
1 v h% v! J* F - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
' Q. P \0 Y8 {- ~' M7 C8 J - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd0 b$ J [7 x' }+ R; C, @7 V
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd$ N0 K1 m( e S
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">$ h9 @. C5 E; b/ K; g0 D
- <!-- 定义映射处理器,指定请求和controller对应关系 -->
& K/ B( B1 c7 p - <bean id="handlerMapping"
1 T8 e& `" C* o, ? - class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">0 A. J5 i T: C$ N, F
- <property name="mappings">
4 {$ z( E) u* d& e - <props>
. N& D, I V, k2 \# ^' M$ k - <prop key="welcome.do">welcomeController</prop>4 T% V$ i9 M* j+ A/ x' \ O- A
- <prop key="login.do">loginController</prop>: H) i6 b( {$ F. `2 ~: I8 l2 K
- <prop key="toLogin.do">toLoginController</prop>8 q9 S. `, q. h* x3 j) t; p: J7 {9 j) A
- </props>8 K o o4 @ d1 I' M1 s
- </property>: ?1 V0 i$ s- I& a$ Z* j7 M
- </bean>
4 l7 `+ ]. U' @6 t - <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->
' L$ }7 p% ]% n' I/ W; w2 i; N - <bean id="viewResolver" 2 S1 A1 F4 _ z/ _
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
/ \- X9 f* d% o* w) w3 ]9 F - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>9 l& h; g- Z4 ?- Y5 h" c$ h$ n
- <!-- 配置视图后缀 --> " x0 g3 m- n2 M, ]/ P% ^
- <property name="suffix" value=".jsp"></property>! c1 U3 c9 v. N) ?, [+ S
- <!-- 配置视图前缀 -->
) \' n' L) m! B, }4 ] - <property name="prefix" value="WEB-INF/jsp/"></property>
x& v7 Y* [- M& O& J - </bean>
4 ~$ |. W0 j2 j$ |9 Y8 M* ? - <!-- 定义Controller组件,等价于原来的Action -->
$ O1 r! K$ a) g6 j - <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>$ U) W9 D6 ?" r+ y
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController">
; D1 p3 \1 w5 Z - <property name="commandClass"
$ \( t }/ x& @0 t& [1 g - value="itstyle.entity.User">
' M" R& b9 g. E! M - </property>- d9 w3 _% h. V. D( x
- </bean>
9 a; G* k6 w. B9 }1 ^6 _# U - <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>
/ }1 W) j H$ H3 \- _: R - </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。
9 l! S0 A5 z; l6 x
: [9 N0 B' j, l! K, P1 P
|