& ~1 r) b: A) v9 I' t
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处理请求的流程:
. a; Q/ c3 |: j# a
四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>1 ^5 V) J* M* Y$ L! n) h
- <web-app version="2.4" : q% U2 s3 }* V) E
- xmlns="http://java.sun.com/xml/ns/j2ee" 0 k* }( N! n" g
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 L6 {( `5 C# h; O4 T& b
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
8 O9 m/ m4 {7 N. t& p - http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
5 I; j. ^; S3 L% J4 T" I( R - <servlet>( C# ^ M+ u9 B& [ |6 Z1 E
- <servlet-name>springmvc</servlet-name>& D& L m# d5 e. h0 x3 K8 r8 c: t
- <servlet-class>+ f! N7 [3 Z' z+ k4 j& [/ ~
- org.springframework.web.servlet.DispatcherServlet2 O; Y& _7 j# q* K2 M
- </servlet-class>/ J" P3 E4 Y4 q* A) O1 T
- <init-param>
# y: q: i+ P' W - <param-name>contextConfigLocation</param-name>
; ?7 d. P7 `/ A - <param-value>classpath:applicationContext.xml</param-value>3 c- j2 |, P3 l Z' e
- </init-param>- @ ^& W1 L# o% W. m: J
- <load-on-startup>1</load-on-startup>( m4 t/ q3 i, H: k
- </servlet>- V* R9 l6 R z% i8 a( |& ~
- <servlet-mapping>+ A( \/ s2 F6 H: |
- <servlet-name>springmvc</servlet-name>6 ]' y* ~. O& S$ a. j7 J
- <url-pattern>*.do</url-pattern>
* k& Y% e( u" Q5 B - </servlet-mapping>
* K; h4 Y3 Q c- a( A - <filter>
+ g/ J' Z) h: M1 j' ~6 ^4 d - <filter-name>CharacterEncodingFilter</filter-name>
9 h/ O" j3 K& N# a! p p1 O - <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>+ ^8 K& C2 k0 ]) F$ [
- <init-param>
- T( y x, n, ~1 F# S% C! Y* @ - <param-name>encoding</param-name> k: b; g3 j f7 v7 W$ L" g
- <param-value>utf-8</param-value>) I% J, u3 Z/ g0 v7 W3 Z" a$ S3 |9 Z
- </init-param>! }- L6 n* Z7 o, U" m$ X7 l
- </filter>
( d( z: \! k* E: }3 ^: S' Q% b; K% R - <filter-mapping>' Q! s& \- ^4 l7 [
- <filter-name>CharacterEncodingFilter</filter-name>" h9 ?6 i# N q/ B
- <url-pattern>/*</url-pattern>
1 b ~8 Y- d" z3 O - </filter-mapping>' ^) m( f3 m1 w+ o, ?! E$ P. ^
- <welcome-file-list>* y) d0 O% i' H& T3 d9 z
- <welcome-file>index.jsp</welcome-file>
u0 J' y: w% f5 o) a& B2 U+ W - </welcome-file-list>
2 t' P7 j! o& ^) v' V - </web-app># [: f4 {/ P0 P( f
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>
2 K' E" z0 i3 h# [. ]2 F% T1 A2 | - <beans xmlns="http://www.springframework.org/schema/beans" 7 c! L6 E' q: H
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"7 A- c/ P( x) C& ^: _" e$ ?
- xmlns:tx="http://www.springframework.org/schema/tx"
( _( e5 K7 `1 E - xmlns:aop="http://www.springframework.org/schema/aop". ?$ r- H( s& u1 n
- xmlns:context="http://www.springframework.org/schema/context" - Z$ m# w1 O! q4 F( y
- xmlns:jee="http://www.springframework.org/schema/jee"0 O. S8 i2 k; d
- xsi:schemaLocation="
: `. K$ ~( w: Z2 u - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
" ~$ \0 |. g) t; ]% N' K. h - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd+ ? b3 u7 ^& ^0 A% l [1 C' \
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+ }" i" x: Z2 D' N - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
/ u$ O- h, @8 ~5 I3 h) r - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">, x% B8 \: y m+ O, i' E5 R
- <!-- 定义映射处理器,指定请求和controller对应关系 -->4 J+ n) l0 ^8 S3 U
- <bean id="handlerMapping"
+ C6 B3 d7 g P$ N, w- w9 i- H - class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">7 n/ I" K% [ P& V
- <property name="mappings">
. ]. W. u, N% _. a4 X- H - <props>
2 k) ?; X6 g$ ^5 Z4 {1 N - <prop key="welcome.do">welcomeController</prop>
3 G8 l; F+ u I. |8 Y - <prop key="login.do">loginController</prop>
. |$ j% h5 ~1 Z( p7 M# t - <prop key="toLogin.do">toLoginController</prop>
' S# [1 x8 S4 U4 u7 c4 A8 P - </props>( ~ ?. x: [% |3 E" B4 E' ~
- </property>
4 a1 n3 p, w3 ?% @: O) p - </bean>' m @8 _4 T3 v+ d5 l
- <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->* N4 n+ Q5 s, p- s. F5 {
- <bean id="viewResolver"
5 O' b1 H* ~8 n2 [8 F, V - class="org.springframework.web.servlet.view.InternalResourceViewResolver">
$ A! ~4 ~; I3 O: n. N. x - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>2 s* V; |/ k' F
- <!-- 配置视图后缀 -->
7 _9 n A, X9 y6 @3 j# {5 y - <property name="suffix" value=".jsp"></property>5 ]. h, @1 J9 c7 z( {
- <!-- 配置视图前缀 --> . B1 y$ }! x+ ]9 L, v" |1 T: Z
- <property name="prefix" value="WEB-INF/jsp/"></property>, E4 P- y" q& C7 X$ D( Y/ ]# @
- </bean>
n& k+ H0 h, z* J - <!-- 定义Controller组件,等价于原来的Action -->5 v" t2 z1 r+ Y3 {6 K, j# O3 h0 P- c# u
- <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>
( ^/ {" M- [4 @ - <bean id="loginController" scope="prototype" class="itstyle.action.LoginController">
/ R- l V! z, ~+ r; x+ V7 s - <property name="commandClass"
3 H6 T9 ^6 |- G- |. v' U - value="itstyle.entity.User">
" h0 i( `9 w1 @) p - </property>2 u' t" w& _% y3 t8 g
- </bean># W& c# z6 L7 n5 H
- <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>
1 q& m* I$ g( N/ N; y7 j: E: u - </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。
7 u/ N$ U, h0 }! M
, Y/ F+ A% f9 ]) ?; |! F/ R* r$ V
|