- 思想:点击增加员工按钮 = 》进入form表单 =》用户填入数据,提交 =》 handler接收新的员工数据,保存 =》重定向至员工列表的页面(可以显示新增加的员工信息条目)
- 主要流程:
一. (1) 填写数据的表单使用的是Spring的from
(2) 怎么使用form :
① 增加taglib
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
② 使用方法与html的form类似,他的path属性与html的name属性作用相同
③ 代码:
<form:form action="emp" method="post" modelAttribute="employee">
lastName: <form:input path="lastName" /> <br>
email: <form:input path="email" /> <br>
<%
Map<Integer, String> genders = new HashMap<Integer, String>();
genders.put(1, "Femal");
genders.put(0, "Male");
request.setAttribute("genders", genders);
%>
gender: <form:radiobuttons path="gender" items="${genders}" /> <br>
department: <form:select path="department.id" itemLabel="departmentName"
items="${departments}" itemValue="id"> <br>
</form:select>
<input value="submit" type="submit">
</form:form>
</body
二. 两个 handler 处理的方法
@RequestMapping(value="/emp",method=RequestMethod.GET)
public String inputEmploy(Map<String,Object> map){
map.put("departments", departmentDao.getAll());
map.put("employee", new Employ());
return "input";
}
@RequestMapping(value="/emp",method=RequestMethod.POST)
public String save(Employ employee111){
employDao.save(employee111);
return "redirect:/springmvc/testAllEmploy";
}