0

我有一个表示角色对象的表单。这个角色对象可以有一个系统对象,它是通过下拉列表(form:select)选择的。它完美地工作,除了一点点障碍:编辑角色对象时,系统对象不会自动在列表中选择。据我了解,应该是这样。谁能告诉我为什么不是?代码如下:

角色类:

/**
 * 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>
4

1 回答 1

0

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

于 2013-10-09T10:08:04.507 回答