+ O2 s/ F( ^( E2 K3 d1 V ]
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处理请求的流程:
1 e) a/ n7 X5 D 四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>$ s7 k& P" O1 V% q
- <web-app version="2.4" - Q! K. @1 I/ y: I& _* `7 o; L
- xmlns="http://java.sun.com/xml/ns/j2ee"
' e3 n, Y0 g* j - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 y3 }; p+ M0 E) y( b - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
9 k& O d, \8 x* Q8 t F ~ - http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
w( c. I/ R+ Y% A$ |2 t5 R - <servlet>
) q9 o, T4 ?3 m$ l5 X! a: m - <servlet-name>springmvc</servlet-name>/ P+ o+ I. K5 c& z! z* n5 K
- <servlet-class>8 `" @# d% L/ }$ t7 V$ e0 a
- org.springframework.web.servlet.DispatcherServlet7 m" a' D3 T/ k
- </servlet-class>
. J2 [) k% M( A# @ - <init-param>' P7 w2 V& [" q& ?. m# _ k* G
- <param-name>contextConfigLocation</param-name>
2 M" M% h- H2 u4 I( P7 l# A' u7 P - <param-value>classpath:applicationContext.xml</param-value>
' E% \$ ~5 k* d6 O. l( [: n9 U - </init-param>
. d2 R3 |3 N$ H+ M - <load-on-startup>1</load-on-startup>4 w/ \" z) u% X' S( f! R
- </servlet>
2 R. d: m% q7 @4 g! } - <servlet-mapping>4 [7 a" q' [# Y1 O9 u
- <servlet-name>springmvc</servlet-name>
0 \9 `& I( o! X, i8 p' a( L - <url-pattern>*.do</url-pattern>8 P3 L4 @: {9 W1 i
- </servlet-mapping>
4 b: |; s+ o# U& k; U* o - <filter>
" Q8 O3 O% {+ x. V/ E6 E* n - <filter-name>CharacterEncodingFilter</filter-name>
6 q4 p: c, k0 c: h; V - <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>1 m# Y8 ?' \( _& r/ e1 ], c
- <init-param>- a* c$ d( F; _3 }0 E* i1 Z' I+ F7 v
- <param-name>encoding</param-name>
* N& B& U2 {( X: c4 ? - <param-value>utf-8</param-value>7 N. u* r* n6 ^5 Z6 t I
- </init-param>. v* E0 ]4 D7 q/ a1 | j
- </filter>6 Q: X& z5 k0 m6 l
- <filter-mapping>2 r; u; t6 _, g9 B8 e" L6 e
- <filter-name>CharacterEncodingFilter</filter-name>
8 e4 R% p, y5 r2 K& y; E - <url-pattern>/*</url-pattern>+ h9 r4 @, f9 \! ^
- </filter-mapping>9 k' g! e; ^$ r. B/ a
- <welcome-file-list>
7 o% x2 X; s2 i6 E7 y) e& D' R9 v - <welcome-file>index.jsp</welcome-file>0 U1 @0 e$ o3 J0 c; F+ C# h. X
- </welcome-file-list>: g- d, r( [. G6 ]
- </web-app>7 P, {# C) P+ B ?' B: m
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>- X0 I V' [4 v: V& ~
- <beans xmlns="http://www.springframework.org/schema/beans" . |1 S7 G! w/ C! F0 G
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"& C: s/ a# k8 A% `$ v
- xmlns:tx="http://www.springframework.org/schema/tx"
" s( t q% U, J) }2 V - xmlns:aop="http://www.springframework.org/schema/aop"
" Z0 {- _- W: x - xmlns:context="http://www.springframework.org/schema/context" 3 `% o8 @ F4 Y
- xmlns:jee="http://www.springframework.org/schema/jee"
8 M1 }5 v( |. ~ - xsi:schemaLocation="
4 A* |* j: }4 R - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd3 Q2 C6 |3 H& I! `" I
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
/ h. g1 X) [ b$ {3 K9 u - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
/ J6 J X. {9 x) ~ - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd7 @% g2 c4 v2 t/ P x
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
6 M0 P5 G! I" A8 } - <!-- 定义映射处理器,指定请求和controller对应关系 -->
1 _! D/ D+ I1 @6 n0 k - <bean id="handlerMapping" 8 X* C3 j" n2 X- Z9 f( |
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">( \9 {5 `& a9 p. i {: v$ q
- <property name="mappings">
% I" z; {- m% H - <props>
/ \+ H s& h+ z: V% z# v - <prop key="welcome.do">welcomeController</prop>. n# _9 V3 V2 d D2 u$ a2 X5 m3 _
- <prop key="login.do">loginController</prop> }! f; B6 j- N0 L& B0 Y
- <prop key="toLogin.do">toLoginController</prop>
+ _( a1 m0 ^: w8 E; \) D) h - </props>
4 b1 [8 u; n9 c7 p - </property>
& s& z' M% B- P a x, A - </bean>% L8 H1 ~6 K$ f1 {% ]# J. P
- <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->
+ K. h5 ~- R& ~" W+ R1 ] - <bean id="viewResolver"
3 O. F" c# B' {% a8 M - class="org.springframework.web.servlet.view.InternalResourceViewResolver">
. @$ L( ], i. p6 C - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
( S# [& ^# B s$ f/ ` - <!-- 配置视图后缀 -->
9 r) q' `2 H% I5 l( z/ T - <property name="suffix" value=".jsp"></property>& ~0 ^7 m' k$ u j) K; ^! }
- <!-- 配置视图前缀 --> 4 y: z9 g0 T% V
- <property name="prefix" value="WEB-INF/jsp/"></property>
( [$ r! _; d# Y - </bean>
2 b# T9 S5 o% f S" e2 h# v - <!-- 定义Controller组件,等价于原来的Action -->" d% ~8 M5 j! N k8 ]
- <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>
6 k/ F! T, W* T - <bean id="loginController" scope="prototype" class="itstyle.action.LoginController"> & e% U% m8 Z3 }* R
- <property name="commandClass" : h' M7 P0 }) Z% Y7 v; x# q
- value="itstyle.entity.User">2 x- s# ~, Z6 r; x2 h% L/ y
- </property>
$ T! a# _/ p3 K% Y5 u - </bean> u& z( ^+ g: H
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>
( q7 {6 M4 a; O - </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。& S4 D- ?' J: e% L) b0 F
7 ^# h) k1 b9 p- G
|