7 w7 n3 v( m' x) G+ B! }! P, L
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处理请求的流程:
. `- s# Y S K3 C4 _
四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>
& M$ m# ^& l0 |! z# r - <web-app version="2.4"
* f. k0 H g' a; Z& G - xmlns="http://java.sun.com/xml/ns/j2ee" 7 K* n# O9 Q* w" D7 D9 b* t/ q
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - u3 ]4 @; B- j q; C8 v% m
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee & S; m1 C4 s4 q
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
4 D5 e: E. b+ P+ h - <servlet>
' q5 T0 L: O+ v% n7 T# C& D6 Z - <servlet-name>springmvc</servlet-name>
: l2 C, {4 u+ v5 ]! B - <servlet-class> i% l% D+ D, _3 N& J6 q" B" S
- org.springframework.web.servlet.DispatcherServlet: W/ T; R% L+ S2 G1 j4 F
- </servlet-class>9 ~! X0 Y. @$ @! B: n( J
- <init-param>
6 o: {4 ~ P6 L D: P' x' ]9 b - <param-name>contextConfigLocation</param-name>
0 h* ]) Y) c6 F6 b! a, l - <param-value>classpath:applicationContext.xml</param-value>3 [$ z3 m7 \7 k4 W T! I) }& ?, ^- {
- </init-param> R7 D5 n5 `+ y, y% }
- <load-on-startup>1</load-on-startup>7 y) S7 k6 ^5 f9 c9 r
- </servlet>: o" M n7 c+ k
- <servlet-mapping>3 x, p$ p( Z6 z
- <servlet-name>springmvc</servlet-name>3 e$ A1 G2 @# h0 [
- <url-pattern>*.do</url-pattern>
8 g) g6 Q+ k6 o) f4 F' g1 M - </servlet-mapping>) w v' E/ @" |2 X- U+ b7 Q
- <filter>
4 w; {8 M2 s0 h" ] - <filter-name>CharacterEncodingFilter</filter-name>2 u1 E9 f0 f& U/ P# ~' `/ @, I
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
8 E/ R8 Y# e T4 F: a( {% y6 g0 P - <init-param>9 x1 z1 R, n7 |- ^6 k- e* o2 _9 @
- <param-name>encoding</param-name>4 l- s$ N/ |. M
- <param-value>utf-8</param-value>- Z( L6 N8 D8 x5 L u* [
- </init-param>
! c- Y. R: ^$ u' F3 ^4 q6 Z+ @ - </filter>
' z. m K$ V6 }. G4 u4 k: q - <filter-mapping>1 b+ @* b, g0 [. p& A' ^& N7 ~
- <filter-name>CharacterEncodingFilter</filter-name> [* A- `# r( f0 N2 d9 Q
- <url-pattern>/*</url-pattern>
. @, B R" p- X* W# c - </filter-mapping>
0 `& v/ h+ W3 c5 l6 h y% B# | - <welcome-file-list>& }& {" X) f; ]& i+ r2 J
- <welcome-file>index.jsp</welcome-file>( n$ W* P8 ?# u5 Z
- </welcome-file-list>
; L( o+ w+ R# s# [% Z# b - </web-app>
0 Z# @( c0 Q# |2 m$ X' q& k' |* J
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>; C# u3 o% |( H/ k
- <beans xmlns="http://www.springframework.org/schema/beans"
/ E, _! e8 D: `% N: K) w! X/ N - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
: Z' g' u& c" |8 J - xmlns:tx="http://www.springframework.org/schema/tx"
, q/ H2 r# h$ _: @ - xmlns:aop="http://www.springframework.org/schema/aop"3 X& h2 u; Z( ^! W; w
- xmlns:context="http://www.springframework.org/schema/context" ( a* o3 m, U; f6 X( D
- xmlns:jee="http://www.springframework.org/schema/jee"
$ J6 q* g+ L" p - xsi:schemaLocation="
6 K5 q8 z+ P0 ]2 {8 ? - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
j! r* b1 ]5 V* U& I3 c - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd3 T6 `$ \3 ~4 g6 u Q
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
( u) o, ?* X# F2 a - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd! Q" o1 V* F/ Z1 K7 D
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
4 _5 L, i0 z6 w! }1 C3 o" m' ^ - <!-- 定义映射处理器,指定请求和controller对应关系 -->: |$ \, z; Q$ x$ Z f' t* j9 @
- <bean id="handlerMapping"
/ r A6 J- F. @& ~& G) N/ m7 T. N - class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
8 ^. }3 k7 @5 K1 q) x - <property name="mappings">
: U6 K+ |$ p" W% X8 N- A6 _+ @ - <props>
( X+ O$ s x# A$ M+ F - <prop key="welcome.do">welcomeController</prop>6 Z; g& M. b' j1 ]3 r( v
- <prop key="login.do">loginController</prop># p1 {* N" W/ g- S. ?
- <prop key="toLogin.do">toLoginController</prop>
& K, a; ^8 d$ H2 v6 P - </props> ]7 j8 X5 m" s! l ~4 ^
- </property>9 d5 j( j( Z8 I. D- Z% q
- </bean>
+ z, P; A' k) V/ z9 I$ i - <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->
; n' E; P: T- J - <bean id="viewResolver" / N6 H- Q* |% o: N& ` T
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
% A. c! o$ Z; @* ^& S3 ? - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>: l) Z: q9 [7 Y+ ?
- <!-- 配置视图后缀 --> % C. Q3 h2 b) F# A, F
- <property name="suffix" value=".jsp"></property>
3 @# M% e4 H( Z. w7 r. { - <!-- 配置视图前缀 --> 6 `, V! y4 c! |$ [( h
- <property name="prefix" value="WEB-INF/jsp/"></property>
1 x, ?2 L' e0 U( k# M8 }" M - </bean>
$ Q# \- n3 d3 W2 w - <!-- 定义Controller组件,等价于原来的Action -->
8 b8 X* G. _% C: ? - <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>2 k; U. b" C) p; V& `8 f& h; {% {" l
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController">
! p- {" U" R" G/ }/ s* @; | - <property name="commandClass"
' r& D! A/ J) {/ c- o - value="itstyle.entity.User">
$ q1 \; J2 j9 e! d& B3 y2 ~ - </property>
9 k) e. z5 b I4 ~. n( Z - </bean>
& V( o0 j u4 Y- \' k& a: @ - <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>; ]& K' s% x9 ?+ K4 B
- </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。9 Y& {) p0 C5 t) ?: Q
$ Q" q. ]& {4 J: N# `
|