我试图在我的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
添加员工
`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">
$
</jsp:include>
</div>
</body>
</html>
</code>
And addEmployeeContacts.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`
package com.employee.comtroller;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.employee.model.Employee;
import com.employee.model.EmployeeContacts;
import com.employee.service.EmployeeContactsService;
@Controller
public class ContactsController {
@Autowired
private EmployeeContactsService employeeContactService;
@RequestMapping(value="/addEmployeeContacts", method=RequestMethod.GET)
public String addEmployeeContacts(@ModelAttribute("employeeContacts") EmployeeContacts employeeContacts,Model model){
model.addAttribute(employeeContacts);
return "addEmployeeContacts";
}
@RequestMapping(value="/addEmployeeContacts", method=RequestMethod.POST)
public String addEmployeeContacts(@ModelAttribute("employeeContacts") EmployeeContacts employeeContacts,HttpSession session,BindingResult result){
if(result.hasErrors()){
System.out.println(result);
return "addEmployeeContacts";
}
else{
Employee employee = (Employee)session.getAttribute("employee");
employeeContacts.setEmployee(employee);
employeeContactService.save(employeeContacts);
}
return "redirect:index.jsp";
}
}
抛出错误
org.apache.jasper.JasperException:java.lang.IllegalStateException:没有BindingResult和bean名称'employeeContacts'的普通目标对象可用作请求属性
2 回答
如果您的localhost:8080 / EmployeeManagement / addEmployee.html由于我在帖子中没有看到的映射而直接登陆addEmployee.jsp,那么您需要在jsp中执行以下操作 . 这应该向您的控制器发出请求以获取包含的视图 . 希望这可以帮助 .
您需要将.tag用作主页面,将部分视图用作.jsp
例如:
像这样创建Layout.tag
然后创建这样的局部视图