J2EE系列之SpringMVC学习笔记(三)--SpringMVC控制器

来源:互联网 发布:网络运维 编辑:程序博客网 时间:2024/06/01 10:00

上一篇的博客的工程中完成了学生查询功能。这一篇博客完成学生的添加,修改和删除功能。

一、工程里面学生添加和学生修改页面当点击提交时都会发生/student/save.do请求,现在写这个请求的处理方法:

@RequestMapping("/save")public String save(Student student){studentList.add(student);return "redirect:/student/list.do";}
无论是添加还是修改,前台都会传一个Student类的对象过来,这里采用了和Struts2类似的处理方式,采用对象的自动封装。学生信息添加到studentList数组之后,采用重定向的方式返回"redirect:/student/list.do",这样的话系统会发起/student/list.do请求,项目跳转到list.jsp页面查看添加后的所有学生信息。

重新看一下add.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="${pageContext.request.contextPath}/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>
这里把/student/save.do的请求地址使用了绝对路径。这里重点强调的是,要想实现自动封装,表格中学生姓名,年龄的<input>标签中的name属性要和Student类中的对应属性的名字相同,这点要切记。


二、执行代码,点击添加学生:

输入以上学生信息,点击提交按钮。在save()方法中设置断点:

可以看到这里student对象中确实自动封装了对象。但是里面封装的汉字却是乱码。继续运行程序,跳转到list.jsp页面:

可以看到这里确实已经有了新的学生信息,但是姓名是乱码。

3.上面有乱码问题,这个是SpringMvc处理的不好的地方。为了解决这个乱码问题,SpringMVC给我们提供了一种过滤器,需要在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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>SpringMVC01</display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>     <servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param>  </servlet>  <servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern>  </servlet-mapping>    <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>  </web-app>
这里使用<filter>标签定义了一个过滤器,把请求数据变成utf-8编码。下面<filter-mapping>标签标签对于所有的*.do请求。两个标签合起来的意义就是:把所有的*.do的请求转变成utf-8编码。

以后使用SpringMvc进行程序开发都要添加这个过滤器。重新运行程序,乱码问题解决:


4.修改一下update.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="${pageContext.request.contextPath}/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>

这里对于/student/save.do请求也改成了绝对路径。点击提交时会发出/student/save.do请求,这里使用隐藏域的方式把学生的id发送给后台。这样的话,就会把整个学生想信息进行自动封装。


5.通过id是否为0,判断当前是添加学生还是修改学生。修改save()方法为:

@RequestMapping("/save")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);}return "redirect:/student/list.do";}

运行程序,可以看到学生信息修改成功。

6.删除学生功能

这里在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 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><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>  <a href="${pageContext.request.contextPath}/student/delete.do?id=${student.id}">删除</a></td></tr></c:forEach></table></body></html>

点击删除的时候,发送/student/delete.do请求,并把要删除的学生的id参数带到后台。

添加响应删除请求的方法为:

@RequestMapping("/delete")public String delete(@RequestParam("id") int id){studentList.remove(id-1);return "redirect:/student/list.do";}
删除成功后,发起/student/list.do请求,重定向到list.jsp页面。


运行程序:

点击删除:

7.上面的页面跳转使用的是重定向方式,这种方式是不能带数据过去的。如果要带数据过去,可以使用转发进行页面跳转。测试一下,修改save()方法为:

@RequestMapping("/save")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);}return "forward:/student/list.do";}


这里使用转发的方式进行页面的跳转。

运行程序,添加一个新的学生:

可以看到这里也添加成功。需要注意的一点是转发的方式的话,浏览器地址是不会变的。使用重定向的话浏览器地址会发生变化:


总结一下:这一篇博客通过实例主要讲了三方面内容

1.SpringMVC 对象属性自动封装;

2.SpringMVC POST 请求乱码解决;

3.Controller 内部转发和重定向

阅读全文
0 0
原创粉丝点击