Ps:注意避免动词的使用,用名词来替代更加符合规范。
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--处理中文乱码--> <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> <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:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> // 针对 put delete 请求 <filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <mvc:annotation-driven> <!-- 消息转换器 针对json格式 --> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 配置自动扫描 --> <context:component-scan base-package="com.imooc"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
package com.imooc.entity; public class Course { private int id; private String name; private double price; // 省略 getter、setter }
package com.imooc.dao; import com.imooc.entity.Course; import org.springframework.stereotype.Repository; import java.util.Collection; import java.util.HashMap; import java.util.Map; @Repository public class CourseDAO { private Map<Integer,Course> courses = new HashMap<Integer,Course>(); /** * 新增课程 */ public void add(Course course){ courses.put(course.getId(),course); } /** * 查询全部课程 */ public Collection<Course> getAll(){ return courses.values(); } /** * 通过id查询课程 */ public Course getById(int id){ return courses.get(id); } /** * 修改课程 */ public void update(Course course){ courses.put(course.getId(),course); } /** * 删除课程 */ public void deleteById(int id){ courses.remove(id); } }
package com.imooc.controller; import com.imooc.dao.CourseDAO; import com.imooc.entity.Course; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; @Controller public class CourseController { @Autowired private CourseDAO courseDAO; /** * 添加课程 */ @PostMapping(value = "/add") public String add(Course course){ courseDAO.add(course); return "redirect:/getAll"; } /** * 查询全部课程 * @return */ @GetMapping(value = "/getAll") public ModelAndView getAll(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("index"); modelAndView.addObject("courses",courseDAO.getAll()); return modelAndView; } /** * 通过id查询课程 */ @GetMapping(value = "/getById/{id}") public ModelAndView getById(@PathVariable(value = "id") int id){ ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("edit"); modelAndView.addObject("course",courseDAO.getById(id)); return modelAndView; } /**修改课程 * */ @PutMapping(value = "/update") public String update(Course course){ courseDAO.update(course); return "redirect:/getAll"; } /** * 删除课程 */ @DeleteMapping(value = "/delete/{id}") public String delete(@PathVariable(value = "id") int id){ courseDAO.deleteById(id); return "redirect:/getAll"; } }
// index.jsp 核心代码 <c:forEach items="${courses}" var="course"> <tr> <td><input type='checkbox' class='check_item'/></td> <td>${course.id}</td> <td>${course.name}</td> <td>${course.price}</td> <td> <form action="${pageContext.request.contextPath}/getById/${course.id}" method="get"> <button class="btn btn-primary btn-sm edit_btn" type="submit"> <span class="glyphicon glyphicon-pencil">编辑</span> </button> </form> </td> <td> <form action="${pageContext.request.contextPath}/delete/${course.id}" method="post"> <button class="btn btn-danger btn-sm delete_btn" type="submit"> <input type="hidden" name="_method" value="DELETE"/> <span class="glyphicon glyphicon-trash">删除</span> </button> </form> </td> </tr> </c:forEach>
// add.jsp 核心代码 <form class="form-horizontal" role="form" action="${pageContext.request.contextPath}/add" method="post"> <div class="form-group"> <label class="col-sm-1 control-label">课程编号</label> <div class="col-sm-3"> <input type="text" class="form-control" name="id" placeholder="请输入课程编号"> </div> </div> <div class="form-group"> <label class="col-sm-1 control-label">课程名称</label> <div class="col-sm-3"> <input type="text" class="form-control" name="name" placeholder="请输入课程名称"> </div> </div> <div class="form-group"> <label class="col-sm-1 control-label">课程价格</label> <div class="col-sm-3"> <input type="text" class="form-control" name="price" placeholder="请输入课程价格"> </div> </div> <div class="form-group"> <div class="col-sm-offset-1 col-sm-3"> <button type="submit" class="btn btn-default">提交</button> </div> </div> </form>
// edit.jsp 核心代码 <form class="form-horizontal" role="form" action="${pageContext.request.contextPath}/update" method="post"> <div class="form-group"> <label class="col-sm-1 control-label">课程编号</label> <div class="col-sm-3"> <input type="text" value="${course.id}" name="id" readonly="readonly" class="form-control"> </div> </div> <div class="form-group"> <label class="col-sm-1 control-label">课程名称</label> <div class="col-sm-3"> <input type="text" value="${course.name}" name="name" class="form-control"> </div> </div> <div class="form-group"> <label class="col-sm-1 control-label">课程价格</label> <div class="col-sm-3"> <input type="text" value="${course.price}" name="price" class="form-control"> </div> </div> <div class="form-group"> <div class="col-sm-offset-1 col-sm-3"> <input type="hidden" name="_method" value="PUT"/> <button type="submit" class="btn btn-default">提交</button> </div> </div> </form>
Ps:实体类自动绑定的话,只要前端页面属性 name="实体类中的属性名" 即可。