* o( d* f" c G5 w
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处理请求的流程:
# b9 w L) g: ?9 f. ^ 四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>
( J O7 C8 X4 { L% | - <web-app version="2.4" ( ^( l# C5 z! f o
- xmlns="http://java.sun.com/xml/ns/j2ee" 1 c& ^2 ^9 @0 M3 @
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" " `0 C9 T2 L( U- S a# c
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
/ U1 j& K: S) q# z. a - http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
: ^- g' n$ i: T6 Q4 ^- j - <servlet>5 }( H/ |3 d9 D; t3 Y9 K4 I3 ]
- <servlet-name>springmvc</servlet-name>- e/ B- t3 P. i3 ?$ r& G) z- V& _, X
- <servlet-class>
. A' [$ F. c- y" P - org.springframework.web.servlet.DispatcherServlet
% W8 c% m0 t4 E7 y - </servlet-class>% O: j* J% Q- N7 H/ E
- <init-param>5 k$ D. i0 p* @) d" B) V4 R# Q
- <param-name>contextConfigLocation</param-name>) i. {, N# e. `( B3 I) x
- <param-value>classpath:applicationContext.xml</param-value>
$ s- ?6 {5 ?+ V5 Z6 m - </init-param>, [! ~+ M) ~& {/ D" Q
- <load-on-startup>1</load-on-startup>' B% Q4 R: Q! S+ N& @9 X* J' o
- </servlet>
7 G% V5 |1 ]7 x6 ~( H - <servlet-mapping>1 v3 `5 f3 o" ^2 U/ [( I) d" F7 c
- <servlet-name>springmvc</servlet-name>
$ q [; F1 O4 } Y( a - <url-pattern>*.do</url-pattern>
) v' c3 F! t L! M- U - </servlet-mapping>; u$ v. y- r8 O3 p0 t: ^( R
- <filter>
5 a. p! L U) W% K - <filter-name>CharacterEncodingFilter</filter-name>3 N# \& K/ a. c! u/ z! L
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>6 b4 V1 U8 G$ K0 |" x# i }
- <init-param>1 A8 K! @! S+ W6 _1 p
- <param-name>encoding</param-name>$ L+ h* j, h% v9 h! z p, W
- <param-value>utf-8</param-value>, K1 T" G+ K, j6 y! l7 } W
- </init-param>1 ^; i0 R/ D/ P3 f; X1 o& D5 \
- </filter>0 q. N0 \! c9 o. c% N5 f8 S" i' J
- <filter-mapping>) B+ B! b; E. `0 f7 i7 w0 d
- <filter-name>CharacterEncodingFilter</filter-name>
) w1 Q; a7 }0 ?7 L$ A - <url-pattern>/*</url-pattern>
n2 R! W+ p$ Y- f0 V - </filter-mapping>
/ {9 `9 U5 P5 y/ w - <welcome-file-list>
y9 }& U9 ~7 y ^$ I0 q, G9 T: I" V - <welcome-file>index.jsp</welcome-file>
# o+ g$ r; ]# F - </welcome-file-list>
I7 N, ^, o+ \$ ^+ V. U- L - </web-app>
: {4 d4 ^1 G8 U
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>
& _- U( R8 n2 H( D" _1 P, T - <beans xmlns="http://www.springframework.org/schema/beans"
, j5 b3 @9 B3 J ?; x - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
. q( i! u0 d" q; K9 O6 F( O/ ~ - xmlns:tx="http://www.springframework.org/schema/tx"
' [1 e6 j6 u8 N2 M - xmlns:aop="http://www.springframework.org/schema/aop"9 b& G; {$ W1 h- W$ P$ T5 _" O
- xmlns:context="http://www.springframework.org/schema/context"
* |4 M Z2 B4 _; H+ @ - xmlns:jee="http://www.springframework.org/schema/jee"' M$ ~# v- z8 t9 z/ U& A' S) y
- xsi:schemaLocation="- g! M u2 X/ ]) K& G
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd9 D) g; J9 W x/ W
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd) C8 @" [$ B r+ e4 i! v4 N
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
5 C7 f( g( `9 x' c5 \ - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
" V2 W( N4 B H( e) t( [ - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">& m' I% z# P0 ]! ?
- <!-- 定义映射处理器,指定请求和controller对应关系 -->' y# O& a8 _( F4 v2 s
- <bean id="handlerMapping" * h) V! J4 {2 |/ w
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
& U5 R: \! h! N! p" Q9 r* S+ ] - <property name="mappings">/ }. S% Z2 Y: \# H- T: p
- <props>. x! B9 @2 c0 d2 [) L& g" v2 e+ ~# H
- <prop key="welcome.do">welcomeController</prop>
& ]. I# z6 v. L) m* e - <prop key="login.do">loginController</prop>
9 e1 }( W1 e* R - <prop key="toLogin.do">toLoginController</prop>. V: N {6 C; Q4 P3 j/ }
- </props>8 e# p! }% E& _% h. K# `/ ~8 f2 U
- </property>
0 r/ U2 i& p" e/ X/ b) @; K - </bean>$ N& P% [& P4 [, x8 w% C; p5 b
- <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->1 b! E8 z1 h7 o
- <bean id="viewResolver" 1 e, Z/ w h2 z8 r q0 t% k
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">" A4 D& a) C% Z
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>! l) c& U9 C6 G2 y8 k- E
- <!-- 配置视图后缀 --> 7 d" X& `8 Q4 s
- <property name="suffix" value=".jsp"></property>! M; C( n, H/ `/ `
- <!-- 配置视图前缀 --> ) {" o- c* h" b# D
- <property name="prefix" value="WEB-INF/jsp/"></property>
# z+ O& Q1 I5 |& H% z" b - </bean>
1 A3 q+ n# d3 @$ A: ]* H% `$ D/ Q - <!-- 定义Controller组件,等价于原来的Action -->
$ m o7 W0 z8 c) Y - <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>2 }% ?$ p* A% t5 i
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController">
: O |# p5 B' r* b - <property name="commandClass" 6 `: m& m9 Y2 S+ K, }% O1 m
- value="itstyle.entity.User">
& {) `/ t% L; r; R6 H; B3 t( v9 U - </property>7 w+ o+ X& q0 {4 G
- </bean>7 w( l. I8 w6 B# j% q) S
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>
0 H7 N$ \' u7 p1 r2 A1 m - </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。4 Z5 p* C5 O1 E; O
+ N) `& b) q: N7 R9 n
|