2 R% \9 U! V4 I- {: ~- {3 _, c
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处理请求的流程:
0 N' `9 ]7 T- s; H Z/ \
四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>
+ Q2 e7 Y* F3 R( r6 C - <web-app version="2.4" # L ?% t" P6 l( [" ]
- xmlns="http://java.sun.com/xml/ns/j2ee" 5 ?. C7 ]* u8 s- f; ~
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
$ [7 ^" @8 m) x% |; O2 y - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
$ w" K) X8 V# V1 o1 L4 j2 W - http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
% u7 ^/ R G0 N2 g7 h! ^# | - <servlet>+ ^3 [9 S; _3 {2 }6 I# f6 D; W% Y
- <servlet-name>springmvc</servlet-name>. V, M# S4 T& y, w* n9 _. B m
- <servlet-class>. n N7 o% j! I+ P4 ~
- org.springframework.web.servlet.DispatcherServlet
( g- K4 M5 ]- J# {% \6 X - </servlet-class>. f8 k0 Y" H) y, X
- <init-param>1 W, z- A# b- I; J1 u
- <param-name>contextConfigLocation</param-name>
8 A0 _! K- r" R, M) A - <param-value>classpath:applicationContext.xml</param-value>
+ _- ?# @( F5 e' d - </init-param>+ ]" g8 X- E8 q8 ^8 ?
- <load-on-startup>1</load-on-startup>
* f! g+ z5 _, E; p* M - </servlet>
0 {2 W6 f* B, } `" O# V& Y - <servlet-mapping>
& \0 q W; v* H4 \ - <servlet-name>springmvc</servlet-name>
1 G. V2 R1 r# s" i# ^6 [2 p9 G - <url-pattern>*.do</url-pattern>
4 L( t2 h7 V! ] - </servlet-mapping>8 c+ \2 g5 g/ I/ c
- <filter>
% H. U' B9 t; X1 g( |9 b - <filter-name>CharacterEncodingFilter</filter-name>
+ r$ ?, ~+ U' X% d3 [, ? - <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
4 r3 a! D7 Q+ I4 j9 r - <init-param>
; T8 ?( s1 {: J w - <param-name>encoding</param-name>
. n! w6 V4 N7 p$ S. t3 s - <param-value>utf-8</param-value>' B/ ~: C$ f8 \! h& f; H9 ~/ M7 L
- </init-param>
2 K3 b& m2 T$ ^8 h - </filter>
. U) c X' `5 I& U - <filter-mapping>2 S8 ~; F/ g; u. F( T
- <filter-name>CharacterEncodingFilter</filter-name>, X& i- ?8 p* v8 d4 w& k0 g; T5 s
- <url-pattern>/*</url-pattern>5 v: O5 e8 F4 B0 i6 b( q
- </filter-mapping>+ d! ?; J8 x8 b9 E' r
- <welcome-file-list>
, J5 k9 L; N/ s& i - <welcome-file>index.jsp</welcome-file>' o5 F9 E5 Z& H- E
- </welcome-file-list>0 [2 h+ m* f9 J" l5 \$ j9 o
- </web-app>
! Y" l- ~: h) F5 {
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>
% | U2 w- F; k: J6 l - <beans xmlns="http://www.springframework.org/schema/beans"
, k p1 i5 q5 Z - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
! q3 d% O: d" Y1 y - xmlns:tx="http://www.springframework.org/schema/tx"
( u1 ~. t1 d- J( `3 c9 i+ C" z - xmlns:aop="http://www.springframework.org/schema/aop"
& n5 B; \) W1 f o) j - xmlns:context="http://www.springframework.org/schema/context"
1 a1 \/ k1 P& f- Y1 X - xmlns:jee="http://www.springframework.org/schema/jee"
4 g4 K x! W$ q& E/ P8 A - xsi:schemaLocation="
- t$ G) h* P! f% p$ N) G - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
# v$ Q) f. V; q% P6 t - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
! w- j) h& i9 S3 { - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
; a* m/ M4 ?& U' W6 s - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd0 ?9 m% o6 @) h c
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">( W7 U! H% i0 ~$ n( v1 j0 p# I
- <!-- 定义映射处理器,指定请求和controller对应关系 -->
, B& h& l0 |8 q7 m% m - <bean id="handlerMapping" / G( g' ^! o- U$ ~, y* t6 k$ A
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">, r' l$ Y& X& p2 V4 X, e
- <property name="mappings">2 n% r/ f& z R e. B
- <props>. s$ @, m7 {" Y- z/ G6 f* Y
- <prop key="welcome.do">welcomeController</prop>
* R9 @5 n& \6 @. V- t - <prop key="login.do">loginController</prop>6 l1 ?# [: P. \" n7 K b* Z0 w
- <prop key="toLogin.do">toLoginController</prop>
4 Z& Q% t: @9 j; k# M, _ - </props>
% \$ } w" I4 B. A4 s/ _3 \6 z - </property>5 ~. m4 q) X; q. z3 ^' B( f
- </bean>9 y" g! b9 o( M7 ^3 L+ ?) q# ^
- <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->
$ n! M& P. c8 n) q! w" a& j - <bean id="viewResolver"
% E/ V0 B& X! i. H6 D - class="org.springframework.web.servlet.view.InternalResourceViewResolver">
, x( C1 `7 Y9 _0 X1 {" V; @3 v& [ - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>) _: `' y6 D% `* c( T3 k
- <!-- 配置视图后缀 --> ( u4 j! V, u* c- T: _9 M z
- <property name="suffix" value=".jsp"></property> R+ r' p- s% g
- <!-- 配置视图前缀 --> 3 o( ?& a) T( C; B( e) t
- <property name="prefix" value="WEB-INF/jsp/"></property>
) ] A* @3 b) Q - </bean>0 W, n- b1 t, Y, I; n* q, M
- <!-- 定义Controller组件,等价于原来的Action -->2 H1 n0 U6 {, p$ K9 I1 v: R; h
- <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>. B2 U) F* X# o2 r
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController">
" \# i, q1 l' L$ r! Z - <property name="commandClass" ( [( N3 k" x k2 [# c0 C$ D
- value="itstyle.entity.User">1 q8 d0 N; {, m
- </property>
1 I5 g/ t6 X" l& X. W - </bean>
! Y9 B" c4 S! F - <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>
0 B/ _& J7 j& Z" m; |8 {) D - </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。3 j3 A2 }$ n" u; K1 r
/ E+ p. Y; F, Z
|