7 i+ c' X% q( K3 K+ U) 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处理请求的流程:
0 v% h$ B! L! I( u6 Q3 Q
四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>
, w8 y) Y# [6 f7 r1 l - <web-app version="2.4"
7 z- ]) U# d, ? W" [2 f6 l - xmlns="http://java.sun.com/xml/ns/j2ee"
, }+ W, D1 R, {/ \3 A6 N - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6 s" T) N6 S; @3 D0 B6 ~ - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 0 ]" C6 X! @5 U, n, u
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">7 ]/ c9 O6 S9 J& J- S: A6 S- F* _
- <servlet>
* i8 J; ^* m8 S - <servlet-name>springmvc</servlet-name>
! R! I! j% J, B: g - <servlet-class> V# q8 q0 { Z8 E" E- Y7 _
- org.springframework.web.servlet.DispatcherServlet
, M( w" c; Y! E: [9 T5 n" I! Z - </servlet-class>
( ?0 i2 H. Z. w7 F) X - <init-param>
1 w2 a" u. b6 j$ g0 f' G' i - <param-name>contextConfigLocation</param-name>
0 g+ m R4 Z4 H# R9 S3 e - <param-value>classpath:applicationContext.xml</param-value>
: P- _* d# s$ H2 t' V, B4 c' E - </init-param> g4 R, D" A2 r/ U+ z
- <load-on-startup>1</load-on-startup>$ |7 e2 d/ e5 L2 A
- </servlet>; x! @* w. ^# ~8 m
- <servlet-mapping>) f+ u4 b& h5 `% b) v
- <servlet-name>springmvc</servlet-name>
" N7 t$ i+ E, C Q6 i- i0 G3 r# h - <url-pattern>*.do</url-pattern>$ y. T& r }! O. I( T0 d4 f Y. b
- </servlet-mapping>
# u4 S3 U9 r$ L; Q5 j% B* ] - <filter>
* `: Z w2 o2 v. S C: C - <filter-name>CharacterEncodingFilter</filter-name>+ I2 K- v s( O+ L4 ~ `
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
. Z8 l3 f! u7 C- }- E0 I - <init-param>; Q* B4 k2 k* g* p( A r+ E1 Z- R# t
- <param-name>encoding</param-name>2 g) ]) ?1 I. a% E
- <param-value>utf-8</param-value>' k, O" Q3 k6 S" o5 }
- </init-param>* C. D6 p8 T% U% J; U8 m2 u& R
- </filter>" w8 e0 A1 V. E5 R/ Q& f, c
- <filter-mapping>: W- v* v6 ~- h; U! Y. a+ X8 {
- <filter-name>CharacterEncodingFilter</filter-name>
6 M' x; G5 D$ I - <url-pattern>/*</url-pattern>8 o) b' h1 q& G; |& H5 \3 z( S' ^* \$ u
- </filter-mapping># k3 e3 g* f3 |0 \& Y9 b! v
- <welcome-file-list>" C; ?% p" B ?) \
- <welcome-file>index.jsp</welcome-file>0 g s# q& H# S8 w0 s
- </welcome-file-list>
$ y/ r6 g+ `0 D - </web-app>
( S, b/ y. a# L% k; \- \& p
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>5 E0 @( @ h, Y8 t- b+ J
- <beans xmlns="http://www.springframework.org/schema/beans" * I C" j$ R6 x
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 p3 O; E+ s1 q3 M' E - xmlns:tx="http://www.springframework.org/schema/tx"
! u1 U' F, f; W1 P/ ` - xmlns:aop="http://www.springframework.org/schema/aop"
9 `/ L6 J, f' E4 Q+ q - xmlns:context="http://www.springframework.org/schema/context"
5 d' D! V K7 X- I8 |2 r2 B - xmlns:jee="http://www.springframework.org/schema/jee"8 ?/ t' w4 P( t$ O7 l7 Z/ D, O' w7 E
- xsi:schemaLocation="
& k! s% K1 ?) B2 Q+ l: q7 e - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
' i& ]1 T6 r. L - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
4 J( D. n. {% y" C9 U3 w - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd1 u- Q6 u6 F- U. A( g) I$ s9 ~5 r3 |
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd d/ n5 n: X) j# L9 |! [; Y
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">. W& F+ V- R4 T4 n$ ?& F
- <!-- 定义映射处理器,指定请求和controller对应关系 -->4 t; D; t1 C+ Y/ j6 q, v
- <bean id="handlerMapping"
1 p [) ]0 S, t - class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"># U) C4 ~" `/ m \
- <property name="mappings">
5 k3 h; Z! P6 f5 V3 e6 T - <props>
% [$ @$ ~+ ~$ L+ v/ F' x - <prop key="welcome.do">welcomeController</prop>* `0 E, a: C+ Y! x+ G
- <prop key="login.do">loginController</prop>4 `. j* ^! v" R8 U
- <prop key="toLogin.do">toLoginController</prop>
0 r) m0 s8 d: l1 g - </props>
$ {/ b) ?3 M# R$ `. |* t6 b; y - </property>
5 C% }( ` X @! W. W2 g& a+ j' T - </bean>
: F6 F& K. @( q) ~* U4 D - <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->
7 V% A1 y1 m( Z - <bean id="viewResolver" " _+ j% e, d& M" i6 }. j
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
+ p% o. ^7 y2 Y; o- ^$ {; M - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>) h8 q! F; A* c3 G8 o0 m
- <!-- 配置视图后缀 -->
% ?! N) D+ [+ x- B$ h2 X B - <property name="suffix" value=".jsp"></property>
! `# J1 z5 X: Y" W k; x - <!-- 配置视图前缀 --> ; o, o9 ?( W/ V5 m4 M: E8 c
- <property name="prefix" value="WEB-INF/jsp/"></property>
% D2 W3 d3 K4 J+ v( q4 K: { B' } - </bean>
, w% n" U! W9 {# o: l5 f - <!-- 定义Controller组件,等价于原来的Action -->
) L- f! p# l! B, I - <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>
& J0 H6 J( u' H$ R/ f - <bean id="loginController" scope="prototype" class="itstyle.action.LoginController">
* x4 i" V$ p# m- f: _4 ] - <property name="commandClass" 0 D2 P1 ? Y4 c5 v* @7 |
- value="itstyle.entity.User">+ u/ L# v. v& _3 c1 i
- </property>
; C2 n4 p+ [: o0 H3 u+ c) `$ h$ g - </bean>- ~( ?0 Z$ n3 W) v7 V- n! B
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>' X3 T2 t9 k- G w4 r
- </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。6 S" |/ @4 F0 ~3 H; P) b( z/ m) N+ i
+ E9 z9 L9 q( C# N. h! H% j4 e2 l
|