源码下载:http://download.youkuaiyun.com/detail/u010469432/6786687
先说一下Spring3 MVC的优点:
spring MVC 属于轻量级框架
1、Spring3 MVC的学习难度小于Struts2,Struts2用不上的多余功能太多。呵呵,当然这不是决定因素。
2、Spring3 MVC很容易就可以写出性能优秀的程序,Struts2要处处小心才可以写出性能优秀的程序(指MVC部分)
3、Spring3 MVC的灵活是你无法想像的,Spring的扩展性有口皆碑,Spring3 MVC当然也不会落后,不会因使用了MVC框架而感到有任何的限制。
对于SpringMVC的一些文档型理论知识我不再多加赘述,今天主要是一步一步的开始搭建框架,当然这是我工作中的成果。
不多说,上代码:
1.创建Web project,我使用的是myeclipse。起名为Springmvc(名字自定)
2.引用SpringMVC 需要的核心jar包:
3.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 在项目启动时,加载Src/config下的所有以 spring- 开头的.xml 配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-*.xml</param-value>
</context-param>
<!--项目启动后默认打开的页面 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!--这里的配置是关键,servlet-name -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置加载相应的文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置servlet映射
在访问URL时路径要写为: /service/+ Controller 层@RequestMapping("/admin") 中的admin /+方法上的@RequestMapping(value = "/login")中的 login
例如 登陆表单 提交的action就是 /service/admin/login -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
4.配置Spring.xml文件,此文件的名称必须与web.xml中的servlet-name名称一致。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springfram