Spring表单:选择多个选定值?

问题描述 投票:0回答:5

我有这个编辑表格:

enter image description here

我希望选择用户的角色。如果这是一对多关系,我知道我可以做这样的事情:

        <form:label path="roles">Roles:</form:label>
        <form:select multiple="true" path="roles">
            <c:forEach items="${roles}" var="rol">
                <c:choose>
                    <c:when
                        test="${usuarioEdit.rol.id ==rol.id}">
                        <option value="${rol.id}" selected="selected">${rol.nombre}</option>
                    </c:when>
                    <c:otherwise>
                        <option value="${rol.id}">${rol.nombre}</option>
                    </c:otherwise>
                </c:choose>

            </c:forEach>
        </form:select>
        <form:errors cssStyle="color:red" path="roles"></form:errors>

但是这是一个多对多的关系。如何在编辑表单中选择选项?有简单的表格吗?

这段代码可以工作,但我想知道spring是否提供了任何设施:

<form:select multiple="true" path="roles">
            <c:forEach items="${roles}" var="rol">
                <c:set var="isSelected" value="false" />
                <c:forEach items="${rolesUsu}" var="rolUsu">
                    <c:if test="${rolUsu.getRol().getId()==rol.id}">
                        <c:set var="isSelected" value="true" />
                    </c:if>
                </c:forEach>
                <c:choose>
                    <c:when test="${isSelected}">
                        <option value="${rol.id}" selected="selected">${rol.nombre}</option>
                    </c:when>
                    <c:otherwise>
                        <option value="${rol.id}">${rol.nombre}</option>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
        </form:select>

编辑:

在我的控制器中,我有:

@InitBinder
    public void initBinder(WebDataBinder binder) {

        binder.registerCustomEditor(Set.class, "roles",
                new RolCollectionEditor(Set.class, rolDao));

    }

RolCollectionEditor:

public class RolCollectionEditor extends CustomCollectionEditor {

     private final RolDAO rolDao;


    public RolCollectionEditor(Class<?> collectionType, RolDAO rolDao) {

        super(collectionType);
        this.rolDao = rolDao;

    }

    @Override
    protected Object convertElement(Object element) {
        String rolId = (String) element;

        Rol rol = rolDao.findById(rolId);
        Usuario_Rol usuRol = new Usuario_Rol();

        //Agregamos un usuario vacio temporal
        //y lo sobreescribimos en el controlador
        Usuario usuario = new Usuario();

        usuRol.setUsuario(usuario);
        usuRol.setRol(rol);
        usuRol.setFechaCreacion(new Date());
        usuRol.setFechaModificacion(new Date());
        usuRol.setStatus("activo");

        return usuRol;
    }

}

这里Usuario_Rol是多对多关系的中间表,除了userId和rolId之外还有其他属性。

编辑2:

角色类别:

@Entity
@Table(name = "rol", uniqueConstraints = { @UniqueConstraint(columnNames = "nombre") })
public class Rol implements Serializable{

    @Id
    @Column(name = "_id")
    private String id;

    @Column(name = "nombre")
    @NotNull
    private String nombre;

    @Column(name = "descripcion")
    private String descripcion;

    @Column(name = "status")
    private String status;

    @Column(name = "fechaCreacion")
    private Date fechaCreacion;

    @Column(name = "fechaModificacion")
    private Date fechaModificacion;

    @Column(name = "fechaSincronizacion")
    private Date fechaSincronizacion;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "usuarioRol_pk.rol", orphanRemoval = true, cascade=CascadeType.ALL)
    private Set<Usuario_Rol> usuarios = new HashSet<Usuario_Rol>(0);


    //getters and setters

    @Override
    final public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((nombre == null) ? 0 : nombre.hashCode());
        return result;
    }

    @Override
    final public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Rol))
            return false;
        Rol other = (Rol) obj;
        if (nombre == null) {
            if (other.nombre != null)
                return false;
        } else if (!nombre.equals(other.nombre))
            return false;
        return true;
    }

乌苏里奥级:

@实体 @Table(name = "usuario", uniqueConstraints = { @UniqueConstraint(columnNames = "登录"), @UniqueConstraint(columnNames = "correo") }) 公共类 Usuario 实现了可序列化 {

@Id
@Column(name = "_id")
private String id;

@Column(name = "nombre")
@NotEmpty
private String nombre;

@Column(name = "apellido")
@NotEmpty
private String apellido;

@Column(name = "login")
@Size(min = 4)
@NotEmpty
private String login;

@Column(name = "password")
@NotEmpty
@Size(min = 4)
private String password;

@Column(name = "salt")
private String salt;

@Column(name = "correo")
@NotEmpty
@Email
private String correo;

@Column(name = "token")
private String token;

@Column(name = "status")
private String status;

@Column(name = "fechaUltimoLogin")
private Date fechaUltimoLogin;

@Column(name = "fechaCreacion")
private Date fechaCreacion;

@Column(name = "fechaModificacion")
private Date fechaModificacion;

@Column(name = "fechaSincronizacion")
private Date fechaSincronizacion;

@NotEmpty
@OneToMany(fetch = FetchType.EAGER, mappedBy = "usuarioRol_pk.usuario", orphanRemoval = true, cascade = CascadeType.ALL)
private Set<Usuario_Rol> roles = new HashSet<Usuario_Rol>(0);


//constructor, getters and setters.

@Override
final public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((login == null) ? 0 : login.hashCode());
    return result;
}

