# L8 T1 O' v- k# g1 D/ n! X
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处理请求的流程:
: v( ^0 p- u. K, R/ ]
四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>
" G9 o/ D/ t" ]! K - <web-app version="2.4"
5 ~0 a2 D# B( R9 N - xmlns="http://java.sun.com/xml/ns/j2ee" 1 o0 V: m7 J7 I. }3 V& H. ~
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ Z5 _8 O2 R; z* R' q - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee ) I& y% v- w6 ]
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
) i# y ~. o* t j( P6 K - <servlet>
( P+ y5 S) d3 {3 W) l - <servlet-name>springmvc</servlet-name>: L* X* {8 D2 P
- <servlet-class>
; R; |+ Z! w# H, e" f; N - org.springframework.web.servlet.DispatcherServlet# H# ~) F1 t z* J) ]2 Q! @! D. B/ f6 }
- </servlet-class>0 o* {% @5 ?# a" H; h% g
- <init-param># f/ m$ _. Q! R- p3 j9 k# H @6 v
- <param-name>contextConfigLocation</param-name>6 W3 c2 U9 d* ]& O
- <param-value>classpath:applicationContext.xml</param-value>6 e5 q1 J4 }6 \/ ^: Z0 N" m
- </init-param>
* {. c9 O) V) t- k+ v8 s+ r" l3 |' i) F - <load-on-startup>1</load-on-startup>' }2 r( v" {# r
- </servlet>
; y/ b: @) m' { - <servlet-mapping>
$ r' I# P8 }0 D+ z L- o' O, v - <servlet-name>springmvc</servlet-name>
0 G: Y0 J# u7 Q. ^. L. k$ b1 V - <url-pattern>*.do</url-pattern>
# N: f: V: ~1 S6 b9 B F) ? - </servlet-mapping>, M7 F. O/ G8 i* O8 d- r6 B4 A
- <filter>8 \; j8 M: C5 R/ B, ~7 [" s
- <filter-name>CharacterEncodingFilter</filter-name>, ^7 t$ @4 S/ @, H' |
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
4 A/ }/ o' W/ k - <init-param>5 n/ u8 P7 x% f! e
- <param-name>encoding</param-name>
3 O! _7 E# G/ ~& g8 g, j. ]" m - <param-value>utf-8</param-value>
# @! }) e" ]& I7 U# } `" h9 m - </init-param>/ W' I( t8 a5 m% }
- </filter>7 q8 m( `5 v3 X! [+ ~
- <filter-mapping> B9 A8 ^# b% z5 R1 B
- <filter-name>CharacterEncodingFilter</filter-name>
) g4 M# b1 r4 b a5 ?6 Q - <url-pattern>/*</url-pattern>, U. g1 V6 Q& W6 A. W
- </filter-mapping>
$ V2 ?+ e+ m5 J& _4 r: ^+ v - <welcome-file-list>
2 G$ i7 z8 Q6 G6 Q8 Z - <welcome-file>index.jsp</welcome-file>) W% ~( V) ?! M5 }- ]0 O% n: j
- </welcome-file-list>
9 }/ g0 ]& \+ w% V9 U4 u( W$ ]9 r - </web-app>( O( w4 M/ I9 N( h; }* {9 q* }
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>
' Z6 [; Q/ }# S - <beans xmlns="http://www.springframework.org/schema/beans" * [0 N& R; Q+ t* c) a3 J
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' A' \* t/ ]3 C' _# }
- xmlns:tx="http://www.springframework.org/schema/tx" 1 N) Z s2 n n. a X$ G
- xmlns:aop="http://www.springframework.org/schema/aop"1 d" F( n: b4 Y- x/ R
- xmlns:context="http://www.springframework.org/schema/context"
# v) L8 n' w' }$ W- c- ^$ D - xmlns:jee="http://www.springframework.org/schema/jee"
/ O7 D3 x" X }7 L - xsi:schemaLocation="+ G( l' [8 Y% g' l0 n
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd& r& Z9 f. G* E7 a0 O( f
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd5 f9 j- \# J' V+ M6 I
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd2 S7 N, S3 `+ Z8 Q) R* O0 W8 B
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
0 q8 a6 B5 s! E, m6 G - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">6 P' ^8 e, w6 N7 s _
- <!-- 定义映射处理器,指定请求和controller对应关系 -->
3 Y1 R, t: D+ d% _ R - <bean id="handlerMapping" * P9 ^, Z. F; _ F
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">9 o* I9 G; q* g
- <property name="mappings"># l2 K3 p" D4 d& h5 g
- <props>) g# v' V4 g. z1 c/ K+ C* i, g/ X
- <prop key="welcome.do">welcomeController</prop>' V7 r6 N& s8 C# q5 B/ \
- <prop key="login.do">loginController</prop>
3 O0 l+ I& I6 C( n - <prop key="toLogin.do">toLoginController</prop>
1 R5 O* z" k! }. ^4 ~$ H5 } - </props>
+ X$ J6 v" q, O! P# e) H' t, g) t% P4 P - </property>
0 F4 _1 H* V k# s3 t8 V5 u - </bean>
) U2 d4 r! L4 j; X/ m8 i( h - <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->$ } k+ z% f \4 J* G
- <bean id="viewResolver"
$ I7 Y, q, q, j% `) W. V - class="org.springframework.web.servlet.view.InternalResourceViewResolver">
4 j0 M \! p! h& e - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>4 z8 `6 ?$ {' g- K6 f
- <!-- 配置视图后缀 --> ) U; h. E5 \, m6 }5 P
- <property name="suffix" value=".jsp"></property>
8 G# @' f6 e! a - <!-- 配置视图前缀 -->
* `1 E- Y! l9 C# g - <property name="prefix" value="WEB-INF/jsp/"></property>
: z& x1 B% b9 [5 Q - </bean>
" |. I2 o! v% Y) u4 C# b. x: q - <!-- 定义Controller组件,等价于原来的Action -->- M; a+ Q& z# G1 l! a, K+ X9 W* E
- <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>6 x: u8 N! [/ w6 O2 _; V' U1 y. h
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController">
: S9 @( m6 @! ]2 e9 C( h+ Q - <property name="commandClass" 1 d, u, g7 C; O4 R6 N8 ]( b
- value="itstyle.entity.User">
4 y! x& B/ U0 y3 ] - </property>
# T9 i. G- f8 }$ [ - </bean>! V _* Y' m- K# d1 u& H" R# h
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>5 u- r# h. O, D
- </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。) Z1 E0 s6 g, Z Z
/ O9 [5 S4 ^- ~3 S" C
|