|
& A% y' p0 ~4 z, z6 i
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处理请求的流程:
" c* ^' h5 C L* f 四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>$ }6 O2 @; W$ S9 W8 B% L
- <web-app version="2.4" ( O+ [% S& P3 U. } R
- xmlns="http://java.sun.com/xml/ns/j2ee" 0 ]. Z% U! h( d2 f
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 _ ? L2 W7 R( W* _" F& z, B - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 9 P ~. X& s# B
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
2 G6 m7 O a4 X - <servlet>
/ W3 R5 y7 c* M' \ - <servlet-name>springmvc</servlet-name>" [2 Z/ H$ P! _+ D# C
- <servlet-class>2 V: V0 i1 O l( ^, k9 j' |
- org.springframework.web.servlet.DispatcherServlet, D u: l; ?* h
- </servlet-class>3 Y5 T4 {" N' k& B8 w. \
- <init-param>
/ ^# C+ \& ?- P( _& K" l5 J* S - <param-name>contextConfigLocation</param-name>( P" }7 G3 u' Z4 l0 B# @# `
- <param-value>classpath:applicationContext.xml</param-value>3 i) n6 E. p& Q3 q0 e- ?
- </init-param>/ L" _% Y7 D7 j1 V2 _7 E
- <load-on-startup>1</load-on-startup>/ R3 x4 X l" D0 ]3 O
- </servlet>
7 C3 r* v8 A0 U/ ? - <servlet-mapping>- G9 Z" {& F6 m3 h. k
- <servlet-name>springmvc</servlet-name>! w. p6 d* S( h# o! a( Y
- <url-pattern>*.do</url-pattern>* }4 K( m! Z c6 ] Z
- </servlet-mapping>5 K/ |! k% V! P; d M# b$ u
- <filter>
2 P; D$ w7 |' R; h+ v6 R3 u - <filter-name>CharacterEncodingFilter</filter-name>3 O# D( r1 o$ a8 g* Z7 D+ H
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
3 B3 i* r5 H$ n - <init-param>
7 u3 l' n+ [# `. u - <param-name>encoding</param-name>
% r6 \ n! g' i3 P - <param-value>utf-8</param-value>
: ]* T. e4 G! n4 g7 J7 r# ]! G' k - </init-param>5 {- @$ G% o: l* K) ^2 Y q* V; ^
- </filter>
6 u' ?' G" r% }. y - <filter-mapping>8 Z; _' w1 y# |$ \
- <filter-name>CharacterEncodingFilter</filter-name>
" Q: ~# p; o% N0 q9 U5 ^ - <url-pattern>/*</url-pattern>
2 i2 Q* M6 }0 D ~2 g/ h! ^! a - </filter-mapping>
6 x# Y8 Y4 c1 e* l* ? - <welcome-file-list>! _/ g0 @* V% y
- <welcome-file>index.jsp</welcome-file>
7 ^# v& L- z6 w( \! y/ s - </welcome-file-list>! w$ ?* u; {1 Z) [$ s3 K8 q
- </web-app>3 P: x; }- C c0 S- `
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>/ r+ `7 B, M5 @. {
- <beans xmlns="http://www.springframework.org/schema/beans" - U6 ^7 |" I+ [# }% n2 V
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
% F. p v; A( s( D - xmlns:tx="http://www.springframework.org/schema/tx"
0 z$ N( B% W/ k - xmlns:aop="http://www.springframework.org/schema/aop": X1 R( ` ? O+ V
- xmlns:context="http://www.springframework.org/schema/context"
+ ^: w ]5 X+ r6 ?% n" l - xmlns:jee="http://www.springframework.org/schema/jee"
& A' F0 J- Z8 I" n' X" K: X - xsi:schemaLocation=". O3 J. G4 K0 z# V7 l. T( s! r9 s
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
$ {3 T# {! k) U4 y1 y8 D8 f - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd) {6 t# V; q0 z: G, N
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd q5 i- T9 L" J% d3 s
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
4 H, Y! d ]. k9 B4 x - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">1 `$ ^( B) D. L% M) H
- <!-- 定义映射处理器,指定请求和controller对应关系 -->. d7 `; b, V2 `: p. h+ t2 e5 {+ z& r
- <bean id="handlerMapping" 4 D3 Z. u! q- e
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">3 D# C: ]& m4 H7 Z- D
- <property name="mappings">$ I0 P" p; `" s$ E2 U; D( t* D
- <props>! P2 @+ f# v8 q. ^, N4 }) l
- <prop key="welcome.do">welcomeController</prop>0 A! G( E. _# E+ R& g5 d
- <prop key="login.do">loginController</prop>; I$ k( Q7 m9 n( ]: L" C0 Y
- <prop key="toLogin.do">toLoginController</prop>+ K9 W8 b; C6 D; h
- </props>; |8 |( M' H3 o4 A3 y0 h2 p
- </property>' @6 k" x" B5 Z2 q, @, S- X8 A, M, J
- </bean>! t3 U1 u( n3 E X" V" z6 \5 q! Q
- <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->
) R. g/ p8 @* s; M7 K3 l1 D8 T9 k |( U - <bean id="viewResolver" 1 j& n1 S9 Z$ |# _" g6 ` r6 E0 A
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
" ~* q! J$ v9 J! ]& j4 r7 C - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
& L. W" @5 D- }& H6 Y8 o% E4 R/ o - <!-- 配置视图后缀 --> 5 H; M* a" X' h. d& s5 M, G0 o5 p
- <property name="suffix" value=".jsp"></property>
3 W( m5 m R( @ T - <!-- 配置视图前缀 -->
" r7 K+ v, X2 e. V - <property name="prefix" value="WEB-INF/jsp/"></property>
4 n$ {9 o1 d8 f' P) W4 H: b7 g c - </bean>) d4 M; D- }/ g0 Y$ q6 V
- <!-- 定义Controller组件,等价于原来的Action -->
/ a6 D/ Y, S( }# d2 m0 g8 p - <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>5 z& o3 \1 g% W+ K. _
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController"> ' w. Z* I7 g0 a
- <property name="commandClass" ( ?3 G- }' e( h r; [
- value="itstyle.entity.User">
6 W' h4 M: v4 S# A - </property>
; E6 C- W' m7 V - </bean>
& l) w4 ~0 h5 h- {7 W - <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>
7 p8 r$ p! }0 |; h4 b - </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。3 g6 }1 ~, m/ t& D
9 u( F! V, r6 z$ ~1 w6 N X
|