標籤:
user類:
@Entity
@Table(name="c_user")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
private int age;
@Column(length=2)
private String sex;
private String LoginName;
private String password;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="user_role",
[email protected](name="user_id",referencedColumnName="id"),
[email protected](name="role_id",referencedColumnName="id")
)
private Set<Role> roles;
Role類:
@Entity
@Table(name="role")
public class Role {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
@JSONField(serialize=false)//注意此處,是轉換JSON
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="user_role",
[email protected](name="role_id",referencedColumnName="id"),
[email protected](name="user_id",referencedColumnName="id")
)
private Set<User> users;
轉換JSON工具類
public static void toBeJson(List list, int total) throws Exception{
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
// List<Map<String,Object>> ObjectList = new ArrayList<Map<String,Object>>();
Map<String,Object> map =new HashMap<String, Object>();
map.put("total", total);
map.put("rows", list);
// ObjectList.add(map);
System.out.println("======"+JSONObject.toJSONString(map));
// System.out.println("+++++++++++++++"+jobj.toString());
response.setCharacterEncoding("utf-8");
response.getWriter().write(JSONObject.toJSONString(map,SerializerFeature.DisableCircularReferenceDetect));
// response.getWriter().write(JSONObject.toJSONString(map));
//log.info(jobj.toString());
}
資料格式:{"total":3,
"rows":[
{"age":12,"id":1,"loginName":"123","name":"123","password":"123","role":[{"id":1,"name":"經理"}],"sex":"1"},
{"age":12,"id":2,"loginName":"nn","name":"122","password":"12","role":[{"id":2,""name":"主管"}],"sex":"1"},
{"age":21,"id":3,"loginName":"hj","name":"da","password":"d","role":[{"id":3,"name":"員工"}],"sex":"as"}
]}
========================================================
easyUI頁面:
<table id="dg" title="使用者資訊管理" class="easyui-datagrid" style="width:1020px;height:500px"
data-options="toolbar:‘#toolbar‘,pagination:true,singleSelect:true,
url:‘user_showUser.action‘,pageSize:10,pageList:[ 5, 10, 15, 20 ]">
<thead>
<tr>
<th field="id" width="100" >編號</th>
<th field="name" width="100">姓名</th>
<th field="sex" width="100">性別</th>
<th field="age" width="100">年齡</th>
<th field="loginName" width="100">登陸名</th>
<th field="password" width="100">登陸密碼</th>
<th field="role" formatter="formatterName" width="100">角色</th>
</tr>
</thead>
===============================================================
<script type="text/javascript">
function formatterName(value,row){
// alert("=="+row.role[0].name)
return row.role[0].name;
}
</script>
easyUI類取嵌套的類型(pastJson結合)