+ A% y9 c1 U! a# q) x8 J9 n
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处理请求的流程:
( Z0 Y$ t0 E! O* R j 四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>
2 n0 O$ g7 l8 r* N6 e: l* H - <web-app version="2.4"
$ s6 W; m1 ?9 P9 d3 Q - xmlns="http://java.sun.com/xml/ns/j2ee"
( r4 b4 a3 \4 Q/ U- y - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 [; P( J- Y) Y) P' x
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 7 l; H Q- ~/ h5 } N
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
+ I1 a1 ?' s* \( M& O9 R9 V - <servlet>. Q, v% V6 o. A: }1 N: W! |& Z
- <servlet-name>springmvc</servlet-name>0 j9 P1 ?' f2 w
- <servlet-class>, A; c1 g+ F2 q7 h8 Q9 Z, N
- org.springframework.web.servlet.DispatcherServlet
+ _+ y1 Y& _% v& A) f u* X& B - </servlet-class>
; F# n* F$ o) I! | o - <init-param>7 G" t3 I3 `- G X9 d, {/ r/ _
- <param-name>contextConfigLocation</param-name>
# k/ h+ H2 k2 x& y; B: N - <param-value>classpath:applicationContext.xml</param-value>* ?" v% b6 w: B$ n5 S8 y. D5 a F, M
- </init-param>
1 y- g5 V0 N' h( x. i" D8 m - <load-on-startup>1</load-on-startup>* q% d5 k/ G, @! n* U4 }% A
- </servlet>; t9 J7 j# P2 i8 o
- <servlet-mapping>
3 K9 L5 S' X+ m/ ^) e5 J! ?. s - <servlet-name>springmvc</servlet-name>+ k( {) G8 f7 ]: N% |
- <url-pattern>*.do</url-pattern>2 q# Y7 Z$ ?: a0 ?1 R4 I
- </servlet-mapping>: i& f! u% D' {6 i# j4 {, f
- <filter> J) D- c6 @; K6 A. f. g+ X& o4 R
- <filter-name>CharacterEncodingFilter</filter-name>: V4 _' E- r- ?9 t' m
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>3 A" H, T$ p' ?8 G
- <init-param>$ i/ l5 G; E+ d
- <param-name>encoding</param-name>; O* b( K5 {( h2 E/ g% j( G. {
- <param-value>utf-8</param-value>
! [/ w( v* Y/ R6 a/ f1 r - </init-param>
; }( i- d7 K* P7 I" [ - </filter>7 J8 X$ K7 j; p- f+ `/ M# H
- <filter-mapping>: ~3 h( k9 m" g! [
- <filter-name>CharacterEncodingFilter</filter-name>
4 b8 I4 r, a4 r# U V6 [ - <url-pattern>/*</url-pattern>; B2 v& I" ]8 Y0 W' |
- </filter-mapping>1 k4 j, n3 w7 J$ D1 s$ O7 h; v9 H
- <welcome-file-list># O8 K! W/ h* }
- <welcome-file>index.jsp</welcome-file>
2 F' G: t1 w( b! P* s - </welcome-file-list>: g- ~1 A/ f$ ?4 p# j# r# `3 \( q
- </web-app>
7 U/ P% j4 x4 t6 f0 h2 H
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>! I0 _! c* t: P$ d. j( y( b
- <beans xmlns="http://www.springframework.org/schema/beans"
" n* N3 i/ D9 ]. I# e& ]( z - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"$ {5 a @4 T: p8 R
- xmlns:tx="http://www.springframework.org/schema/tx" . X/ N1 P3 i2 p2 d3 R, }, J1 q
- xmlns:aop="http://www.springframework.org/schema/aop"2 `4 G# s& _5 i9 Q, G( u
- xmlns:context="http://www.springframework.org/schema/context"
; Y1 ^: I k" _; E% ~# N. y3 w - xmlns:jee="http://www.springframework.org/schema/jee"
: v6 }/ d& a1 ? - xsi:schemaLocation="& o9 Z5 h! j1 X. C5 G9 R+ s$ `
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd9 Z+ w5 s' p5 l; j. D% W/ c9 }
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd2 z" v8 Y: G$ @/ n0 D
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
( M" r% K# {, w5 } - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
# x) k' S$ F3 h/ P4 p! S7 Z$ J7 K - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">( p3 N8 e1 {- w( P3 S; ]
- <!-- 定义映射处理器,指定请求和controller对应关系 -->
; C W) ? ^+ F" A - <bean id="handlerMapping" : q$ v/ N; V- p4 ^* k" l7 S
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
0 _# T% t G5 B - <property name="mappings">6 ?) X$ z" |# ?& ^
- <props>
& P, \ G5 w& P. J- f - <prop key="welcome.do">welcomeController</prop>; Z0 N6 i' h1 p9 k8 K7 ]
- <prop key="login.do">loginController</prop>
% f( v# o, V6 v - <prop key="toLogin.do">toLoginController</prop>0 _8 j3 [/ l9 D+ l; B! t
- </props>
5 v+ `. f6 Y9 k6 s8 F - </property>
) ]) O, i( `2 w" w- Y3 f, O - </bean>; X2 Y' \8 m) d r. E
- <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->
3 `5 `; Q7 T$ j. }7 G8 X4 e - <bean id="viewResolver"
+ G+ B2 J+ T) p6 p) [ - class="org.springframework.web.servlet.view.InternalResourceViewResolver">) G T5 f1 b/ z5 G+ C! k8 {
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
% @% K+ c/ A& M) w9 T - <!-- 配置视图后缀 --> 3 r( F! Q) j7 U' [4 {7 N
- <property name="suffix" value=".jsp"></property>
, K* a: `) V& @4 h0 o; b. h; D - <!-- 配置视图前缀 --> % ?; B4 j9 s$ p& O1 ?0 z
- <property name="prefix" value="WEB-INF/jsp/"></property>
& j6 p/ z5 F' r/ N, g6 f - </bean>
" S$ l1 D3 p) \# N; l - <!-- 定义Controller组件,等价于原来的Action -->5 z+ ~: H- W& W
- <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>/ O3 Z$ @+ x4 ]
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController">
! ^% o! }8 Q$ C - <property name="commandClass"
4 T; ]" B2 o9 |8 t8 W3 q) G - value="itstyle.entity.User">
5 T# H3 J" U! n( c" P, V1 E7 { - </property>' Q# y6 }* k# r7 d+ I) x
- </bean>$ D% M/ _7 H. W* d5 Z
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean> m K+ C. L0 b9 _. U+ o
- </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。
9 o! z/ ]% N5 C a0 M3 c' O
4 C4 G5 z6 ]( B/ k
|