Springmvc初识

1.SpringMVC与Struts2

0 SpringMVC

基于web的应用系统,特别是状态管理、工作流以及验证都是需要解决的重要功能。http协议的无状态性决定了这些功能都不容易实现。spring 的web框架就是为帮助你解决这方面而设计的。

使用spring,你可以让它的web框架自动将传递进来的请求参数填充到你的模型对象中,同时提供验证和错误处理。你可以依赖这个框架,让它帮你管理用户在web表单中创建的对象的状态。

除了这些功能外,会发现整个框架都是模块化的。每个组件都有自己的特定任务,完全与框架中的其他部分没有耦合。这让你能够用插件的方式进行web应用程序的前台开发。

1.1 struts2

Struts2也是比较优秀的MVC构架,优点非常多比如良好的结构。但这里想说的是缺点,Struts2由于采用了值栈、OGNL表达式、struts2标签库等,会导致应用的性能下降。Struts2的多层拦截器、多实例action性能都很好。
可以参考我写的一篇关于Spring MVC与Struts2与Servlet比较的文章

1.2 springmvc的优点

  1. Spring3 MVC的学习难度小于Struts2,Struts2用不上的多余功能太多。呵呵,当然这不是决定因素。
  2. Spring3 MVC很容易就可以写出性能优秀的程序,Struts2要处处小心才可以写出性能优秀的程序(指MVC部分)
  3. Spring3 MVC的灵活是你无法想像的,Spring的扩展性有口皆碑,Spring3 MVC当然也不会落后,不会因使用了MVC框架而感到有任何的限制。

1.3 SpringMVC:Model-View-Control

框架性质的C 层要完成的主要工作:封装web 请求为一个数据对象、调用业务逻辑层来处理数据对象、
返回处理数据结果及相应的视图给用户。

Spring C 层框架的核心是 DispatcherServlet,它的作用是将请求分发给不同的后端处理器,也即 使用了一种被称为Front Controller 的模式(后面对此模式有简要说明)。 Spring 的C 层框架使用了后端控制器来、映射处理器和视图解析器来共同完成C 层框架的主要工作。并且spring 的C 层框架还真正地把业务层处理的数据结果和相应的视图拼成一个对象,即我们后面会经常用到的ModelAndView 对象。

2 入门

2.1 搭建环境

在spring 的官方API 文档中,给出所有包的作用概述,现列举常用的包及相关作用:
org.springframework.aop-3.0.5.RELEASE.jar :与Aop 编程相关的包
org.springframework.beans-3.0.5.RELEASE.jar :提供了简捷操作bean 的接口
org.springframework.context-3.0.5.RELEASE.jar :构建在beans 包基础上,用来处理资源文件及国际化。
org.springframework.core-3.0.5.RELEASE.jar :spring 核心包
org.springframework.web-3.0.5.RELEASE.jar :web 核心包,提供了web 层接口
spring-web-4.1.3.RELEASE.jar :web 层的一个具体实现包,DispatcherServlet也位于此包中。

后文全部在spring3.0 版本中进行,为了方便,建议在搭建环境中导入spring3.0 的所有jar 包(所
有jar 包位于dist 目录下)。

2.3 编写web.xml配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>spmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

简要说明 :DispatcherServlet 就是一个Servlet ,也是对请求进行转发的核心Servlet 。在这里即所有.do 的请求将首先被DispatcherServlet 处理,而DispatcherServlet 它要作的工作就是对请求进行分发(也即是说把请求转发给具体的Controller )。可以简单地认为,它就是一个总控处理器,但事实上它除了具备总控处理理器对请求进行分发的能力外,还与spring 的IOC 容器完全集成在一起,从而可以更好地使用spring 的其它功能。在这里还需留意 < servlet-name > spmvc </ servlet-name > ,下面步骤三会用到。

2.4 建立springmvc的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!-- 加载属性文件 -->
<context:property-placeholder location="classpath:resource.properties"/>

<!-- 配置扫描 器 -->
<context:component-scan base-package="cn.itcast.core.web.controller"/>

<!-- 配置处理器映射器 适配器 -->
<mvc:annotation-driven/>

<!-- 配置视图解释器 jsp -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

<!-- 配置静态资源-->
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/fonts/" mapping="/fonts/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/jsp/" mapping="/jsp/**"/>
</beans>
article.share