案例使用員工emp和部門dept來演示。
實體對象
emp和dept
public class Dept implements java.io.Serializable {
private Integer deptid;
private String deptname;
public class Emp implements java.io.Serializable {
private Integer empid;
private String empname;
private Date empdate;
private Dept dept;
Action類
使用hibernate得到員工List集合,然後轉換成Json傳給JSP
public String getEmpData() {
List<Emp> emps = empService.getEmps(page, rows);
try {
writer.write(JSONUtil.serialize(emps));
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
JSP頁面
<table id="dg" title="用戶列表" class="easyui-datagrid" style="width:100%;height:90%;"
url="${pageContext.request.contextPath}/emp_getEmpData"
toolbar="#toolbar" pagination="true" pagesize="20"
rownumbers="true" fitColumns="true" singleSelect="true">
<thead>
<tr>
<th field="empid" width="65">編號</th>
<th field="empname" width="100">姓名</th>
<th field="empdate" width="100">入職日期</th>
<th field="dept" formatter="deal_dept" width="100">部門</th>
</tr>
</thead>
</table>
JS代碼
function deal_dept(value,row,rowIndex) {
if(row.dept!=undefined){
return row.dept.deptname;
}
}
查詢結果