! }' |9 j7 G' {% @# f, P
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处理请求的流程:
7 w5 i, }& L) }
四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>
/ O$ u3 H( p: n6 c# \9 l; W - <web-app version="2.4" ! I& K4 E5 o% z2 x; `
- xmlns="http://java.sun.com/xml/ns/j2ee"
: H. r+ j! K- J6 C, j* n1 ~ - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 ?! c1 ^2 A7 n; x4 N) y) F - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 3 J1 x8 S0 ~: X5 W- ?, c9 D
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
/ N; E1 V n0 f f" s! F - <servlet>+ a! y8 K& j5 s/ s
- <servlet-name>springmvc</servlet-name>6 x/ S3 R) _' Y4 n- E7 g7 Z# O* p( W
- <servlet-class>/ J- `: N7 J3 E/ Q
- org.springframework.web.servlet.DispatcherServlet( s" V! O! x% W, ~9 z" m, L
- </servlet-class>
2 \: |/ [. U( y- r' e% q9 Y - <init-param>, Y( \' h- x! I8 M" e7 S( g
- <param-name>contextConfigLocation</param-name> n- ]6 ^1 r/ @- I7 k6 d
- <param-value>classpath:applicationContext.xml</param-value>
0 d3 P" X( }7 g4 y* D - </init-param>" O9 R7 b* ]$ O) ^4 g% h
- <load-on-startup>1</load-on-startup>' O; N- H1 f! m" ~ c
- </servlet>
5 P$ U; |+ `8 u$ B" |0 x- R0 O - <servlet-mapping>
' _3 A! |3 @0 H u2 l - <servlet-name>springmvc</servlet-name>7 T0 B7 C: W- s6 b
- <url-pattern>*.do</url-pattern>" {, U# a6 q, L+ C, [
- </servlet-mapping>
j( { J O( ] - <filter>7 p/ m0 B4 r, K7 J0 ~2 i/ C3 n
- <filter-name>CharacterEncodingFilter</filter-name>
% g* d: F q5 v6 \ - <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
0 ?4 B" }) { |: |# m0 ]* V, ~ - <init-param>
' ^; Q3 r: }: _ - <param-name>encoding</param-name>; y+ a1 c5 }! i& K) Z
- <param-value>utf-8</param-value> q7 J$ V& o9 B2 r+ q! y
- </init-param>7 x4 @ l" S& z% g: ^/ ~0 t
- </filter># b$ S( F, X, c. b d1 ^
- <filter-mapping>
) v( g4 y6 V+ i. H; p" ^ - <filter-name>CharacterEncodingFilter</filter-name>
* f6 Z, }# A5 P - <url-pattern>/*</url-pattern>
: t$ \& z- H! Q } - </filter-mapping>& N( Y& g/ a9 C5 @% R4 z2 n2 F
- <welcome-file-list>, ?4 `* n5 V# j* q/ C$ ?( ^
- <welcome-file>index.jsp</welcome-file>* a: \3 P% D) V( k; w4 r" m2 i
- </welcome-file-list>
5 _1 [( C4 X6 t1 f* v - </web-app>$ b2 n* @& }6 J- \; v& N0 W/ z+ S
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>
; A3 R$ R- t0 s* d+ M. p( M% k - <beans xmlns="http://www.springframework.org/schema/beans"
" @: t& `( b5 e$ y4 B* R - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
: F7 N& z0 `0 K' X - xmlns:tx="http://www.springframework.org/schema/tx"
: c( V& S/ n+ J4 A. D# H. Z/ ? - xmlns:aop="http://www.springframework.org/schema/aop"8 b8 ? Q& J& \6 r. A2 {' i
- xmlns:context="http://www.springframework.org/schema/context"
, W( }9 j- ]6 `& \. n M - xmlns:jee="http://www.springframework.org/schema/jee"
9 x [0 x* O$ I2 Z+ L - xsi:schemaLocation="/ t. \: ~$ M& n3 L# i
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
' c. X+ i. C, P3 H - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd' O; F! K% h# a2 ^ J1 x9 R
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
8 R- c6 q3 y( b$ h& }8 \: x5 P - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd8 [7 \7 c. n% d9 ~ j
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">$ i3 b$ G5 J5 \( O1 k
- <!-- 定义映射处理器,指定请求和controller对应关系 -->5 Z) e! f: i# O: S
- <bean id="handlerMapping"
# r; {$ s$ v; g* ^6 X; R% ~( s5 K, s - class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">$ Z2 t. i7 X% p: d0 K7 \ M
- <property name="mappings">
4 C% E4 a$ }3 y8 S" ^ - <props>$ R% J7 p* o1 z9 l
- <prop key="welcome.do">welcomeController</prop>! Y6 x% A, K0 M; i2 ]( l
- <prop key="login.do">loginController</prop>
$ \6 v `) z; D% ] - <prop key="toLogin.do">toLoginController</prop>
3 l- }' w* j! _5 R - </props>
% H9 I0 M# u; x# M2 G9 V - </property>0 r) F- d9 H' G/ e! y- a4 R
- </bean>1 l: }: g" L# `' o9 t
- <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->
+ U+ J W' F( C9 { - <bean id="viewResolver" 1 w/ s* Z8 W) @( w" k9 d# @7 s7 [
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
5 y, Q" d" s7 t0 @# W' a: y3 U - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>6 z% g1 J ?; M& k' a
- <!-- 配置视图后缀 -->
# D" @# R/ [& i1 q0 v9 s3 y' W* e, L - <property name="suffix" value=".jsp"></property>
# _7 I. F" S& x% d/ A - <!-- 配置视图前缀 -->
! W: m" l. H% \7 b5 F( W- _ - <property name="prefix" value="WEB-INF/jsp/"></property>
# V7 }# w9 F) f7 I - </bean>" a0 ^1 R+ X" E) E8 k7 ]( \
- <!-- 定义Controller组件,等价于原来的Action -->
$ q, J9 X8 T( O& Y - <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>' R# B2 g8 C4 S
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController">
( K! l8 _2 H9 P8 c2 O: ~ - <property name="commandClass"
6 k$ X8 M% j% A6 A( L- G2 Y( u5 G - value="itstyle.entity.User">
: v: k1 x7 v6 ]% E! Y - </property>
! P X/ y* F }9 g8 | - </bean>/ g2 {1 n* F2 R, @ ~# J
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean> [8 q% U1 U: B5 \) n
- </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。
$ |1 n* ?& W. A0 E" V, N
) W" E! d$ }, D- n' C* M
|