easyUI类取嵌套的类型(pastJson结合)

时间:2022-09-08 12:03:47

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",
    joinColumns=@JoinColumn(name="user_id",referencedColumnName="id"),
    inverseJoinColumns=@JoinColumn(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",
 joinColumns=@JoinColumn(name="role_id",referencedColumnName="id"),
 inverseJoinColumns=@JoinColumn(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>