0 m, A- r3 \0 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处理请求的流程:
& G/ J# E" ?0 K0 y" C( B& z
四:项目案例 web.xml配置: - <?xml version="1.0" encoding="UTF-8"?>7 R7 y1 m. b" Z7 K7 x: }! w# R
- <web-app version="2.4"
' e. x8 ~8 g, H( \7 j - xmlns="http://java.sun.com/xml/ns/j2ee"
. j7 f6 A% x4 ^# }5 p6 p! Y" K - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9 ~6 ]% J$ F& Z5 U8 |; q - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
) ]$ V0 h9 U1 M$ R" u - http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">$ V9 B( ?, o* F5 J) D& [" j7 d
- <servlet> X: q% T5 L/ X# X% b. Z
- <servlet-name>springmvc</servlet-name>
- b+ s4 i7 N% t8 } - <servlet-class>
2 j7 J- D" h. Y S. i* U' L - org.springframework.web.servlet.DispatcherServlet- |; y- W: n1 h8 d1 Q& O9 A
- </servlet-class>
: D0 w6 Y, t* S$ | - <init-param>8 j8 x, l! p5 N: g: @( [
- <param-name>contextConfigLocation</param-name>4 u4 {$ t: g, W9 n
- <param-value>classpath:applicationContext.xml</param-value> E+ F; }* h f9 ~( m
- </init-param>
6 G6 A7 |; x4 f j# G& J - <load-on-startup>1</load-on-startup>
0 j3 k6 O( [ y4 j) ?" i - </servlet>
% T0 H: r1 X/ w- \6 b- @ - <servlet-mapping>
9 E* l% J4 y9 @, @: B; j - <servlet-name>springmvc</servlet-name>
, }3 _* j( b- G - <url-pattern>*.do</url-pattern>; X4 w, G( A3 @! r( |) ^
- </servlet-mapping>' L6 }' | A& x) X/ F2 N
- <filter># \: c+ Z5 s. @7 i& F
- <filter-name>CharacterEncodingFilter</filter-name>
a! B# e6 q8 u+ a" O3 B' S/ \ - <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>' O! s( s/ B( n, d ~" f2 \
- <init-param>
" p6 |2 ~" p( M6 m# a - <param-name>encoding</param-name> S- ~3 H& k& {, l- \: ^
- <param-value>utf-8</param-value>
$ d. @4 u$ P9 X2 } - </init-param>8 A Y1 R9 G( w; A
- </filter>
, r8 T% r2 A' _& p# x9 Q6 } - <filter-mapping>: z) N; x* d( W' O
- <filter-name>CharacterEncodingFilter</filter-name>' \2 P9 }! N" n2 j2 {) r f8 e
- <url-pattern>/*</url-pattern>
/ x) R& i+ D% B - </filter-mapping>- n+ D5 E v/ y) V( X% R
- <welcome-file-list>" w( K+ [1 z ] J. V7 V
- <welcome-file>index.jsp</welcome-file>
0 @: y0 U6 m( J8 H) l - </welcome-file-list>
; A3 C( P& j% J+ T - </web-app>/ u: P+ X2 D Z4 a4 [" \! f
复制代码 applicationContext.xml配置:- <?xml version="1.0" encoding="UTF-8"?>
' z- j; x" R7 g4 J3 b! I - <beans xmlns="http://www.springframework.org/schema/beans"
: c6 P$ N9 W* _- U/ q6 T) R - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"5 u/ z: K- p$ |
- xmlns:tx="http://www.springframework.org/schema/tx" / ?' u, C; w2 f' a0 m& Y
- xmlns:aop="http://www.springframework.org/schema/aop"! D. Z2 @+ M2 D
- xmlns:context="http://www.springframework.org/schema/context" + i. b1 r1 n$ ]0 G
- xmlns:jee="http://www.springframework.org/schema/jee"
/ _0 o1 U5 f" y0 T0 p - xsi:schemaLocation="
4 f* T2 f, c6 {. C9 J { - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd' x4 z( f: R- B7 V& D# u
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
; u$ U# Q u7 b% E. D - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd2 g) g; g( h, |$ h
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd2 y6 @% a8 ] z6 R( k! {; ^0 r
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
2 n" V6 r' w- Y) O - <!-- 定义映射处理器,指定请求和controller对应关系 -->, I) A- T q" j/ E, A6 z* s* M# }
- <bean id="handlerMapping"
1 i! N9 F) N5 q; t1 a" x - class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
9 ~; E2 |5 H, A" M# S - <property name="mappings">* `4 M& Q U8 v6 Q' f
- <props>7 G9 w+ o% O y
- <prop key="welcome.do">welcomeController</prop>
% M" D5 c! E* F, H/ _( g q - <prop key="login.do">loginController</prop>+ O# |0 z* k& y9 q( m V, E
- <prop key="toLogin.do">toLoginController</prop>) i8 \& u( a' C6 W" k
- </props>
& o% v+ Z9 s- C2 q& x. I# e - </property>! q+ {/ a: T! q! W6 }# Z
- </bean>
/ {( X9 ]/ H- X o# l' x - <!-- 定义视图解析器,根据ModelAndView信息定位视图组件 -->, E6 p) F0 x/ X7 _, p* X B
- <bean id="viewResolver"
6 l' c1 ^6 G0 q6 w" U4 b - class="org.springframework.web.servlet.view.InternalResourceViewResolver">8 D0 B& r+ R7 a" d2 Y5 j
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>' M- Q: y8 D% e% s
- <!-- 配置视图后缀 --> $ S" @$ L! R; Z5 a! \
- <property name="suffix" value=".jsp"></property>, S9 w7 K' t) c
- <!-- 配置视图前缀 --> # D5 K. K) {5 O# N" F- C ~
- <property name="prefix" value="WEB-INF/jsp/"></property>) y' \' K }9 f+ l# \/ r: b- U3 v. A+ `
- </bean>
5 F8 @. `4 ?+ C4 s1 y/ z' ~ - <!-- 定义Controller组件,等价于原来的Action -->
& p5 o b6 |# i) _2 [- o( D5 O - <bean id="welcomeController" scope="prototype" class="itstyle.action.WelcomeController"></bean>2 E$ k. c( ^! |7 {% |
- <bean id="loginController" scope="prototype" class="itstyle.action.LoginController">
* R3 R; @) e8 B, [ - <property name="commandClass"
7 {7 a+ [, L( h, s/ z/ I - value="itstyle.entity.User">
5 a. H: a/ p# s3 S. m - </property>5 ^6 Y% ]& ]7 G
- </bean>
" W( ] ?$ _. m2 ]5 B6 k) Q - <bean id="toLoginController" scope="prototype" class="itstyle.action.ToLoginController"></bean>6 b9 r. X7 F5 F- X) v% y2 _: C7 _2 h, `
- </beans>
复制代码 项目测试通过 包含所有的jar包和配置文件 导入即可。* U% |) Y/ @; t( U0 f: M8 _
) i% s- H$ r. ~, A I, E/ C4 Y
|