@Override
final public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof Usuario))
        return false;
    Usuario other = (Usuario) obj;
    if (login == null) {
        if (other.login != null)
            return false;
    } else if (!login.equals(other.login))
        return false;
    return true;
}

中级班:

@Entity
@Table(name = "usuario_rol")
@AssociationOverrides({
    @AssociationOverride(name = "usuarioRol_pk.usuario", joinColumns = @JoinColumn(name = "idUsuario")),
    @AssociationOverride(name = "usuarioRol_pk.rol", joinColumns = @JoinColumn(name = "idRol")) 
})
public class Usuario_Rol implements Serializable{

    @EmbeddedId
    private Usuario_RolId usuarioRol_pk = new Usuario_RolId();

    @Temporal(TemporalType.DATE)
    @Column(name = "fechaCreacion")
    private Date fechaCreacion;

    @Temporal(TemporalType.DATE)
    @Column(name = "fechaModificacion")
    private Date fechaModificacion;

    @Temporal(TemporalType.DATE)
    @Column(name = "fechaSincronizacion")
    private Date fechaSincronizacion;

    @Column(name = "status")
    private String status;

    //gettters, setters

    @Override
    final public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((usuarioRol_pk == null) ? 0 : usuarioRol_pk.hashCode());
        return result;
    }


    @Override
    final public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Usuario_Rol))
            return false;
        Usuario_Rol other = (Usuario_Rol) obj;
        if (usuarioRol_pk == null) {
            if (other.usuarioRol_pk != null)
                return false;
        } else if (!usuarioRol_pk.equals(other.usuarioRol_pk))
            return false;
        return true;
    }

Usuario_RolId:

@Embeddable
public class Usuario_RolId implements Serializable{

    @ManyToOne
    private Usuario usuario;

    @ManyToOne
    private Rol rol;

    public Usuario getUsuario() {
        return usuario;
    }
    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }
    public Rol getRol() {
        return rol;
    }
    public void setRol(Rol rol) {
        this.rol = rol;
    }


    @Override
    final public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((rol == null) ? 0 : rol.hashCode());
        result = prime * result + ((usuario == null) ? 0 : usuario.hashCode());
        return result;
    }

    @Override
    final public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Usuario_RolId))
            return false;
        Usuario_RolId other = (Usuario_RolId) obj;
        if (rol == null) {
            if (other.rol != null)
                return false;
        } else if (!rol.equals(other.rol))
            return false;
        if (usuario == null) {
            if (other.usuario != null)
                return false;
        } else if (!usuario.equals(other.usuario))
            return false;
        return true;
    } 

最后一个类用于模拟多对多关系的技巧。我已经遵循了本教程:http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

spring jsp spring-mvc jsp-tags
5个回答
5
投票

你为什么要自己写? Spring 应该能够为你做到这一点。用

<c:forEach />
标签替换整个块,而不是
<form options .. />
。然后 Spring 将能够自行进行选择(您可能需要一个 ConverterPropertyEditor)。

<form:select multiple="true" path="roles" items="${roles}" itemLabel="nombre" itemValue="id" />

沿着这些思路......

链接:

  1. 表单选项 文档
  2. 表格选择文档
  3. 参考指南

4
投票

如果你使用这个:

<form:select multiple="true" path="roles" items="${roles}" itemLabel="nombre" itemValue="id" />

您需要以适合您的类的正确方式重写 Usuario_Rol 的 toString() 方法,以确保 Spring 为您预先选择初始值。


1
投票

如果我正确理解了这个问题,你想要的是你的 Spring 标签生成这样的 HTML:

    <select id="roles" name="roles multiple="multiple">
       <option value="1">Administrador</option>
       <option value="2">Usuario avanzado</option>
       <option value="3" selected="selected">Usuario </option>
       <option value="4" selected="selected">Invitado</option>
    </select>

如您所见,选择了两个值(“Usuario”和“Invitado”)。

Spring 标签中“path”引用的“roles”模型属性是一个数组而不是单个值。就这么简单。请注意,我在控制器中手动设置了数组。我不熟悉代码的 ORM 方面的含义。


0
投票

我想你希望得到这样的输出:

<select id="roles" name="roles" multiple="multiple">
 <option value="1">Administrador</option>
 <option value="2" selected="selected">Usuario avanzado</option>
 <option value="3" selected="selected">Usuario </option>
</select>

在模型类中,您可以为“角色”创建方法,该方法应返回数组。在该方法中增加您的业务复杂性。

public Integer[] getRoles(){
    Integer[] selectedRoles = {2,3};
    return selectedRoles;
}

在 JSP 中:

<form:select multiple="true" path="roles">
    <form:options items="${...}" itemValue="..." itemLabel="..."/>
</form>

0
投票

像这样使用就可以了

/表格:选项>

© www.soinside.com 2019 - 2024. All rights reserved.