如何在Spring MVC中渲染局部视图
我试图在我的jsp视图页面中包含一个局部视图。我该怎么做? 我想将我的“addEmployeeContacts.jsp”包含在“addEmployee.jsp”页面中。 addEmployee.jsp如何在Spring MVC中渲染局部视图,spring,spring-mvc,jsp,Spring,Spring Mvc,Jsp,我试图在我的jsp视图页面中包含一个局部视图。我该怎么做? 我想将我的“addEmployeeContacts.jsp”包含在“addEmployee.jsp”页面中。 addEmployee.jsp 在此处插入标题 添加员工 名字: 姓氏: <tr> <td>Date of Birth:</td> <td><form:input path="dob"
在此处插入标题
添加员工
名字:
姓氏:
<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}
和addEmployeeContacts.jsp
<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>
<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>
在此处插入标题
添加员工
接触式
细节
加法器
<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>
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";
}
}
包com.employee.com控制器;
导入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.employeecontacts服务;
@控制器
公共类联系人控制器{
@自动连线
私人雇员联系服务雇员联系服务;
@RequestMapping(value=“/addEmployeeContacts”,method=RequestMethod.GET)
公共字符串addEmployeeContacts(@ModelAttribute(“employeeContacts”)employeeContacts employeeContacts,模型模型){
model.addAttribute(employeeContacts);
返回“addEmployeeContacts”;
}
@RequestMapping(value=“/addEmployeeContacts”,method=RequestMethod.POST)
公共字符串addEmployeeContacts(@ModelAttribute(“employeeContacts”)employeeContacts employeeContacts,HttpSession会话,BindingResult){
if(result.hasErrors()){
系统输出打印项次(结果);
返回“addEmployeeContacts”;
}
否则{
Employee=(Employee)session.getAttribute(“Employee”);
employeeContacts.setEmployee(employee);
employeeContactService.save(employeeContacts);
}
返回“redirect:index.jsp”;
}
}
投掷误差
<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>
org.apache.jasper.jaspereException:java.lang.IllegalStateException:bean名称“employeeContacts”的BindingResult或普通目标对象都不能作为请求属性使用如果您的本地主机:8080/EmployeeManagement/addEmployee.html由于我在您的帖子中没有看到的映射而直接将您登录到addEmployee.jsp,然后,您需要在jsp中执行以下操作。这将向控制器发出请求,以获取包含的视图。希望这有帮助
<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>
<div>
<jsp:include page="/addEmployeeContacts">
${employeeContacts}
</jsp:include>
</div>
${employeeContacts}
您需要使用.tag作为主页面,部分视图作为.jsp
<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>
例如:
<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>
像这样创建Layout.tag
<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>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@tag description="Overall Page template" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Home</title>
</head>
<body>
<section class="content">
<jsp:doBody />
</section>
</body>
</html>
<%@taglib prefix="t" tagdir="/WEB-INF/tags/"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<t:Layout>
<div>
your partial view html content
</div>
</t:Layout>
家
然后创建这样的局部视图
<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>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@tag description="Overall Page template" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Home</title>
</head>
<body>
<section class="content">
<jsp:doBody />
</section>
</body>
</html>
<%@taglib prefix="t" tagdir="/WEB-INF/tags/"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<t:Layout>
<div>
your partial view html content
</div>
</t:Layout>
部分查看html内容
那么问题出在哪里呢?显示错误:org.apache.jasper.jaspereException:java.lang.IllegalStateException:bean名称“employeeContacts”的BindingResult和普通目标对象都不能作为请求属性使用,当您出现此错误时,地址栏中显示的URL是什么?谢谢您的回复,我只是想知道是否有此错误帮助