一. SpringMVC控制器请求应答

  1. 控制层 controller
  • @RequestMapping请求映射
  • @RequestParam请求参数
  • ModelAndView返回模型和视图
@Controller
@RequestMapping("/student") //分模块
public class StudentController {

private static List<Student> studentList=new ArrayList<Student>();

static{
studentList.add(new Student(1,"岳不群",11));
studentList.add(new Student(2,"平一指",12));
studentList.add(new Student(3,"张翠山",13));
}

@RequestMapping("/list") //请求的是 /student/list.do
public ModelAndView list(){
ModelAndView mav=new ModelAndView();
mav.addObject("studentList", studentList); //设定与前端view交互的参数/对象
mav.setViewName("student/list"); //设定前端页面list.jsp
return mav;
}

@RequestMapping("/preSave") //请求的是 /student/preSave.do
public ModelAndView preSave(@RequestParam(value="id",required=false) String id){
ModelAndView mav=new ModelAndView();
if(id!=null){
mav.addObject("student", studentList.get(Integer.parseInt(id)-1)); //设定与前端view交互的参数/对象
mav.setViewName("student/update"); //设定前端页面update.jsp
}else{
mav.setViewName("student/add"); //设定前端页面add.jsp
}
return mav;
}
}

在spring-mvc.xml配置文件中对web静态资源的根目录进行了设定

<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>

所以视图到资源目录的对应关系如下

视图位置

资源目录

student/list

/WEB-INF/jsp/student/list

student/update

/WEB-INF/jsp/student/update

student/add

/WEB-INF/jsp/student/add

注意在客户端的浏览器中使用的URL

http://localhost:8080/SpringMVCDemo/student/list.do
http://localhost:8080/SpringMVCDemo/student/preSave.do
^
|----->
  1. 视图层 view
  • index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% response.sendRedirect("student/list.do"); %>
  • add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学生添加</title>
</head>
<body>
<form action="student/save.do" method="post">
<table>
<tr>
<th colspan="2">学生添加</th>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
</body>
</html>
  • list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>显示学生信息</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/student/preSave.do">添加学生</a>
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<c:forEach var="student" items="${studentList }">
<tr>
<td>${student.id }</td>
<td>${student.name }</td>
<td>${student.age }</td>
<td><a href="${pageContext.request.contextPath}/student/preSave.do?id=${student.id}">修改</a></td>
</tr>
</c:forEach>
</table>

</body>
</html>

${pageContext.request.contextPath}指的就是/WEB-INF/jsp
推荐使用上边的绝对路径形式。

  • update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>更新学生信息</title>
</head>
<body>
<form action="student/save.do" method="post">
<table>
<tr>
<th colspan="2">学生修改</th>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name" value="${student.name }"/></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" value="${student.age }"/></td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="id" value="${student.id }"/>
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
</body>
</html>

二. SpringMVC的控制器其他应用

  • SpringMVC对象属性自动封装
  • 控制层Controller内部转发和重定向
  1. 控制层代码
@Controller
@RequestMapping("/student")
public class StudentController {
......

@RequestMapping("/save") //访问的url:/student/save.do
// 前端表单的name属性值需要与后端实体bean中的类属性名相同,才能完成对象属性的自动封装。
public String save(Student student){
if(student.getId()!=0){
Student s=studentList.get(student.getId()-1);
s.setName(student.getName());
s.setAge(student.getAge());
}else{
studentList.add(student);
}
// 重定向(不能携带数据,且URL会发生改变。)
// return "redirect:/student/list.do";

// 内部转发(可以在前端页面间传递数据,且URL不会改变。)
return "forward:/student/list.do";
}

@RequestMapping("/delete") //访问的url:/student/delete.do
public String delete(@RequestParam("id") int id){
studentList.remove(id-1);
return "redirect:/student/list.do";
}
}
  1. POST请求乱码问题的解决
    在WEB-INF/web.xml中配置一个过滤器
<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>
</filter>

<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>