【问题标题】:form:select options value is same text as the options text形式:选择选项值与选项文本相同的文本
【发布时间】:2016-09-14 12:23:01
【问题描述】:

我想在弹簧选择元素内显示明细表的父表行。这是父表的bean定义:

@Entity
@Table(name = "HR.DEPARTMENTS")
public class Dept {

    @Id
    @Column(name = "DEPARTMENT_ID")
    private int id;

    @Column(name = "DEPARTMENT_NAME")
    private String dname;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "dept")
    @Transient
    private Set<User> users = new HashSet<User>(0);

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    @Override
    public String toString() {
        return dname;
    }

}

spring select元素的来源就是这个List:

@Override
@Transactional
public List<Dept> list() {
    @SuppressWarnings("unchecked")
    List<Dept> listDept = (List<Dept>) sessionFactory.getCurrentSession()
    .createCriteria(Dept.class)
    .addOrder(Order.asc("dname"))
    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
    return listDept;
}

jsp 是:

<form:select path="dept">
    <form:option value="" label="-- choose a department --"/>
    <form:options items="${depts}" />
</form:select>

在运行时进入更新模式时,选择选项的值与其文本相同!

那么如何让options的值成为bean的id属性值呢?

【问题讨论】:

    标签: spring jsp spring-mvc


    【解决方案1】:

    &lt;form:options&gt; 正在调用 Dept 对象上的 toString() 方法,以填充 html:option 的 valuename

    在 spring-form.tld 中,&lt;form:options&gt; 的定义是这样的:

    <tag>
            <description>Renders a list of HTML 'option' tags. Sets 'selected' as appropriate
            based on bound value.</description>
            <name>options</name>
            <tag-class>org.springframework.web.servlet.tags.form.OptionsTag</tag-class>
            <body-content>empty</body-content>
            <attribute>
                <description>HTML Standard Attribute</description>
                <name>id</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
            <attribute>
                <description>The Collection, Map or array of objects used to generate the inner
                'option' tags. This attribute is required unless the containing select's property
                for data binding is an Enum, in which case the enum's values are used.</description>
                <name>items</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
            <attribute>
                <description>Name of the property mapped to 'value' attribute of the 'option' tag</description>
                <name>itemValue</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
            <attribute>
                <description>Name of the property mapped to the inner text of the 'option' tag</description>
                <name>itemLabel</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
    

    因此,您可以配置&lt;form:options&gt; 以使用您想要用作idvalueDept 的正确属性:

    <form:options items="${depts}" itemValue="id" itemLabel="dname" />
    

    【讨论】:

      猜你喜欢
      • 2014-11-10
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 2011-06-27
      • 1970-01-01
      • 2019-02-13
      相关资源
      最近更新 更多