$ O8 N( O$ c- P 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处理请求的流程:
: g+ g' M' G; `9 v# f
四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>
- A- y [0 P d6 y7 n - <web-app version="2.4" 5 W$ E8 j. b3 ~! V
- xmlns="http://java.sun.com/xml/ns/j2ee"
3 Y) [- e0 B6 V/ H5 o/ n) e# u, h - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
0 W! u4 ?$ r% f9 @) J' ^ - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
% u9 | e: k0 M ?! b P - http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">9 C! }1 G( v! o1 E* r
- <servlet>0 c' I0 @$ H7 t% v2 y
- <servlet-name>springmvc</servlet-name>$ c' o+ ]: v& v$ d5 X8 d7 S
- <servlet-class>
( P' D0 ~: P: F7 A# u( b& h - org.springframework.web.servlet.DispatcherServlet
. O' z ?& ` I4 D# Q( F) I - </servlet-class>8 x" t, ?5 ?- C
- <init-param>! ]5 ^( X6 s q5 ]
- <param-name>contextConfigLocation</param-name>
% D- P% l! _8 ` r/ M# x - <param-value>classpath:applicationContext.xml</param-value>0 ^7 m9 x& P6 y9 c9 S7 W
- </init-param>
4 E9 F+ w! {5 p) m3 d( y) ~$ O - <load-on-startup>1</load-on-startup>
- I. r) s5 x; b - </servlet>
9 c9 {( p* s7 } F- [/ g r& Y6 ^ - <servlet-mapping>2 P- A' R9 d, v, @" Z
- <servlet-name>springmvc</servlet-name>" C K7 E1 Y8 Y6 C$ J# C. S
- <url-pattern>*.do</url-pattern>! e: b! m' c) N: H1 k
- </servlet-mapping>3 t' F- A# @9 W2 h
- <filter>
@! }% g2 P+ M5 x- R3 q) R - <filter-name>CharacterEncodingFilter</filter-name>9 q I1 G8 `; O
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>0 S! e- {1 ?; G5 p$ ?: s
- <init-param>7 z2 h! q- R; N( _$ l
- <param-name>encoding</param-name>
0 K7 ^; K' `0 u; P - <param-value>utf-8</param-value>* r$ t w! t. S; B1 e# r4 q% i" e
- </init-param>7 Z4 D0 h! G0 U( n% d8 [2 X |
- </filter>
/ r3 \# u3 D" r% k8 k" O - <filter-mapping>
$ y% {/ L' q; B; f - <filter-name>CharacterEncodingFilter</filter-name>
! ^' _1 ]$ Z7 b. B0 w7 z. ^: \ b' g, }- \ - <url-pattern>/*</url-pattern>
" z% I% G* t m; L/ x - </filter-mapping>3 _# z; J( X3 [' \3 G' ^+ @
- <welcome-file-list>+ h& f6 d$ d$ Y l' Q! H4 z
- <welcome-file>index.jsp</welcome-file>' F4 E5 b- _- X5 f
- </welcome-file-list>5 O1 e0 r3 Q% d; U: f3 z4 V
- </web-app>- S) ]( Y4 l: T! X- @ M8 | A8 g
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>6 w$ O# T7 \3 ~: R
- <beans xmlns="http://www.springframework.org/schema/beans"
: I2 F: \- F+ A0 H) M: M% ]* e6 B - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9 ]0 O7 n+ r$ l8 ?) w9 S, b( x - xmlns:tx="http://www.springframework.org/schema/tx"
- E4 H2 d5 [6 l* n - xmlns:aop="http://www.springframework.org/schema/aop"( t! ?" `: D/ q
- xmlns:context="http://www.springframework.org/schema/context"
: a& \) M* U; G2 W/ b% W& ~ - xmlns:jee="http://www.springframework.org/schema/jee"; ]3 W2 V) ^& x; @7 Y5 `4 e. Z
- xsi:schemaLocation="
' P6 Z# N! y( ?% [' T - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
6 c3 \, n4 ^% V. \* J- o - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- X) C! W( j5 q+ |6 p5 B - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd3 r' C/ v+ ~5 T1 b( A7 c
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd/ {" _6 U$ P# O0 H% r0 J% }' {9 J; _
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
9 Y2 F; B5 I% o l1 G5 m+ L - <!-- 定义映射处理器,指定请求和controller对应关系 -->
n+ X: `3 G% l/ v" e5 Y - <bean id="handlerMapping" 7 b- r- t5 M( {* N
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
1 O$ M6 c$ U+ j3 ]' Q+ H, s( e - <property name="mappings">
7 m3 V4 C, I, r) A; k - <props>
- b& j% X7 t0 ^# ~0 X: l8 F - <prop key="welcome.do">welcomeController</prop>
/ b7 @6 `% T- `- c$ M6 p, W - <prop key="login.do">loginController</prop>
& q ~. `9 i8 v5 Y t - <prop key="toLogin.do">toLoginController</prop>
5 i" U# g, k1 O+ C$ n. _ - </props>& M# K: L j+ t" a7 z- {
- </property>
2 p% u8 n! p% W! h9 q - </bean>6 w9 {5 q6 c. m( T% C0 Y% I# e8 J1 ^
- <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->
) o9 }+ J$ N, Z - <bean id="viewResolver" 4 O2 @1 E z, j# `& J3 s
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">8 p1 ~2 [& I5 w6 o7 ?6 W+ D! K+ i3 ]- {
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
* D8 `0 f! U/ ^1 z$ F - <!-- 配置视图后缀 --> " O6 m. }% D) B, L; u+ }9 | S
- <property name="suffix" value=".jsp"></property>
0 P4 s" n" F7 H. q - <!-- 配置视图前缀 --> 2 A2 H9 \) @( V! I- S5 [/ l$ i
- <property name="prefix" value="WEB-INF/jsp/"></property>& q" z. r% N: W
- </bean>
- M$ d5 F3 e, }$ U0 o- X+ P - <!-- 定义Controller组件,等价于原来的Action -->
; ^) K4 m$ _! Z6 I5 f' E! l7 O - <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>* e0 I( F) s5 M7 ?
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController"> " G7 }7 I+ I3 w8 P
- <property name="commandClass" " `6 B! W' K8 M9 O1 P8 e7 D
- value="itstyle.entity.User">
6 m. T5 s2 T' m" T; k - </property>
$ R- @' J7 @9 n - </bean>0 w8 G2 i* B5 P( R4 H+ V& A, u
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>
3 I' \) S$ R" \, g' I! C0 T - </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。
1 L: }, D6 u+ S$ F( o
& k7 }$ d8 t& v; ^+ g P$ _5 D
|