好记性不如烂笔头。next..
1.工程目录结构
使用java ee新建一个动态web工程
LoginController.java,一个控制器类
web.xml,全局配置文件
spring-servlet.xml,加载的一个配置文件
index.html,tomcat启动测试页面
index.jsp,springMvc返回页面
lib包,springMvc需要的一些jar包
2.jar包
springMvc需要的一些jar包:
网上关于这些jar包的解释,
spring.jar 是包含有完整发布模块的单个jar 包。
org.springframework.aop 包含在应用中使用Spring的AOP特性时所需的类。
org.springframework.asm Spring独立的asm程序, Spring2.5.6的时候需要asmJar 包,3.0开始提供他自己独立的asmJar。
org.springframework.aspects 提供对AspectJ的支持,以便可以方便的将面向方面的功能集成进IDE中,比如Eclipse AJDT。
org.springframework.beans所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。
org.springframework.context.support包含支持缓存Cache(ehcache)、JCA、JMX、
邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。
org.springframework.context为Spring核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类,UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、JasperReports集成的类,以及校验Validation方面的相关类。
org.springframework.core 包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心。
org.springframework.expression Spring表达式语言。org.springframework.instrument.tomcat Spring3.0对Tomcat的连接池的集成。
org.springframework.instrument Spring3.0对服务器的代理接口。
org.springframework.jdbc 包含对Spring对JDBC数据访问进行封装的所有类。
org.springframework.jms 提供了对JMS 1.0.2/1.1的支持类。
org.springframework.orm 包含Spring对DAO特性集进行了扩展,使其支持 iBATIS、JDO、OJB、TopLink,
因为Hibernate已经独立成包了,现在不包含在这个包里了。这个jar文件里大部分的类都要依赖spring-dao.jar里的类,用这个包时你需要同时包含spring-dao.jar包。
org.springframework.oxm Spring 对Object/XMl的映射支持,可以让Java与XML之间来回切换。
org.springframework.test 对Junit等测试框架的简单封装。
org.springframework.transaction为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。
org.springframework.web.portlet SpringMVC的增强。
org.springframework.web.servlet 对J2EE6.0 的Servlet3.0的支持。
org.springframework.web.struts Struts框架支持,可以更方便更容易的集成Struts框架。
org.springframework.web 包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入
WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。
3.web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 请求后缀设为.do -->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 设置编码格式为UTF8 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
</web-app>
3.servlet.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 注册aop功能 -->
<aop:aspectj-autoproxy/>
<!-- 自动扫面com.springmvc目录及其子目录下面所有类文件,自动注入所有带注解的类 -->
<context:component-scan base-package="com.test.*" />
<!-- handleAdapter会根据map中的key-value找到具体的handle,并交由它处理-->
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/login.do">loginController</prop>
</props>
</property>
</bean>
<bean id="loginController" class="com.test.spring.mvc.controller.LoginController">
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4.LoginController.java
package com.test.spring.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController {
@RequestMapping("login.do")
private ModelAndView login(){
ModelAndView view = new ModelAndView("index");
return view;
}
}
代码解释为:所有的.do请求过来后,都交给org.springframework.web.servlet.DispatcherServlet这个servlet来统一调度处理,这个servlet会去找请求后缀对应的处理类,比如说“/login.do”对应的controller为LoginController,那么“login.do”的请求就会给这个controller相应的方法来处理。最后返回的ModelAndView把数据组合到视图中,由org.springframework.web.servlet.view.InternalResourceViewResolver类加上前缀“/”和后缀“.jsp”返回给用户。
4.验证
启动tomcat,浏览器输入:http://localhost:8080/testMvc/login.do
可以看到:
OK,简单实现了springMvc。