第二章 Spring MVC 增改查实例
程序员文章站
2022-04-07 21:17:53
...
本章是个小示例,使用spring mvc 模拟student对象的增删改查
一、spring mvc 注解:
1、@Controller 声明控制器
2、@RequestMapping 请求映射/请求路径
3、@RequestParam 请求参数
4、ModelAndView 返回模型和视图
二、spring 中文编码过滤器,添加在web.xml;
三、jsp 遍历HashMap中的对象,并取对象中的属性
四、ModelAndView返回模型和视图:
五、SpringMVC对象属性自动封装
六、内部重定向与转发:
项目源码:
web.xml
spring-mvc.xml
Student.java
StudentController.java
list.jsp
add.jsp
update.jsp
访问路径:
http://localhost:8080/SpringMvc02/student/list.do
一、spring mvc 注解:
1、@Controller 声明控制器
@Controller public class StudentController { //.... }
2、@RequestMapping 请求映射/请求路径
@RequestMapping("/student") public class StudentController { //访问路径,项目路径/student @RequestMapping("/list") public ModelAndView list() { //访问路径,项目路径/student/list } }
3、@RequestParam 请求参数
@RequestMapping("/preSave") public ModelAndView preSave(@RequestParam(value = "id", required = false) String id){ //参数id } @RequestMapping("/save") public ModelAndView save(Student student){ //默认缺省参数,可传对象 }
4、ModelAndView 返回模型和视图
@RequestMapping("/delete") public ModelAndView delete(Student student) { studentMap.remove(student.getId()); ModelAndView mav = new ModelAndView(); mav.addObject("studentList", studentMap); mav.setViewName("student/list"); return mav; }
二、spring 中文编码过滤器,添加在web.xml;
<filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
三、jsp 遍历HashMap中的对象,并取对象中的属性
<c:forEach var="student" items="${studentList }"> <tr> <td>${student.value.id }</td> <td>${student.value.name }</td> <td>${student.value.age }</td> <td> <a href="${pageContext.request.contextPath}/student/preSave.do?id=${student.value.id}">修改</a> <a href="${pageContext.request.contextPath}/student/delete.do?id=${student.value.id}">删除</a> </td> </tr> </c:forEach>
四、ModelAndView返回模型和视图:
/** * 跳转修改或新增页面 * * @param id * @return */ @RequestMapping("/preSave") public ModelAndView preSave( @RequestParam(value = "id", required = false) String id) { ModelAndView mav = new ModelAndView(); if (id != null) { mav.addObject("student", studentMap.get(Integer.parseInt(id))); mav.setViewName("student/update"); } else { mav.setViewName("student/add"); } return mav; }
五、SpringMVC对象属性自动封装
/** * 保存,有则修改,没有则 * * @param student * @return */ @RequestMapping("/save") public String save(Student student) { if (student.getId() == null) { // 新增id自动增1 identityId += 1; student.setId(identityId); studentMap.put(student.getId(), student); } else { // 修改则直接覆盖原有数据 studentMap.put(student.getId(), student); } //转发 return "redirect:/student/list.do"; //重定向 // return "redirect:/student/list.do"; }
六、内部重定向与转发:
/** * 删除 * * @param student * @return */ @RequestMapping("/delete") public String delete(@RequestParam("id") Integer id) { studentMap.remove(id); //转发 return "redirect:/student/list.do"; //重定向 // return "redirect:/student/list.do"; }
项目源码:
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"> <!-- 中文编码过滤器 --> <filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <display-name>SpringMvc02</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 定义spring mvc --> <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> <!-- controller访问后缀 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
spring-mvc.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 使用注解的包,包括子集 --> <context:component-scan base-package="com.fx"/> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp"></property> </bean> </beans>
Student.java
package com.fx.model; public class Student { private Integer id; private String name; private Integer age; public Student() { super(); } public Student(Integer id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
StudentController.java
package com.fx.controller; import java.util.HashMap; import java.util.Map; 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.fx.model.Student; /** * Controller:定义Controller控制器 RequestMapping:设置请求路径 * * @author fx * */ @Controller @RequestMapping("/student") public class StudentController { private Integer identityId = 3; private static Map<Integer, Student> studentMap = new HashMap<Integer, Student>(); static { studentMap.put(1, new Student(1, "张三", 11)); studentMap.put(2, new Student(2, "李四", 12)); studentMap.put(3, new Student(3, "王五", 13)); } /** * 查询列表 * * @return */ @RequestMapping("/list") public ModelAndView list() { ModelAndView mav = new ModelAndView(); mav.addObject("studentList", studentMap); mav.setViewName("student/list"); return mav; } /** * 跳转修改或新增页面 * * @param id * @return */ @RequestMapping("/preSave") public ModelAndView preSave( @RequestParam(value = "id", required = false) String id) { ModelAndView mav = new ModelAndView(); if (id != null) { mav.addObject("student", studentMap.get(Integer.parseInt(id))); mav.setViewName("student/update"); } else { mav.setViewName("student/add"); } return mav; } /** * 保存,有则修改,没有则 * * @param student * @return */ @RequestMapping("/save") public String save(Student student) { if (student.getId() == null) { // 新增id自动增1 identityId += 1; student.setId(identityId); studentMap.put(student.getId(), student); } else { // 修改则直接覆盖原有数据 studentMap.put(student.getId(), student); } //转发 return "redirect:/student/list.do"; //重定向 // return "redirect:/student/list.do"; } /** * 删除 * * @param student * @return */ @RequestMapping("/delete") public String delete(@RequestParam("id") Integer id) { studentMap.remove(id); //转发 return "redirect:/student/list.do"; //重定向 // return "redirect:/student/list.do"; } }
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.value.id }</td> <td>${student.value.name }</td> <td>${student.value.age }</td> <td> <a href="${pageContext.request.contextPath}/student/preSave.do?id=${student.value.id}">修改</a> <a href="${pageContext.request.contextPath}/student/delete.do?id=${student.value.id}">删除</a> </td> </tr> </c:forEach> </table> </body> </html>
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="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>
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="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>
访问路径:
http://localhost:8080/SpringMvc02/student/list.do