所需要的包可在本站下载http://download.youkuaiyun.com/detail/fonxian/9079125
在WEB-INF下配置web.xml和spring-servlet.xml(若web.xml中servlet-name
设置为xxx,则配置文件也要改成xxx-servlet.xml,这里需要特别注意),只需要配置这两个文件就好。
1、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 核心控制器 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2、spring-servlet.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 开始spring mvc的注解 -->
<mvc:annotation-driven />
<!-- 配置注解扫描的包路径 -->
<context:component-scan base-package="com" />
<!-- 配置action中返回的视图配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
3、在src文件夹下新建一个controller
@Controller
public class MeetingController {
@RequestMapping(value="/getuser")
public String getName(String userName,Model model){
model.addAttribute("username", userName);
return "index";
}
}
这里的意思是当服务器接收到一个getuser的请求,这个请求会通过dispatch servlet转发到Controller中,Controller对数据进行操作(这里的model.add..是把参数带到要跳转的页面),然后跳转到index.jsp中。
index的前缀和后缀就在spring-servlet.xml中配置的
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
4、然后需要写一个发送请求的界面和一个跳转的界面
发送请求的界面
<%@ 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="getuser" method="post">
<input type="text" name="name" />
<input type="submit" value="tijiao"/>
</form>
</body>
</html>
接收请求的界面index.jsp,这里的username是与model.addAttribute(“username”, userName)对应起来的
<%@ 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>
<p>${username }</p>
</body>
</html>
5、如果需要返回的是一个列表,例如
ArrayList<User> userlist = ......;
model.addAttribute("userlist", userlist);
则界面可以这样写
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<c:forEach items="${userlist}" var="p">
<tr>
<td>${p.id}</td>
<td>${p.name}</td>
</tr>
</c:forEach>
</body>
</html>
6、如果出现错误
Null ModelAndView returned to DispatcherServlet
需要检查一下@RequestMapping(value=”/getuser”)下面有没有加 @ResponseBody,加了这个注解,则返回的是Json串,不能再使用return “index” 这种方式