' H1 A I' k7 |( J' B% V, h
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处理请求的流程:
3 D( x$ A. A8 M; o) W 四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>; |/ ~& y$ V# @
- <web-app version="2.4" ) R6 B. p% M+ F! X: y, F# k) J }# I
- xmlns="http://java.sun.com/xml/ns/j2ee"
5 B. i' m7 p4 J* l - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" % J2 y! N* T" ^. X- h
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 8 B* B. d5 {3 ?* O% {$ w6 O9 c
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">% @8 z0 S6 e( S, ~
- <servlet>6 a- Y% f1 v5 I3 G# Z- y! z
- <servlet-name>springmvc</servlet-name>
_( l- f" N! e* A/ L - <servlet-class>/ M5 N9 N+ z$ A% |/ z
- org.springframework.web.servlet.DispatcherServlet2 X7 O& I, o- f: ^( _& L' V* R
- </servlet-class>
3 o4 X# R9 _3 Y* i+ E - <init-param>
! m; m- @: U9 i) v& }5 Q& r - <param-name>contextConfigLocation</param-name>
" W8 @" S7 O9 w - <param-value>classpath:applicationContext.xml</param-value>
. \& W! B% Q) L9 x - </init-param>8 R9 ] R' T( @4 g5 z8 q
- <load-on-startup>1</load-on-startup>( }4 x: J# v# E! r. k
- </servlet>1 z9 p. |& u+ o* R( i! }) C
- <servlet-mapping>1 k2 R4 B1 x) }5 C0 k/ g- S
- <servlet-name>springmvc</servlet-name>
9 `* b) a% G; c. S# h - <url-pattern>*.do</url-pattern>7 b1 z+ O7 ~* g5 A. i; ], @
- </servlet-mapping>
# s: H8 g d1 E3 N( C( J - <filter>
, J1 ^+ n" o; c: ~1 } - <filter-name>CharacterEncodingFilter</filter-name>
. j- w8 N, l( O4 [" G - <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
! @3 A& \; K) V& u2 Q- V$ x - <init-param>) |8 }! w4 b7 d: h9 j
- <param-name>encoding</param-name># Q% h' `# |& s" }9 b# H: c0 O& x# \- W
- <param-value>utf-8</param-value>
% ^" U" D# Y4 k0 @8 [/ ^* O - </init-param>8 a4 Z. r# F* |
- </filter>
: Y/ f l) @* G! {% `! V* m - <filter-mapping>+ Q2 Y# F3 `3 G) D! j. Z) b
- <filter-name>CharacterEncodingFilter</filter-name>! G$ j6 ^. k% i
- <url-pattern>/*</url-pattern>) C: }: z3 Y; B r k7 Z( Y
- </filter-mapping>
; d4 t$ N! u. W# i$ C. o7 | - <welcome-file-list>
7 d; {' r4 A+ U - <welcome-file>index.jsp</welcome-file>0 E9 d1 l! A; y$ L! I
- </welcome-file-list>" d& L, M" r; @/ x+ }
- </web-app>" U' g/ g, P8 [* h7 {* X$ u9 U
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>: n" h7 L5 V/ e) R* y
- <beans xmlns="http://www.springframework.org/schema/beans"
1 W# ~- T4 A# e( e+ x6 w - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"8 X$ ? X/ G2 a0 N
- xmlns:tx="http://www.springframework.org/schema/tx" $ }+ C' Q( M0 f! c) F! l7 M: e, v' q
- xmlns:aop="http://www.springframework.org/schema/aop", z, G- ~& q6 L6 |7 X. m( N% W
- xmlns:context="http://www.springframework.org/schema/context" . ?+ O2 \& G$ v% D
- xmlns:jee="http://www.springframework.org/schema/jee"
1 w( D% t$ F) X& v+ D0 N, s5 U - xsi:schemaLocation="
2 }" K& _( S2 P1 `. e# o - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
8 ]0 H b- D" _ - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd9 N6 E; z' o; i* |
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
) |8 u& b1 {! ?7 u - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd0 P' L/ N" e6 c5 A6 @2 v
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
! }% ^0 V) m) A$ `' Z9 V7 r- f - <!-- 定义映射处理器,指定请求和controller对应关系 -->
. F- E+ [6 ]0 M4 t: m3 \7 j - <bean id="handlerMapping"
. x V/ f. \/ V* \/ f& N - class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">. L. Q; g0 D9 h2 F5 q
- <property name="mappings">
. r; l$ o+ ]( c0 p* g, n - <props>- S' `7 \4 `' ? _% X/ m8 [
- <prop key="welcome.do">welcomeController</prop>
( ^- k8 O9 k x6 U2 s9 | - <prop key="login.do">loginController</prop>
8 D! S0 s2 s. m* X( \ - <prop key="toLogin.do">toLoginController</prop>
9 I0 F3 D- `( [6 h - </props>
+ _ P& _% Y% C: R+ C - </property>
( D) q/ u/ Z+ |. _$ u - </bean>6 q( k* _2 K" P! M- @3 {5 i" Z
- <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 --> f# x, d6 O$ C/ k* {5 e
- <bean id="viewResolver" , [/ \+ g( v1 H" o1 O
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
+ ]; [! X k# a: V7 Y) Q: |# n - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>1 C4 ^2 [9 D* J# ~9 e* z: Z
- <!-- 配置视图后缀 --> - U# z Z5 h' n; ]3 Y, o
- <property name="suffix" value=".jsp"></property>
( W) I: J& d, l2 N3 C - <!-- 配置视图前缀 --> 3 \: y1 f* p2 ]2 n
- <property name="prefix" value="WEB-INF/jsp/"></property>4 e# v0 C j- C7 D9 K( M
- </bean>+ k, o% b+ l' {" G3 F
- <!-- 定义Controller组件,等价于原来的Action -->
' ?: c9 r/ J# M, a# S - <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>
" K! N, k6 K9 d1 ?8 w$ J - <bean id="loginController" scope="prototype" class="itstyle.action.LoginController"> % m. l6 B D; X s6 F/ ~, S/ {; t0 z9 f
- <property name="commandClass" " T% U0 M; n+ n9 F5 f4 i9 i4 e
- value="itstyle.entity.User">
5 r7 L) H5 e: _* R" s - </property> E' }0 c/ W; x" _. ]9 H
- </bean>/ Z& i/ a" E+ p- l
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>: X, D3 q8 S, A$ w9 N$ ~) @
- </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。" d9 _) p" O9 Q; e t+ ?
8 d; s8 J1 j: e4 D
|