easyUI類取嵌套的類型(pastJson結合)

來源:互聯網
上載者:User

標籤:

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結合)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.