SpringMVC框架
7.6 添加员工实验代码
- 表单
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!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> <!-- 1.为什么使用SpringMVC的form标签 ① 快速开发 ② 表单回显 2.可以通过modelAttribute指定绑定的模型属性, 若没有指定该属性,则默认从request域中查找command的表单的bean 如果该属性也不存在,那么,则会发生错误。 --> <form:form action="empAdd" method="POST" modelAttribute="employee"> LastName : <form:input path="lastName" /><br><br> Email : <form:input path="email" /><br><br> <% Map<String,String> map = new HashMap<String,String>(); map.put("1", "Male"); map.put("0","Female"); request.setAttribute("genders", map); %> Gender : <br><form:radiobuttons path="gender" items="${genders }" delimiter="<br>"/><br><br> DeptName : <form:select path="department.id" items="${deptList }" itemLabel="departmentName" itemValue="id"></form:select><br><br> <input type="submit" value="Submit"><br><br> </form:form> </body> </html> |
- 控制器方法
@Controller public class EmployeeHandler { @RequestMapping(value="/empAdd",method=RequestMethod.POST) public String empAdd(Employee employee){ employeeDao.save(employee); return "redirect:/empList"; } } |