2013-07-09 48 views
0

我有一个表示角色对象的窗体。这个角色对象可以有一个System对象,通过下拉列表(form:select)选择它。除了一个小小的障碍之外,它完美地工作:在编辑Role对象时,System对象不会在列表中自动选中。据我所知,应该是。谁能告诉我为什么不是?代码如下:弹簧窗体:选择不默认保存的对象

角色类:

/** 
* Represents a Role in the Database. Used for tracking purposes it allows us to 
* find out what users and systems have certain roles. Role entity. @author 
* MyEclipse Persistence Tools 
*/ 
@Entity 
@Table(name = "roles", catalog = "jess") 
public class Role implements java.io.Serializable { 
// Fields 
    private static final long serialVersionUID = -8599171489389401780L; 
    private Integer roleId; 
    @Valid 
    private System system; 

    ... 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "SYSTEM_ID") 
    public System getSystem() { 
     return this.system; 
    } 

    public void setSystem(System system) { 
     this.system = system; 
    } 

控制器:

@RequestMapping(value = "/" + MappingConstants.EDIT_ROLE + "/{id}", 
        method = RequestMethod.POST) 
    public ModelAndView getEditRoleForm(@PathVariable("id") Integer id) 
    { 
     Role r = new Role(); 
     r.setRoleId(id); 
     Role role = roleService.searchAllRolesByID(r); 

     ModelAndView modelView = new ModelAndView(MappingConstants.ROLES_FOLDER + MappingConstants.EDIT_ROLE); 
     modelView.addObject(AttributeConstants.ROLE, role); 

     List<System> systems = systemService.searchAllSystems(); 
     modelView.addObject(AttributeConstants.ALL_SYSTEMS, systems); 

     return modelView; 
    } 

属性编辑器:

public class SystemEditor extends PropertyEditorSupport 
{ 
    private final ISystemService systemService; 

    private static Logger logger = LogManager.getLogger(SystemEditor.class.getName()); 

    public SystemEditor(ISystemService service) 
    { 
     super(); 
     this.systemService = service; 
    } 

    /* 
    * (non-Javadoc) 
    * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String) 
    */ 
    public void setAsText(String text) throws IllegalArgumentException 
    {  
     try 
     { 
      if(logger.isDebugEnabled()) 
       logger.debug("System value coming in the editor as: {}", text); 

      System system = systemService.searchAllSystemsById(Integer.valueOf(text)); 

      setValue(system); 
     } 
     catch (Exception e) 
     { 
      logger.error("There was an error attempting to process the System from the Editor.", e); 
     } 

    } 

    /* 
    * (non-Javadoc) 
    * @see java.beans.PropertyEditorSupport#getAsText() 
    */ 
    public String getAsText() 
    { 
     System system = (System) getValue(); 

     return system.getSystemId().toString(); 
    } 
} 

和jsp:

<form:form method="post" action="${contextPath}/jess/saveeditedrole" modelAttribute="role"> 
           <h2>${role.name}</h2> 
           <br/><br/> 

           <form:errors path="system"/> 
           <form:label path="system">System:</form:label> 
           <form:select path="system"> 
           <form:options items="${systems}" itemValue="systemId" itemLabel="fullName"/> 

           </form:select> 
+0

完美!现在,你有问题吗? –

+0

是的,问题是“为什么它不工作”? – user1893043

回答

0

在你form:select你使用System类。确保这个类有正确的.equals()hashCode()方法,否则Spring不知道如何判断选择哪个System对象。