在实体中填充后的空下拉列表

时间:2014-06-08 06:11:48

标签: java hibernate spring-mvc jpa

即使我在onLoad中填写实体,我也有空的下拉列表 任何人都可以帮忙吗?

组实体是:

@Entity
@Table(name="GROUPS")
public class Group implements Serializable,Lifecycle{

    /**
     * 
     */
    private static final long serialVersionUID = 5551707547269388327L;
    @Id
    @Column(name="ID")
    @GeneratedValue
    private int id;
    @Column(name="E_NAME")
    private String eName;
    @Column(name="A_NAME")
    private String aName;

   /*getters and setters*/


    @Override
    public boolean onDelete(Session arg0) throws CallbackException {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public void onLoad(Session session, Serializable arg1) {
        // TODO Auto-generated method stub
        Group adminGroup =new Group();
        Group sectionAdminGroup =new Group();
        Group userGroup =new Group();



        adminGroup.seteName("Admin");
        sectionAdminGroup.seteName("Section Admin");
        userGroup.seteName("User");

        adminGroup.setaName("مسشرف عام");
        sectionAdminGroup.setaName("مشرف قطاع");
        userGroup.setaName("مستخدم");

        session.save(adminGroup);
        session.save(sectionAdminGroup);
        session.save(userGroup);
    }
    @Override
    public boolean onSave(Session arg0) throws CallbackException {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public boolean onUpdate(Session arg0) throws CallbackException {
        // TODO Auto-generated method stub
        return false;
    }

}

在用户实体

@Entity
@Table(name="USERS")
public class User implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -5446873648771603353L;
    @Id
    @Column(name="ID")
    @GeneratedValue
    private int id;
    @Column(name="USERNAME")
    private String username;
    @Column(name="PASSWORD")
    private String password;
    @Column(name="DATE_CREATED")
    private String dateCreated;
    @Column(name="LAST_LOGIN_DATE")
    private String lastLoginDate;
    @Column(name="ACTIVE")
    private int active;
    @Column(name="MSISDN")
    private String msisdn;
    @Column(name="MSGS_CREDIT")
    private int msgsCredit;
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name="Group_id",referencedColumnName="ID")
    private Group group;
   /*getters and setters*/
Spring Controller中的

@SuppressWarnings("unchecked")
        @RequestMapping(method=RequestMethod.GET)
          public String listUsers(Map<String, Object> map) {

            map.put("user", new User());
            map.put("usersList", usersService.getAllObjects(User.class));
            map.put("groupsList", usersService.getAllObjects(Group.class));
            return "index";
        }
JSP中的

 <tr>
                  <td>groups</td>
                  <td><form:select path="group">
                  <form:options items="${groupsList}"/> 
                  </form:select></td>
                  </tr>
                <tr>

注意:我已将usersService.getAllObjects(Group.class)作为一个arraylist并打印了它的大小,但我得到0,所以实体在初始化后没有加载数据 我怎么装呢?

1 个答案:

答案 0 :(得分:2)

它表明OnLoad函数存在一个带有Hibernate的错误 所以你必须在你的控制器

中获取这样的值
@SuppressWarnings("unchecked")
        @RequestMapping(method=RequestMethod.GET)
          public String listUsers(Map<String, Object> map) {
            List <Group> groupsList=(ArrayList<Group>)usersService.getAllObjects(Group.class);
            map.put("user", new User());
            map.put("usersList", usersService.getAllObjects(User.class));
            map.put("groupsList", usersService.getAllObjects(Group.class));

            if (groupsList.size()!=0){
                for (Group group:groupsList){
                    }
            }else{
                fillData(fillGroups());
                map.put("groupsList", usersService.getAllObjects(Group.class));     
            }
            return "index";
        }

并且方法填充数据将

private ArrayList fillGroups(){

         List <Group> groupsList=new ArrayList();

        Group adminGroup =new Group();
        Group sectionAdminGroup =new Group();
        Group userGroup =new Group();

        adminGroup.setId(1);
        sectionAdminGroup.setId(2);
        userGroup.setId(3);


        adminGroup.seteName("Admin");
        sectionAdminGroup.seteName("Section Admin");
        userGroup.seteName("User");

        adminGroup.setaName("مشرف عام");
        sectionAdminGroup.setaName("مشرف قطاع");
        userGroup.setaName("مستخدم");


        groupsList.add(adminGroup);
        groupsList.add(sectionAdminGroup);
        groupsList.add(userGroup);


        System.out.println("he call fillGroup....");
        return (ArrayList) groupsList;

    }

     @ModelAttribute("Group")
     private  void fillData(ArrayList<Group> list){

         for (Group group:list){
           usersService.addObject(group);
         }
     }

在你的Jsp中

                  <td>groups</td>
                  <td><form:select path="group.id">
                  <form:options items="${groupsList}" itemLabel="eName" itemValue="id" /> 
                  </form:select></td>
                  </tr>