9 T* L7 r5 l1 D3 K% ]4 b) a1 Y
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处理请求的流程:
4 u1 D/ E$ w( x1 P4 ]: P, ^ 四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>
7 ]1 S/ Y' u( E T6 G# { - <web-app version="2.4"
9 ?# F- Q$ F3 b1 I) o# O1 @9 u - xmlns="http://java.sun.com/xml/ns/j2ee" , J5 j% w" J' r. j# D+ x5 G
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
! O1 B- U5 K. Q+ L- K* K - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
# _3 S, c9 t7 k* \. z, n - http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
9 }& m4 x9 D. _1 D! c& N: u$ Q - <servlet>
. ~. `& } s( o& d6 a8 Q - <servlet-name>springmvc</servlet-name>
$ I. F0 [8 U9 [: J! z: u* R) L" P, E - <servlet-class>
/ r6 R( V" N- z* [# N# T5 r - org.springframework.web.servlet.DispatcherServlet
0 y2 x* _1 M8 v- @5 u - </servlet-class>3 T, ?5 d6 p8 B1 t1 ?4 o# @' [1 ?
- <init-param>" r8 c0 q% E) d" M
- <param-name>contextConfigLocation</param-name>
4 p7 b* B* E& B& d, | - <param-value>classpath:applicationContext.xml</param-value>
; |& r* z- u+ B. q3 J" S - </init-param>
7 h9 V& n- V1 a; K, M - <load-on-startup>1</load-on-startup>
9 Q" s+ Z) a6 A0 n% R0 H' u - </servlet>
3 ^ j- |2 U9 O. j6 @% {0 a! E6 e1 [9 E - <servlet-mapping>
/ p/ P6 V$ A3 Y8 J. t4 Q - <servlet-name>springmvc</servlet-name>9 s$ l2 u1 e$ @7 Z8 D4 Z- l
- <url-pattern>*.do</url-pattern>+ K: E( \6 W& B/ n" V
- </servlet-mapping>
! z# _: j, [* i1 F" h3 U; ~ - <filter>
5 n& r) N0 d2 ~' w" r: \3 } - <filter-name>CharacterEncodingFilter</filter-name>
0 h" Q5 U) ]. ]! ^/ ^ - <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>: v* }# b! o% j
- <init-param>) T3 o' b. s, d" a
- <param-name>encoding</param-name>
' V/ ~3 Y1 i# e- |0 p/ a - <param-value>utf-8</param-value>
2 f; }' F" T! {+ G - </init-param>
9 G! w6 ^$ v- n0 g6 n" n - </filter>
+ a: h" y& B- W9 r Q; S - <filter-mapping>
( E! y3 t& o4 I/ Y8 i - <filter-name>CharacterEncodingFilter</filter-name>4 J6 x+ B% e5 q/ ?$ ^- _" O- D
- <url-pattern>/*</url-pattern>
- j& `5 z; w* M# V* v# e - </filter-mapping>
4 f( ~$ \2 u+ U2 f' Y! Z8 k, ` - <welcome-file-list>
5 j/ b. F3 P% G& i - <welcome-file>index.jsp</welcome-file>
& K' H" f- E/ @" h - </welcome-file-list>6 I% a, O! A3 w z& Q
- </web-app>
9 ~( H. R1 \) ^
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>$ E4 ?$ j* R0 J& t$ w6 B
- <beans xmlns="http://www.springframework.org/schema/beans"
0 c6 o+ z8 D4 F! V0 I - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
/ r' y3 K( ~ n! g3 B - xmlns:tx="http://www.springframework.org/schema/tx"
% G2 D3 F3 f' T - xmlns:aop="http://www.springframework.org/schema/aop" b# X5 W4 o% j3 `
- xmlns:context="http://www.springframework.org/schema/context" / Z& r' Z9 H' c( l+ ^) b
- xmlns:jee="http://www.springframework.org/schema/jee"! E9 f' }$ ?" h; C% s* e
- xsi:schemaLocation="$ j% b. L" Q$ k7 j, }" _: _ N9 E4 P
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd4 b% [8 e2 v/ L4 v/ o
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd- c/ [3 ?$ s6 I, _) X7 f- ]
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" P) P4 b# \) {) `) I0 r: t
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd; g7 ~% K/ e' q% ?6 \
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
9 u! O' {! }6 O" y( U7 h - <!-- 定义映射处理器,指定请求和controller对应关系 -->1 F. U% C/ a I# h; B
- <bean id="handlerMapping" ! ~! X2 A) @. c: C$ r
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
7 r1 U+ k' X* P5 O$ _ - <property name="mappings">. v& ?2 k6 y8 g
- <props>7 N* p+ T7 |; l1 g5 j3 i
- <prop key="welcome.do">welcomeController</prop>
. n. ~+ a6 \) p# { - <prop key="login.do">loginController</prop>
+ }1 h- s# \, x3 Q - <prop key="toLogin.do">toLoginController</prop>
3 y7 k0 R( c, h8 p4 W - </props>3 c% B- r) F0 p3 h D+ X
- </property>
1 D/ o) @4 i) Q& X" v- V - </bean>
( R, }& u. x' Q$ U: M) m - <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->8 b! X$ y6 Q6 e3 {+ P" X" c5 P r! }
- <bean id="viewResolver"
2 Q2 \( y* W7 C& r' H - class="org.springframework.web.servlet.view.InternalResourceViewResolver">! \+ T( C% \& Y$ q- x y, }
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
; m+ h3 e- {) y1 P) v c - <!-- 配置视图后缀 -->
/ B9 X# [) \8 o( J - <property name="suffix" value=".jsp"></property>
' F; s9 N! w2 q) ~$ Z. f' o( O - <!-- 配置视图前缀 --> % {% O' L! n5 `
- <property name="prefix" value="WEB-INF/jsp/"></property>
# L4 N- s0 j, L$ O; a - </bean>2 H' A+ o' d2 |4 R9 V. w; i
- <!-- 定义Controller组件,等价于原来的Action -->
! I$ Q0 A3 a6 }8 h% M2 d - <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>
: R d( @$ r# A3 ?( ?8 B - <bean id="loginController" scope="prototype" class="itstyle.action.LoginController"> ' ]5 ~9 _2 [, c& y& b5 N/ W3 N
- <property name="commandClass"
# h( w3 q! h2 }% `8 D/ N% W& [ - value="itstyle.entity.User">
8 o8 O( q4 r/ c) ]. s0 K% e - </property>
/ |! v9 q# P; |2 G# D - </bean>; K" f/ E% \% e) p% F1 n3 q$ I
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>
- ]+ I( \. W( }3 o - </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。
% f. h3 a+ }5 A- y! K
2 ?. D; X6 m/ I' J
|