SpringMVC-RestfulCRUD 冷不防 2022-10-05 04:45 86阅读 0赞 ### 文章目录 ### * * 回顾helloword基本配置 * 员工列表展示 * 添加 * 修改 * 删除 ## 回顾helloword基本配置 ## web.xml <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>SpringMVC_crud</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>SpringMVC_crud</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置一个字符编码的filter --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- encoding:指定解决POST请求乱码 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <!-- forceEncoding:顺手解决响应乱码 --> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 支持Rest风格转换的filter --> <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> xxxx-sevlet.xml <!-- 扫描所有组件 --> <context:component-scan base-package="com.jh"></context:component-scan> <!-- 配置视图解析器,拼接页面 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> @Controller public class helloController { @RequestMapping("/hello") public String hello(){ return "success"; } } ## 员工列表展示 ## 访问index.jsp---->直接发送/emps---->控制器查询所有员工---->放在请求域中---->转发到list页面展示 @Controller public class EmployeeController { @Autowired EmployeeDao employeeDao; /* * 查询所有员工 */ @RequestMapping("/emps") public String getEmps(Model model){ Collection<Employee> all = employeeDao.getAll(); model.addAttribute("emps", all); return "list"; } index.jsp <!-- 访问项目就要展示员工列表页面 --> <jsp:forward page="/emps"></jsp:forward> list.jsp <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <title>员工列表</title> </head> <body> <h1>员工列表页面</h1> <table border="1" cellpadding="5px" cellspacing="0"> <tr> <td>ID</td> <th>lastName</th> <th>email</th> <th>gender</th> <th>departmentName</th> <th>edit</th> <th>delete</th> </tr> <c:forEach items="${emps }" var="emp"> <tr> <td>${ emp.id }</td> <td>${ emp.lastName }</td> <td>${ emp.email }</td> <td>${ emp.gender==0?"女":"男" }</td> <td>${ emp.department.departmentName }</td> <td>edit</td> <td>delete</td> </tr> </c:forEach> </table> </body> ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2NjcyNzQ2_size_16_color_FFFFFF_t_70] ## 添加 ## 1、list.jsp点击“员工添加”---->2、查询出所有的部门信息要展示在页面\---->3、来到add.jsp---->4、输入员工数据---->5、点击保存---->6、处理器收到员工保存请求(保存员工)---->7、保存完成后来到list.jsp /* * 去员工添加页面,去之前查出所有部门信息进行展示 */ @RequestMapping("/toaddpage") public String toAddPage(Model model){ //1.查出所有部门 Collection<Department> departments = departmentDao.getDepartments(); //2.放在请求域中 model.addAttribute("depts", departments); model.addAttribute("employee", new Employee()); //3.去添加页面 return "add"; } list.jsp <a href="toaddpage">添加员工</a> add.jsp <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <h1>员工添加</h1> <!-- 表单标签 通过springMVC的表单标签可以实现将模型数据中的属性和HTML表单元素绑定, 以实现表单数据更便捷编辑和表单值的回显 1)SpringMVC认为,表单数据中的每一项最终都是要回显的 path指定的是一个属性,这个属性是从隐含模型(请求域中取出的某个对象的属性); path指定的每一个属性,请求域中必须有一个对象,拥有这个属性;这个对象就是请求域中的command; modelAttribute="" 1)、以前我们变淡标签会从请求域中获取一个command对象;把这个对象中的每一个属性对应的显示出来 2)、可以告诉springmvc不要去取command的值,我放了一个modelAttribute指定的值 去对象用的key就用我modelAttribute指定的 --> <form:form action="" modelAttribute="employee"> lastName:<form:input path="lastName"/><br> email:<form:input path="email"/><br> gender:<br> 男:<form:radiobutton path="gender" value="1"/><br> 女:<form:radiobutton path="gender" value="0"/><br> dept: <!-- items="",指定要遍历的集合,自动遍历,遍历出的每一个元素都是一个department对象 itemLabel="属性",指定遍历出的这个对象的哪个属性时作为option标签体的值 itemValue="属性名",指定刚才遍历出来的这个对象的哪个属性时作为要提交的value值 --> <form:select path="department.id" items="${depts }" itemLabel="departmentName" itemValue="id"></form:select><br> <input type="submit" value="保存"> </form:form> 联系 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2NjcyNzQ2_size_16_color_FFFFFF_t_70 1] 5、点击保存 /* * 保存员工 */ @RequestMapping(value="/emp",method=RequestMethod.POST) public String addEmp(Employee employee){ System.out.println("要添加的员工;"+employee); employeeDao.save(employee); return "redirect:/emps"; } add.jsp <!-- 绝对路径 --> <% pageContext.setAttribute("ctp", request.getContextPath()); %> <form:form action="${ctp }/emp" modelAttribute="employee" method="POST"> 联系: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2NjcyNzQ2_size_16_color_FFFFFF_t_70 2] 运行结果 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2NjcyNzQ2_size_16_color_FFFFFF_t_70 3] ## 修改 ## /* * 修改 */ @RequestMapping(value="/emp/{id}",method=RequestMethod.PUT) public String updateEmp(@ModelAttribute("employee")Employee employee){ System.out.println("要修改的员工"+employee); //更新保存二合一 employeeDao.save(employee); return "redirect:/emps"; } /* * 提前查询员工 */ @ModelAttribute public void myModelAttribute(@RequestParam(value="id",required=false)Integer id,Model model){ if(id!=null){ Employee employee = employeeDao.get(id); model.addAttribute("employee", employee); } } list.jsp 为edit加链接 <% pageContext.setAttribute("ctp", request.getContextPath()); %> <td> <a href="${ctp }/emp/${emp.id }">edit</a> </td> <td>delete</td> </tr> </c:forEach> </table> <a href="${ctp }/toaddpage">添加员工</a> edit.jsp <% pageContext.setAttribute("ctp", request.getContextPath()); %> <h1>员工修改页面</h1> <form:form action="${ctp }/emp/${employee.id }" modelAttribute="employee" method="post"> <input type="hidden" name="_method" value="put"> <input type="hidden" name="id" value="${employee.id }"> email:<form:input path="email"/> gender: 男:<form:radiobutton path="gender" value="1"/> 女:<form:radiobutton path="gender" value="0"/><br> dept: <form:select path="department.id" items="${depts }" itemLabel="departmentName" itemValue="id"></form:select> <br> <input type="submit" value="修改"> </form:form> ## 删除 ## /* * 删除 */ @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE) public String deleteEmp(@PathVariable("id")Integer id){ employeeDao.delete(id); return "redirect:/emps"; } list.jsp <td> <form action="${ctp }/emp/${emp.id }" method="post"> <input type="hidden" name="_method" value="DELETE"> <input type="submit" value="删除"> </form> </td> [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2NjcyNzQ2_size_16_color_FFFFFF_t_70]: /images/20221005/d819d8113ebe40008edb302e6dade007.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2NjcyNzQ2_size_16_color_FFFFFF_t_70 1]: /images/20221005/cfa77bbaee1e4f56bbc9699ca15e8aa4.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2NjcyNzQ2_size_16_color_FFFFFF_t_70 2]: /images/20221005/8bca3beca8494b73890eb32a71fe6cf5.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2NjcyNzQ2_size_16_color_FFFFFF_t_70 3]: /images/20221005/1edc3971949c4475916d88c9f31e2aab.png
还没有评论,来说两句吧...