0
我在spring項目中有一個JSP。我曾經使用Ajax的數據表獲取所有員工詳細信息。但是在加載頁面時我收到了異常。這是我的JSP,模型和控制器方法。糾正我的錯誤。數據表ajax請求中的異常
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User and Employee Management</title>
// Required script and Css link
</head>
<body>
<div class="generic-container" style="width:97%; height:95%;">
<%@include file="authheader.jsp" %>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading"><span class="lead">All Employees</span>
</div>
</div>
<div class="panel-heading"><span class="lead">Employee Details </span>
</div>
<div style="height:70%;overflow:auto;">
<table class="table table-hover" style="overflow-x:auto" id="example">
<thead>
<tr>
<th>Employee Reference Id</th>
<th>Employee Name</th>
<th>Employee Designation</th>
<th>Employee Salary</th>
<th>Address</th>
</tr>
</thead>
<tbody>
</div>
</body>
<script type="text/javascript">
$(document).ready(
function() {
$("#example").DataTable({
scrollY: 280,
scrollCollapse: true,
paging: true,
"sAjaxSource" : "getEmployees",
"aoColumns" : [
{
"mData" : "employeeReferenceId"
},
{
"mData" : "employeeName"
},
{
"mData" : "employeeDesg"
},
{
"mData" : "salary"
},{
"mData" : "address.address"
}
]
});
});
</script>
型號:僱員
@Entity
@Table(name = "Employee")
public class Employee implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "EMPLOYEE_ID")
private int id;
@NotEmpty
@Column(name = "EMPLOYEE_NAME")
private String employeeName;
@NotEmpty
@Column(name = "EMPLOYEE_DESG")
private String employeeDesg;
@NotEmpty
@Column(name = "SALARY")
private String salary;
@NotEmpty
@Column(name = "EMPLOYEE_REFERENCE_ID")
private String employeeReferenceId;
@OneToOne(mappedBy="employee", cascade = CascadeType.ALL)
private Address address;
//Getters and Setters
}
型號:地址
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@Column(name="ADDRESS_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="emp_id",insertable = false, updatable = false)
private int employeeId;
@NotNull
@Column(name = "ADDRESS")
private String address;
@OneToOne(fetch=FetchType.EAGER , cascade=CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="emp_id",referencedColumnName="employee_id")
private Employee employee;
//Getters and Setters
}
和控制器的方法是
@RequestMapping(value = { "/getEmployees" })
public @ResponseBody Map<String, Object> getEmployees(ModelMap model) {
Map<String, Object> data = new HashMap<String, Object>();
List<Employee> employees=employeeService.findAllEmployees();
data.put("data", employees);
return data;
}
,誤差
Apr 13, 2017 5:58:25 PM
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
handleHttpMessageNotWritable
WARNING: Failed to write HTTP message:
org.springframework.http.converter.HttpMessageNotWritableException: Could
not write content: failed to lazily initialize a collection of role
也應該由stacktrace輸入引起。這將告訴究竟發生了什麼。看起來好像你在某個實體的某個地方有角色收集,而某些事情是不對的。 – user871199