spring框架是当前java世界中最优秀的框架之一。
1.首先需要收集spring3 mvc所使用的jar包。
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.context.support-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
这里使用的版本是3.05 目前spring的最高版本。
2.创建web工程,配置web.xml好让spring框架可以顺利加载。
<?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">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
创建一个servlet,目的是将所有的*.action请求交由spring来处理.
3.配置一个请求映射文件,spring默认的映射文件名称应该是[servletName]-servlet.xml,所以这里的映射文件为dispatcherServlet-servlet.xml
4.我们需要创建一个请求处理类,test.FirstAction,该类实现一个Controller接口
package test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class FirstAction implements Controller {
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("first controller is working!");
return new ModelAndView("first");
}
}
这里基本上和普通的servlet类似,有request response。
5.接下来我们配置一个用于显示的页面,这里我们只是创建一个jsp,/WEB-INF/jsp/first.jsp
6.配置一下请求转发规则,配置dispatcherServlet-servlet.xml,我们将第一个映射请求为first.action.
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="first.action">firstAction</prop>
</props>
</property>
</bean>
<bean id="firstAction" class="test.FirstAction">
</bean>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
这里SimpleUrlHandlerMapping用于将请求映射到action中。
UrlBaseViewResolver用于映射view的处理,其属性viewClass表示处理方式可以是pdf,xls,jsp。。。这里我们直接解析jsp
prefix、suffixb分别表示视图前缀和后缀
当访问 first.action的时候将转到FirstAction处理并交由/WEB-INF/jsp/first.jsp进行显示。
分享到:
相关推荐
很直观,有具体的代码实现,在细节地方都很详细
基于注解的spring_3.0.x_MVC学习笔记
SpringMVC 已经成为目前最主流的 MVC 框架之一,并且随着 Spring3.0 的发布,全面超越 Struts2,成为最优秀的 MVC 框架。它通过一套注解, 让一个简单的 Java 类成为处理请求的控制器, 而无须实现任何接口。 同时它...
springboot学习笔记 spring基础 Spring概述 Spring的简史 xml配置 注解配置 java配置 Spring概述 Spring的模块 核心容器CoreContainer Spring-Core Spring-Beans ...
mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in ...
简单笔记记录REST应用 ### 1。 使用的技术 Maven的3.0 春天4.1.6.RELEASE HSQLDB 2.3.2 ... 访问http://localhost:8080/spring-mvc-embedded-db-1.0-SNAPSHOT/api/notes ### 6。 其他问题: 您的note-searc
Spring学习笔记...............345 第一章 Spring概述..........................345 第二章 Spring IOC(控制反转)........347 第三章 Spring AOP(面向切面编程)..........351 第四章 Spring中的数据访问.............
Spring学习笔记...............345 第一章 Spring概述..........................345 第二章 Spring IOC(控制反转)........347 第三章 Spring AOP(面向切面编程)..........351 第四章 Spring中的数据访问.............
flex actionscript 开发 对flex mxml技术的掌握 对java数据库后台框架 spring+hibernate ejb 3.0+jpa 等等后台技术
补码= 反码 +1 正数=负数的补码(反码+1) 反码= 非(二进制数) 八进制数,零开头 011(八进制)=9(十进制) 十六进制数,零x开头 0x55(十六进制)=5*16+5(十进制) 类型:数据都必须有类型 boolean (8bit,不定...
XCodeFactory3.0Beta1推出 ! SQL Server 2005 SQL Server 2005的几个新功能 在SQL Server 2005中解决死锁 在 SQL Server 2005 中查询表结构及索引 sql server 2005中的DDL触发器 在 SQL Server 2005 中使用表值函数...