【发布时间】:2015-05-09 14:25:15
【问题描述】:
我试图在我的 jsp 视图页面中包含一个局部视图。我怎样才能做到这一点?
我想将我的“addEmployeeContacts.jsp”包含到“addEmployee.jsp”页面中。
addEmployee.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
Insert title here
Add Employee
Firstname:
Lastname:
<tr>
<td>Date of Birth:</td>
<td><form:input path="dob" type="date"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Employee">
</td>
</tr>
</table>
</form:form>
<div>
<jsp:include page="addEmployeeContacts.jsp">
${employeeContacts}
</jsp:include>
</div>
</body>
</html>
</code>
并添加EmployeeContacts.jsp
<code>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Add Employee</h1>
<form:form commandName="employeeContacts">
<table>
<tr>
<td>Contact Type</td>
<td><form:input path="contactType"/></td>
</tr>
<tr>
<td>Details</td>
<td><form:input path="contactValue"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Contacts">
</td>
</tr>
</table>
</form:form>
</body>
</html>
</code>
addEmployeeContactController
包 com.employee.comtroller;
导入 javax.servlet.http.HttpSession;
导入 org.springframework.beans.factory.annotation.Autowired;
导入 org.springframework.stereotype.Controller;
导入 org.springframework.ui.Model;
导入 org.springframework.validation.BindingResult;
导入 org.springframework.web.bind.annotation.ModelAttribute;
导入 org.springframework.web.bind.annotation.RequestMapping;
导入 org.springframework.web.bind.annotation.RequestMethod;
导入 com.employee.model.Employee;
导入 com.employee.model.EmployeeContacts;
导入 com.employee.service.EmployeeContactsService;
@控制器
公共类 ContactsController {
@自动连线
私人 EmployeeContactsService 雇员联系服务;
@RequestMapping(value="/addEmployeeContacts", method=RequestMethod.GET)
public String addEmployeeContacts(@ModelAttribute("employeeContacts") EmployeeContacts employeeContacts,Model model){
model.addAttribute(employeeContacts);
返回“addEmployeeContacts”;
}
@RequestMapping(value="/addEmployeeContacts", method=RequestMethod.POST)
public String addEmployeeContacts(@ModelAttribute("employeeContacts") EmployeeContacts employeeContacts,HttpSession session,BindingResult 结果){
如果(结果。hasErrors()){
System.out.println(结果);
返回“addEmployeeContacts”;
}
别的{
员工employee = (Employee)session.getAttribute("employee");
employeeContacts.setEmployee(employee);
employeeContactService.save(employeeContacts);
}
返回“重定向:index.jsp”;
}
}
投掷错误
org.apache.jasper.JasperException: java.lang.IllegalStateException: Bean 名称 'employeeContacts' 的 BindingResult 和普通目标对象都不能用作请求属性
【问题讨论】:
-
那有什么问题呢?
-
显示错误:org.apache.jasper.JasperException: java.lang.IllegalStateException: Bean 名称“employeeContacts”的 BindingResult 和普通目标对象都不能用作请求属性
-
当您收到此错误时,地址栏中显示的 URL 是什么?
-
只是想看看它是否有帮助
标签: spring spring-mvc jsp