JSF 2无法识别 itemLabel 和 itemValue 属性
我在 Facelets 页面中有以下下拉列表:
<h:selectOneMenu value="#{contactBean.selectedContact}" converter="#{contactConverter}">
<f:selectItems value="#{contactsHolder.contacts}" var="contact"
itemLabel="#{contact.firstName}" itemValue="#{contact}" />
</h:selectOneMenu>
问题是,无论我为 itemLabel
输入什么内容(JSF EL 表达式或纯文本),它都不会显示。知道我做错了什么吗?
这是 ContactConverter
:
@ManagedBean(name = "contactConverter")
@RequestScoped
public class ContactConverter implements Converter, Serializable {
@ManagedProperty(value = "#{contactsHolder}")
private ContactsHolder contactsHolder;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return contactsHolder.getContacts().get(value);
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return ((Contact) value).getContactID();
}
//getter & setters...
}
和 ContactsHolder
:
@ManagedBean
@SessionScoped
public class ContactsHolder implements Serializable {
private Map<String, Contact> contacts;
@PostConstruct
public void init() {
contacts = new LinkedHashMap<String, Contact>();
//get Contacts data and populate map...
}
//getters & setters...
}
I have the following dropdown in a Facelets page:
<h:selectOneMenu value="#{contactBean.selectedContact}" converter="#{contactConverter}">
<f:selectItems value="#{contactsHolder.contacts}" var="contact"
itemLabel="#{contact.firstName}" itemValue="#{contact}" />
</h:selectOneMenu>
The problem is, no matter what I put in for itemLabel
(JSF EL expression or just plain text), it doesn't display. Any idea what I'm doing wrong?
Here's ContactConverter
:
@ManagedBean(name = "contactConverter")
@RequestScoped
public class ContactConverter implements Converter, Serializable {
@ManagedProperty(value = "#{contactsHolder}")
private ContactsHolder contactsHolder;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return contactsHolder.getContacts().get(value);
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return ((Contact) value).getContactID();
}
//getter & setters...
}
And ContactsHolder
:
@ManagedBean
@SessionScoped
public class ContactsHolder implements Serializable {
private Map<String, Contact> contacts;
@PostConstruct
public void init() {
contacts = new LinkedHashMap<String, Contact>();
//get Contacts data and populate map...
}
//getters & setters...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在向 仅具有返回
提供Map
。var
属性中的每个项目都是 < code>Map.EntryString< 的
getKey()
和getValue()
方法/code> 地图键和Contact
地图值 分别。Map.Entry
类确实没有getFirstName()
方法。相应地修复它:
或者,如果您的目标是支持 Servlet 3.0 / EL 2.2 的容器,该容器允许调用非 getter 方法,以便您可以使用
Map#values()
获取Collection
:或者,将
#{contactsHolder.contacts}
设为List
,以便您的初始视图代码正常工作:You're feeding a
Map<String, Contact>
to<f:selectItems value>
. Each item invar
attribute will be aMap.Entry<String, Contact>
which has onlygetKey()
andgetValue()
methods returning theString
map key andContact
map value respectively. TheMap.Entry
class indeed doesn't have agetFirstName()
method.Fix it accordingly:
Or, if you target a Servlet 3.0 / EL 2.2 capable container which allows invoking non-getter methods, so that you can use
Map#values()
to get aCollection<Contact>
:Or, make the
#{contactsHolder.contacts}
aList<Contact>
instead so that your initial view code will work: