|
8 T# q/ f4 g, Y3 O1 {
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处理请求的流程:
" j# J6 f5 d& p 四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>
. e. t- a, ]7 k7 Q* P( {3 c - <web-app version="2.4"
$ d7 v( y4 B8 y8 B1 ]& l - xmlns="http://java.sun.com/xml/ns/j2ee" / z' H f2 Y( z; D/ ^( D
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 W; d3 W$ z+ o2 u' Z - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee % Z/ X4 g3 L5 k4 `7 L
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">0 W& s" o1 ]4 t1 g0 K+ d6 C
- <servlet>
0 k {+ p0 Z5 y! q4 S `0 f - <servlet-name>springmvc</servlet-name>, |5 b! t, \) j0 J8 Y" C: D
- <servlet-class>
2 {6 K- z* j+ ?; k+ [$ l9 I - org.springframework.web.servlet.DispatcherServlet$ u \* l9 [* c4 h+ }
- </servlet-class>
' Q8 ~& Y: U C1 J9 S - <init-param>! A, l- B/ F0 w
- <param-name>contextConfigLocation</param-name>) X$ E: h# ^3 a& Y
- <param-value>classpath:applicationContext.xml</param-value>
& G' A& K: n2 M/ @3 \ - </init-param>9 A8 W! Z3 Q8 [5 t9 P
- <load-on-startup>1</load-on-startup>. u+ r: G1 ~- ], m1 ~1 G7 j: q
- </servlet>
: T+ M; X& S- H - <servlet-mapping># l# S$ [/ q& E5 V, [
- <servlet-name>springmvc</servlet-name>9 V$ u5 N! Q6 ^% G! g* _/ x
- <url-pattern>*.do</url-pattern>6 o2 `; m/ x7 `. ]# s
- </servlet-mapping>- Q0 y+ R3 c! {/ {7 ]' D3 X
- <filter>" ^! l' z/ n+ |! _% F1 X
- <filter-name>CharacterEncodingFilter</filter-name>- K7 a7 y$ r: L7 n# T/ i$ l/ t
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
9 ~% U$ R) k4 S& ~' d+ W: k7 \) T - <init-param>4 i3 l) [; f/ c0 i
- <param-name>encoding</param-name>
. ^ B- z: ]9 j6 ^- {- x2 }+ q2 P - <param-value>utf-8</param-value>" s3 u. y/ D) {- P# Q
- </init-param>
/ U* |$ \2 g5 l, z' x - </filter>
7 w' q' Z4 w2 {8 l - <filter-mapping>$ a. m0 H8 _) Q
- <filter-name>CharacterEncodingFilter</filter-name>
9 z; ^, m C" Y! O. |* d& b0 R+ l6 Z6 c - <url-pattern>/*</url-pattern>/ q1 P$ e( z0 r* K) s
- </filter-mapping>2 w0 ~" [; j, R6 j( G
- <welcome-file-list>' D/ y- U- m! c- X' q5 m
- <welcome-file>index.jsp</welcome-file>* N6 o1 D" b1 O H6 o1 g
- </welcome-file-list>- l" _& B1 S' t- Q$ |2 i! q8 X
- </web-app> l8 N' I! I5 q- m( c
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>
8 [( M, ^% L. A$ U& X! } - <beans xmlns="http://www.springframework.org/schema/beans"
5 y* ?8 w- F- u# ]. r - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"+ {/ \5 |$ ^# q( L2 {
- xmlns:tx="http://www.springframework.org/schema/tx" + @4 W+ b" n7 I8 ?0 y+ d. w! W
- xmlns:aop="http://www.springframework.org/schema/aop"
2 p* K; Y: m! q1 w. N7 U4 W - xmlns:context="http://www.springframework.org/schema/context" 5 U: \- B/ O* ?. ^( H6 a
- xmlns:jee="http://www.springframework.org/schema/jee"* E }8 [/ p# O. h
- xsi:schemaLocation="
1 z3 p$ N1 C2 ~; K) B: m- A - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd$ [1 c- I3 O) W
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd# r1 @& T z) _! j- }$ `
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
; F! l' x1 W$ G, I - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd! T3 d5 X/ H( \1 z. G- B
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">% u0 \. U& X8 |: g
- <!-- 定义映射处理器,指定请求和controller对应关系 -->" j( @ |7 K5 U2 M
- <bean id="handlerMapping"
% w! j. J$ u2 ]5 v1 K# S - class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
: s% |: b" s, A0 k) z) o - <property name="mappings">
) [" N0 I! }3 [ - <props>
( e& a1 |2 |, q. ?8 o - <prop key="welcome.do">welcomeController</prop>
- N. c8 W/ i( |1 Z- i) D% b - <prop key="login.do">loginController</prop>
& a6 w: P- C/ f+ p* o - <prop key="toLogin.do">toLoginController</prop>
( v ]; P! t1 c - </props>4 q% V, B# l* X. n7 s
- </property>
6 y: n" |- h- b - </bean>
8 K; a. ?3 m9 c6 E- `+ A( M- A - <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->: L. R& g8 W4 y) R. x
- <bean id="viewResolver" 8 D; [( u. E$ x B: S5 Z4 b
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">1 c* v6 F9 [7 J3 c/ d0 ]
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
3 d. x% b8 m- D$ {- O3 W - <!-- 配置视图后缀 -->
9 p4 G. W; m/ I: I - <property name="suffix" value=".jsp"></property>/ F* v# k# h, ?7 y- d
- <!-- 配置视图前缀 --> 4 v7 N) O, c: y( O
- <property name="prefix" value="WEB-INF/jsp/"></property>5 O z% d3 M1 G; J/ l
- </bean>; l: D$ J5 _8 j5 p) W
- <!-- 定义Controller组件,等价于原来的Action -->
% k! S# R4 C7 d- H1 J+ V - <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>
. m F+ N7 e) `- U9 O - <bean id="loginController" scope="prototype" class="itstyle.action.LoginController"> : J4 e' W4 @$ }& v/ H- |' h
- <property name="commandClass"
' Y( W: I" H+ d# g6 O0 n - value="itstyle.entity.User">
$ ], y6 B! x: k9 a1 e2 @- P, f - </property># U! H: U: P. ?' l0 e
- </bean>! N1 ]3 X1 z5 ~! P. T/ b: m% e
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>
9 R1 d; H, N6 m8 p ^8 |& L' m - </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。
& W+ }0 i4 F, U2 W/ r$ V. _
0 K( }, N) _& Q* S0 O9 H) M- v& W
|