( q: \% K5 r7 x! P/ P1 c2 ^0 D
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 N8 Z. H H% p' E
四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>- \$ W+ k. ?) r/ b
- <web-app version="2.4"
+ i. T' k% V" @. ?$ s - xmlns="http://java.sun.com/xml/ns/j2ee" * e% K4 G) k& v
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
0 V! L; J6 y! k - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
+ O# d: p' e" ^- R4 P+ N8 R( F: B+ m, C - http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
, s% @$ q! [- S; N4 e' R8 _ - <servlet>) ~0 b, Z" u6 n5 w, A
- <servlet-name>springmvc</servlet-name>9 H$ X% W5 m' y/ _+ b4 T
- <servlet-class>
' D1 H) J7 r2 S0 ] - org.springframework.web.servlet.DispatcherServlet
, ]' N/ _& G+ ` - </servlet-class>
# S! C* `. j# r$ q" ] - <init-param>: ~* F7 H/ }, e, L% M. Y: M1 o7 N
- <param-name>contextConfigLocation</param-name>* B+ y0 n0 W* @5 B: n
- <param-value>classpath:applicationContext.xml</param-value>4 R, @& |& I9 \! Q3 d
- </init-param>
" ` m! E0 L' }' I: T4 x! s7 ] - <load-on-startup>1</load-on-startup>
4 P8 c. `6 _. k9 i$ N - </servlet>
6 h% e' G- d% ]% q; { i9 ]0 y - <servlet-mapping>
, B* g" Y; m0 q* [7 i( a - <servlet-name>springmvc</servlet-name>
2 e# K6 x8 g9 H$ G8 K: K - <url-pattern>*.do</url-pattern>% p0 [7 S1 ^8 `0 b7 b
- </servlet-mapping>
9 n: S1 `+ l4 `( _3 G; ]* L1 p - <filter># B5 N6 y+ F- F3 ~& b+ m( K
- <filter-name>CharacterEncodingFilter</filter-name>$ k3 e7 C/ F2 Y' s1 q
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
( @! S' U5 A% f7 p. N7 a d4 W - <init-param>1 E* G" L) }7 I: x, W _
- <param-name>encoding</param-name>
1 [# q3 R( d0 N9 o0 _5 }$ f, {2 h; [ - <param-value>utf-8</param-value>. g/ H) N Y) ^& Z) j3 ~. O8 g! ^
- </init-param>. \* p3 B% g3 \3 {6 S+ H: l/ F
- </filter>9 }0 ]+ D& }7 {
- <filter-mapping>
8 l9 j' }) a0 C F! g5 w! g: T - <filter-name>CharacterEncodingFilter</filter-name>
+ D: X& Q2 I+ S( z+ R - <url-pattern>/*</url-pattern>
+ D) f' {. H" l - </filter-mapping>9 G% B6 ]' z8 O! l7 E) x, d
- <welcome-file-list>; d! [9 O% ~, r
- <welcome-file>index.jsp</welcome-file>
$ Z* f0 d' j1 ~$ I/ f7 } - </welcome-file-list>/ d7 h, Z& O, E D" m' p
- </web-app>* H! {+ h/ @5 [# V! ?/ b3 |' O5 ]
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>
5 O' f4 D' [+ t$ a, c0 y - <beans xmlns="http://www.springframework.org/schema/beans"
5 J2 @- U* I2 T- w7 m% `6 c - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7 }; h* N3 @% W - xmlns:tx="http://www.springframework.org/schema/tx"
5 H& f/ z4 U* C/ r - xmlns:aop="http://www.springframework.org/schema/aop"! u+ G$ t4 `9 g8 @3 j
- xmlns:context="http://www.springframework.org/schema/context"
9 t1 r% r3 B) F; R, M; g - xmlns:jee="http://www.springframework.org/schema/jee" R) t8 F2 p! s# x+ c1 f+ A
- xsi:schemaLocation="8 H) X! |. g; W }1 g! ]
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
3 r2 F4 A& j5 ]2 M - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd7 ?( w+ ?' ]" `, |4 q9 F, O. w, \
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" I1 J: Z& X. \" h: g
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd/ f: m1 ~- R# v0 N V- Q& l% T; U
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
7 X. {' g4 p, ^$ Z) _! z - <!-- 定义映射处理器,指定请求和controller对应关系 -->2 K, w) ?" q8 v Y3 y+ _4 D
- <bean id="handlerMapping" 1 {' Y8 F I2 y. |+ C
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">% i$ X8 h9 X7 k& W+ [
- <property name="mappings">
( a T5 Z# x0 p, M8 G - <props>2 D" j8 G. j- C- p5 X5 D: o
- <prop key="welcome.do">welcomeController</prop>
/ F! u9 d5 z: B - <prop key="login.do">loginController</prop>
' b6 s0 A$ k) }, ~! o - <prop key="toLogin.do">toLoginController</prop>
, r) e2 Q7 V$ V q - </props>. q% e. m! J5 Z( O8 j* ]: |9 m
- </property>
# D7 v3 x9 U" r3 s% b - </bean>2 L$ P, G* c, o- _$ b1 f
- <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->) V2 b' w" q2 I9 t9 v# Y A
- <bean id="viewResolver"
$ Q8 d7 Z$ B" `) g/ b1 ]: H' H+ S! u - class="org.springframework.web.servlet.view.InternalResourceViewResolver">
1 R) Z5 F9 W9 v! W - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>1 c m& _8 O( w- Y* _* v
- <!-- 配置视图后缀 -->
1 `* K) c) L2 w9 H- }' w6 y& s& [ - <property name="suffix" value=".jsp"></property>
( R% A( O7 C, o5 a+ m5 A% W$ { - <!-- 配置视图前缀 -->
( I9 x( _- {+ ]8 V; }# m5 H( j - <property name="prefix" value="WEB-INF/jsp/"></property>
* H" t: F% g- g - </bean>8 `) w8 ~, s) b! p8 u
- <!-- 定义Controller组件,等价于原来的Action --># ^* O$ e) n a
- <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>& y u. ~& G) E4 B9 B: i# m
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController">
) W1 C5 E7 t: |( Z3 {- K6 ?6 D' s - <property name="commandClass"
7 H* ^' y$ O+ f, G- F& q+ ^ - value="itstyle.entity.User">& a. Y0 z! p% ?, p6 w2 X! Y
- </property>& M3 X! r0 {6 I
- </bean>
9 J6 |8 T' D* Z" \: \ - <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>
' W: @1 Y& @$ a5 V/ N - </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。' a( W: c! u1 b- {! ] p6 j
0 _* {) |" k$ U& h
|