注意!!!
此教程是基于《【SpringMVC】7.REST风格的CRUD实战(一)之前期工作》来讲解的,在阅读前请务必查阅此文章。
一、前情提要
在之前的第一篇文章《【SpringMVC】7.REST风格的CRUD实战(一)之前期工作》中,我们明确了两个API接口要求
显示添加页面
- URI:emp
- 请求方式:GET
- 显示效果
添加员工信息
- URI:emp
- 请求方式:POST
- 显示效果:完成添加,重定向到 list 页面。
二、接口分析
- 显示页面的URL都是emp,但是请求方式分别是
GET
和POST
。 - 页面中有一个
select
按钮(Department),这个必须从数据库拿,所以需要经过handler
类。 - 为了更快的开发页面,需要使用springmvc标签。
- 添加成功后,重定向到list页面。
- Spring 的表单标签:通过 SpringMVC 的表单标签可以实现将模型数据 中的属性和 HTML 表单元素相绑定,以实现表单 数据更便捷编辑和表单值的回显。
form
标签:一般情况下,通过 GET 请求获取表单页面,而通过 POST 请求提交表单页面,因此获取表单页面和提交表单 页面的 URL 是相同的。只要满足该最佳条件的契约,<form:form>
标签就无需通过 action 属性指定表单 提交的 URL可以通过modelAttribute
属性指定绑定的模型属性,若没有指定该属性,则默认从request
域对象中读取command
的表单 bean,如果该属性值也不存在,则会 发生错误。
三、具体步骤
1.在EmployeeHandler
把相关的Handler方法写好
由于两个全局变量employeeDao
和departmentDao
在之前的文章已经有被创建了,在这里重复写是为了照顾没有看之前文章的同学,方便理解这个变量是从哪来的,切记不要重复创建这两个全局变量了。
ackage com.springmvc.crud.handlers;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.springmvc.crud.dao.DepartmentDao;
import com.springmvc.crud.dao.EmployeeDao;
import com.springmvc.crud.entities.Employee;
@Controller
public class EmployeeHandler {
@Autowired
private EmployeeDao employeeDao;
@Autowired
private DepartmentDao departmentDao;
@RequestMapping(value = "emp", method = RequestMethod.POST)
public String save(Employee employee) {
employeeDao.save(employee);
return "redirect:/emps";
}
@RequestMapping(value = "emp", method = RequestMethod.GET)
public String input(Map<String, Object> map) {
map.put("departments", departmentDao.getDepartments());
map.put("employee", new Employee());
return "input";
}
}
2.input.jsp
相关代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'input.jsp' starting page</title>
</head>
<body>
<!--
为什么使用form标签呢?
1.因为可以更快速地开发出表单页面,而且可以更方便地进行表单值的回显
2.注意
可以通过modelAttribute 属性指定绑定的模型属性,
若没有指定该属性,则默认从request 域对象中读取command的表单bean,
如果也不存在,则报错java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute。
-->
<form:form action="${pageContext.request.contextPath }/emp" method="POST" modelAttribute="employee">
<c:if test="${employee.id == null }">
<!-- path属性对应html表单标签的name -->
LastName:<form:input path="lastName" />
<br>
</c:if>
<c:if test="${employee.id != null }">
<form:hidden path="id"/>
<input type="hidden" name="_method" value="PUT">
<%--
对于_method 不能使用form:hidden 标签,因为ModelAttribute 对应的bean中没有 _method 属性--%>
<%-- <form:hidden path="_method"/> --%>
<br>
</c:if>
Email:<form:input path="email" />
<br>
<%
Map<String, String> genders = new HashMap<String, String>();
genders.put("1", "Male");
genders.put("0", "Female");
request.setAttribute("genders", genders);
%>
Gender:<form:radiobuttons path="gender" items="${genders }" />
<br>
Department:<form:select path="department.id" items="${departments }"
itemLabel="departmentName" itemValue="id"></form:select>
<br />
<input type="submit" value="Submit">
</form:form>
</body>
</html>
3.在list.jsp
中添加一个用于添加操作的超链接
<a href="emp">Add New Employee</a>
4.最终效果
启动TomCat,访问 http://localhost:8080/springmvc-2/emps
点击超链接Add New Employee
能够成功添加用户