直接开整
(1)创建一个web项目(老程序员忽略)
(2)搭建spring支持(老程序员忽略)
(3)编写springMVC-servlet.xml配置文件
(4)写一个测试类
(5)配置web.xml
(6)使用index.jsp测试(老程序员忽略)
具体步骤
(1)创建一个web项目
(2)添加spring支持
版本是spring3.0;包有前四个+web
finish..
(3)编写springMVC-servlet.xml配置文件
复制一份applicationContext.xml,重命名springMVC-servlet.xml
如图:
编写springMVC-servlet.xml:(1)引入tx,p,context。(2)引入自动扫包标签
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/p
http://www.springframework.org/schema/p/spring-p.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- springMVC自动扫包标签 -->
<context:component-scan base-package="control"/>
</beans>
把它放到WEB-INF文件夹下
最终的文件结构:
(4)写一个测试类
路径是control包下
package control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Test {
@RequestMapping("1.spring")
public void test1() {
System.out.println("进来了!");
}
}
两个注解controller,requestMapping("路由")
(5)配置web.xml
主要是配置springMVC的转发类
<?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">
<display-name></display-name>
<!-- springMVC的转发类 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.spring</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
url-pattern标签表示只有后缀名是*.spring才会被springMVC转发
(6).使用index.jsp测试(老程序员忽略)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<a href="1.spring">注册</a>
</body>
</html>
测试成功!