一、SpringMVC对象属性自动封装
接着02的内容
add.java
<%@ 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>
StudentController.java
package com.java.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.java.model.Student;
//Controller说明是控制器
//分模块,请求的时候要加student在前面
@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));
}
//ModelAndView:返回模型和视图
//模型:数据的封装,试图:返回到哪个jsp
//请求的时候:student.list
@RequestMapping("/list")
private ModelAndView list(){
ModelAndView mv = new ModelAndView();
mv.addObject("studentList", studentList);//把数据封装起来
mv.setViewName("student/list");//返回到哪个jsp
return mv;
}
//RequestParam这样写就可以把学生id的值自动注入到String id
//因为添加和修改都是用preSave这个方法,但是添加是没有id传过来的
//required=false意思是,这个id不用也可以,用也可以,这样不用的时候就添加,用的时候就修改
@RequestMapping("/preSave")
public ModelAndView preSave(@RequestParam(value="id",required=false) String id){
ModelAndView mav=new ModelAndView();
//如果id=null就是跳到添加页面,不为空就获取该条数据返回到修改页面,id-1=索引。索引从零开始
if(id!=null){
mav.addObject("student", studentList.get(Integer.parseInt(id)-1));
mav.setViewName("student/update");
}else{
mav.setViewName("student/add");
}
return mav;
}
//数据自动封装成student对象
//在添加学生页面下面两条数据
//<input type="text" name="name"/>对应model类的学生name
//<input type="text" name="age"/>对应model类的学生age
//这样数据就能自动封装成对象
@RequestMapping("/save")
public String save(Student student){
//如果id不等于零就只执行更新
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";
}
}
结果
然后调试发现我们输入的数据自动封装成student对象
但是我们发现存入的数据是乱码,这是因为spring对中文还不支持,所以要在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>SpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 拦截请求交给Spring处理 -->
<!-- DispatcherServlet负责请求的分发 -->
<!-- 拦截所有的.do方法,可以自定义 -->
<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>
<!-- 过滤器,解决中文乱码问题,所以以后开发都要配置CharacterEncodingFilter -->
<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>
结果解决了对象属性自动封装
模拟修改(没有数据库,只是模拟,是有问题的)年龄改为22
最后模拟删除
list.java
<%@ 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>
StudentController.java
package com.java.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.java.model.Student;
//Controller说明是控制器
//分模块,请求的时候要加student在前面
@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));
}
//ModelAndView:返回模型和视图
//模型:数据的封装,试图:返回到哪个jsp
//请求的时候:student.list
@RequestMapping("/list")
private ModelAndView list(){
ModelAndView mv = new ModelAndView();
mv.addObject("studentList", studentList);//把数据封装起来
mv.setViewName("student/list");//返回到哪个jsp
return mv;
}
//RequestParam这样写就可以把学生id的值自动注入到String id
//因为添加和修改都是用preSave这个方法,但是添加是没有id传过来的
//required=false意思是,这个id不用也可以,用也可以,这样不用的时候就添加,用的时候就修改
@RequestMapping("/preSave")
public ModelAndView preSave(@RequestParam(value="id",required=false) String id){
ModelAndView mav=new ModelAndView();
//如果id=null就是跳到添加页面,不为空就获取该条数据返回到修改页面,id-1=索引。索引从零开始
if(id!=null){
mav.addObject("student", studentList.get(Integer.parseInt(id)-1));
mav.setViewName("student/update");
}else{
mav.setViewName("student/add");
}
return mav;
}
//数据自动封装成student对象
//在添加学生页面下面两条数据
//<input type="text" name="name"/>对应model类的学生name
//<input type="text" name="age"/>对应model类的学生age
//这样数据就能自动封装成对象
@RequestMapping("/save")
public String save(Student student){
//如果id不等于零就只执行更新
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";
//转发
//return "forward:/student/list.do";
}
//删除需要id,所以要注入@RequestParam("id")给int id
@RequestMapping("/delete")
public String delete(@RequestParam("id") int id){
studentList.remove(id-1);
return "redirect:/student/list.do";
}
}