我想在spring select元素中显示详细信息表的父表行。这是父表的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 属性值呢?
答案 0 :(得分:2)
<form:options>
正在调用Dept
对象上的toString()方法,以填充html:选项的value
和name
从spring-form.tld开始,<form:options>
的定义如下:
<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>
因此,您可以配置<form:options>
,以便使用Dept
id
和value
所使用的<form:options items="${depts}" itemValue="id" itemLabel="dname" />
的正确属性:
pip list