8 e- _: b, Z$ j/ H
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处理请求的流程:
2 M. ?8 W3 l: g/ o+ w! h# _! w9 t5 F 四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>
0 F2 P9 a a+ A4 O( n - <web-app version="2.4"
/ p% T; o0 B7 G6 J - xmlns="http://java.sun.com/xml/ns/j2ee"
, M& M8 P4 e; ?( i - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 F6 b# o+ Y- Y
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee e& I! [3 I$ j
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">+ b! b! [+ N( w( v o: E
- <servlet>
0 z1 q2 N3 I7 R m& E* ` - <servlet-name>springmvc</servlet-name>6 e/ r; R0 h' @
- <servlet-class>& W; t/ A: V* C, c7 M; m& G' c
- org.springframework.web.servlet.DispatcherServlet$ {' W1 P% p* D9 {
- </servlet-class> _' Y0 d$ S5 W" y
- <init-param>
9 ?2 |0 N# H" N2 w2 v4 i - <param-name>contextConfigLocation</param-name>$ L7 ?/ Y" h6 V7 e% s" O. u
- <param-value>classpath:applicationContext.xml</param-value>0 ?* k! t$ x& X2 v8 R' ], b
- </init-param>
5 @9 {: A$ u: @2 C* L4 b% U' t6 ]( o1 ] - <load-on-startup>1</load-on-startup>( l; v: y8 \ t; r5 o5 G# u
- </servlet># D$ d4 T9 ?7 b# O
- <servlet-mapping>9 ?- D, e. Z) [& R1 ^* d6 r
- <servlet-name>springmvc</servlet-name>
0 G) H& O: z* i' w- J - <url-pattern>*.do</url-pattern>$ K# y6 x, R% `1 l; n
- </servlet-mapping>
( q: M( i [* i3 X - <filter>
) ` z- Y5 d) c9 ^) Q - <filter-name>CharacterEncodingFilter</filter-name>4 O: w7 m1 x' D! H# k. U& V1 S
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>+ ~4 w, P+ U2 |/ z
- <init-param>
( x8 B7 e k& R: z# o - <param-name>encoding</param-name>5 z4 x( y; h6 h
- <param-value>utf-8</param-value>
) i- |+ r8 ?- f, p6 T! \ - </init-param>( W U# o2 O2 _ X4 }4 c$ i
- </filter>
( F+ D/ W/ Z. x1 f/ I4 {, h$ E; G - <filter-mapping>+ z0 p( m; ^" a, J- `2 Y) Z# [
- <filter-name>CharacterEncodingFilter</filter-name>
3 Z/ ~" w' |2 Q8 C - <url-pattern>/*</url-pattern>1 G9 d- x6 @ j* M) z
- </filter-mapping>4 h4 v! O q8 T) G
- <welcome-file-list>- `9 L0 h# ?1 I
- <welcome-file>index.jsp</welcome-file># t. a2 m s5 J D
- </welcome-file-list>! R0 B8 G! R( ^( Q/ ~/ t0 N' `
- </web-app>
/ n6 z' w! L( ]; W5 g% z/ e
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>5 C X6 H4 Y/ A {/ l: C* P2 [4 H
- <beans xmlns="http://www.springframework.org/schema/beans"
, G( j+ n* l: x - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
' S8 W g7 ?& \2 k - xmlns:tx="http://www.springframework.org/schema/tx" 5 X" r5 H# r$ j7 W3 p6 C5 @9 W
- xmlns:aop="http://www.springframework.org/schema/aop"
% k8 I) U$ i& p4 S: f+ W$ Q% w - xmlns:context="http://www.springframework.org/schema/context" + Y$ f8 C4 D O# c7 e
- xmlns:jee="http://www.springframework.org/schema/jee"
# |) D( M2 e) \: B9 d - xsi:schemaLocation="0 d9 q1 b; d: O p9 }3 d' F) H. w& n
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
4 x/ |6 j/ [4 t9 R - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
6 i k; u4 G+ \0 ?4 X - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd, Y. P3 H5 p; `( j
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
. v1 W! H3 \ ^" `9 n) Z - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
) u" t, t+ e/ F - <!-- 定义映射处理器,指定请求和controller对应关系 -->- f) v4 n" a; S; z( f+ j7 R
- <bean id="handlerMapping" . @3 w9 T0 z# y9 s% R
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">1 [# u" k* g9 {9 g Q
- <property name="mappings">
$ D$ `- @/ g' ?# o6 N3 `/ u2 F - <props>
6 N2 F: b% z4 d2 ~# W- L - <prop key="welcome.do">welcomeController</prop>
0 r# W8 e0 @7 e+ p5 q: p - <prop key="login.do">loginController</prop>
0 `7 s) m' c3 W - <prop key="toLogin.do">toLoginController</prop>6 p0 h0 c) P7 ]) q& j0 @
- </props>
4 F7 _! X; q% ~* s- Z& T0 H - </property>% S3 \ B# l* r# j
- </bean>& R+ G; O, @ ]- d5 J7 a
- <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->
" e0 }& N: ~' } - <bean id="viewResolver"
0 D$ e, T2 ^! N+ Z - class="org.springframework.web.servlet.view.InternalResourceViewResolver">: B A+ p& P2 x# W% O
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
) x: ^& n) K- @; u6 R - <!-- 配置视图后缀 -->
: U+ i& `' s5 T" e8 ~ - <property name="suffix" value=".jsp"></property>! D, @) y# R4 P) b! a* O& _2 b
- <!-- 配置视图前缀 --> + R) f* V q5 Y+ N4 ]) v8 ]
- <property name="prefix" value="WEB-INF/jsp/"></property>
' ~ C5 ^$ W6 h6 g4 n- `. x4 u$ } - </bean>
( _) A0 [; L9 P l4 g e# m. s - <!-- 定义Controller组件,等价于原来的Action -->4 a3 q* `0 ^/ w. k- z7 {2 v* @1 ]- Y
- <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>+ n1 f1 ^3 v" u5 I" c
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController">
8 j+ f1 m% _# _7 x3 P$ R - <property name="commandClass"
# \5 m$ j- ~5 y3 V+ d! \" j5 ] - value="itstyle.entity.User">1 ?' S0 r! ?% F$ W
- </property>! [: \/ i' q: `* K0 c/ M/ a4 T/ n
- </bean>) P* K2 w0 ]5 T# A) H' V9 g6 H, ~
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>- u4 Y' k7 o) Q2 T0 ]
- </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。* o% x4 a* N" }6 g8 C! t/ r: V
- ]6 S& l/ P% l% n
|