所用的工具为eclipse for javaEE,tomcat 8.0
1.导入jar包
把以上的jar包全部复制到项目的WebContent/WEB-INF/lib目录中
2.在webContent/WEB-INF下新增或编辑web.xml
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:jsp="http://java.sun.com/xml/ns/javaee/jsp"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>hadoopWeb</display-name> <servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
这里只拦截.do后缀的请求,spring-servlet.xml放在classpath(右键build path/configure bulid path/source里可以看到哪几个文件夹,也可以在libraries下新增)下就行
3.在java Resource/src下新增或编辑spring-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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="action"></context:component-scan> <bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean> </beans>
这里springMVC在拦截到.do后缀的请求后会扫描 scr/action 下标记了@Controller注解的类,处理完后会请求 webContent/ 下的后缀为".jsp"的文件
3.进行测试,顺便记录SpringMVC怎么传值
在webContent下新建jsp文件index.jsp进行传值,完整内容为:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="print.do">
<input type="text" name="s" ></input>
<input type="submit"></input>
</form>
</body>
</html>
在src/action下新建class文件print.java,完整内容为:
package action; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class PrintAction { @RequestMapping(value="print.do")
public String print(String s,Model model){ System.out.println(s);
s+=".badck";
model.addAttribute("s2",s);
return "print";
}
}
在webContent下新建jsp文件print.jsp,完整内容为:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<title>Insert title here</title>
</head>
<body>
<%
String s=(String)request.getAttribute("s2");
out.println("输入的内容为:"+s);
%>
</body>
</html>
index.jsp请求print.do,被pringMVC拦截到,扫描action文件夹,发现print.java被标注为Controller,并发现它的方法print(String s,Model model)被注释了@RequestMapping(value="print.do"),开始运行print方法.
参数s是要传入的值,在index.jsp和print的方法的参数两边的名称要一致,会自动将index.jsp 的请求中的名为"s"的值传到参数中的"s",
在print.java中处理完后,通过model.addAttribute("s2",s); 语句把s的值传给下一个文件,由于最后return "print";,所以springMVC会到webContent下找名为"print",后缀为".jsp"的文件
在print.jsp上接收的时候用String s=(String)request.getAttribute("s2");语句接受s的值.
效果:
在index.jsp输入3并提交
跳转到print.jsp,显示3.badck
并在后台输出了3,我运行了两次,所以有两个3
最后print.jsp输出带了".badck"并在后台有输出说明print.java确实运行到了