SpringMVC框架
7.4 RESTRUL_CRUD_添加操作
- 在jsp上增加连接
<a href="empInput">Add Employee</a> |
- 增加处理器方法
@RequestMapping(value="/empInput",method=RequestMethod.GET) public String empInput(Map<String,Object> map){ map.put("deptList", departmentDao.getDepartments()); //解决错误:java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute Employee employee = new Employee(); //map.put("command", employee); map.put("employee", employee); return "add"; } |
- 显示添加页面
<%@ 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> |
- 显示表单信息时,会报错:
HTTP Status 500 - type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/add.jsp at line 18 15: ② 表单回显 Stacktrace: root cause java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute |