java 如何访问jsf数据表中的Map键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11550354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access Map key in jsf datatable
提问by Catfish
I'm getting the error javax.el.PropertyNotFoundException: /member/apps/cms/edit.xhtml @228,49 value="#{props.key}": Property 'key' not found on type java.util.HashMap$Values
when trying to display the datatable below.
javax.el.PropertyNotFoundException: /member/apps/cms/edit.xhtml @228,49 value="#{props.key}": Property 'key' not found on type java.util.HashMap$Values
尝试显示下面的数据表时出现错误。
<p:dataTable id="properties" var="props" value="#{contentEditorBacking.properties}" editable="true">
<p:column headerText="Property">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{props.key}" />
</f:facet>
<f:facet name="input">
<h:inputText value="#{props.key}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Value">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{props.value}" />
</f:facet>
<f:facet name="input">
<h:inputText value="#{props.value}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Edit">
<p:rowEditor />
<!-- Need to put an update on here yet -->
<p:commandLink styleClass="ui-icon ui-icon-trash" id="deleteProperty" actionListener="#{contentEditorBacking.deleteProperty}">
<f:attribute name="key" value="#{props.key}" />
</p:commandLink>
</p:column>
</p:dataTable>
Here's the relevant part of my contentEditorBacking:
这是我的 contentEditorBacking 的相关部分:
@ManagedBean
@ViewScoped
public class ContentEditorBacking {
private Map<String, Properties> properties = new LinkedHashMap<String, Properties>();
public Collection<Properties> getProperties() throws Exception{
return properties.values();
}
public static class Properties{
private String key;
private String value;
public Properties(String key, String value) {
super();
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "key=" + key + ", value=" + value + "";
}
}
}
How can i access the key value from my properties map?
如何从我的属性映射中访问键值?
回答by BalusC
Until the upcoming JSF 2.2, the <h:dataTable>
/<p:dataTable>
doesn't support Collection<E>
. It only supports among others List<E>
.
在即将到来的 JSF 2.2 之前,<h:dataTable>
/<p:dataTable>
不支持Collection<E>
. 它只支持等等List<E>
。
You need to replace
你需要更换
public Collection<Properties> getProperties() throws Exception{
return properties.values();
}
by
经过
private List<Properties> propertiesAsList;
public List<Properties> getProperties() throws Exception{
return propertiesAsList;
}
and somewhere directly after map's initialization do this
并在地图初始化后的某个地方直接执行此操作
propertiesAsList = new ArrayList<Properties>(properties.values());
(note: don't do it inside the getter!)
(注意:不要在 getter 中这样做!)
回答by Nicolae Albu
In the JSF code, you are trying to access the key property of the thing returned by getProperties() method, which is the entire collection. You need to iterate through the props variable.
在 JSF 代码中,您试图访问 getProperties() 方法返回的事物的关键属性,即整个集合。您需要遍历 props 变量。
回答by Jonathan S. Fisher
Your properties class needs to implement the map interface here: http://docs.oracle.com/javase/6/docs/api/java/util/Map.html
您的属性类需要在此处实现地图接口:http: //docs.oracle.com/javase/6/docs/api/java/util/Map.html
The better question is though, why are you creating your own class? Java offers a Properties class in the SDK: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html
更好的问题是,为什么要创建自己的类?Java 在 SDK 中提供了一个 Properties 类:http: //docs.oracle.com/javase/7/docs/api/java/util/Properties.html
EDIT: Update my answer per request of OP
编辑:根据 OP 的要求更新我的答案
Change the following line, then fix the compile errors in Eclipse:
更改以下行,然后修复 Eclipse 中的编译错误:
public static class Properties{
to:
到:
public static class Properties implements Map<String, String> {
EDIT #2: Actually my answer is probably wrong. The OP didn't post his entire xhtml file, and I made an assumption that was incorrect.
编辑#2:实际上我的回答可能是错误的。OP 没有发布他的整个 xhtml 文件,我做了一个不正确的假设。
回答by Luiggi Mendoza
The dataTable must receive a Collection (for example, a List). Map doesn't implement the Collection interface. You should convert your map into a list and modify it, then convert the list back into your map. Here's a good link showing how to do it:
dataTable 必须接收一个集合(例如,一个列表)。Map 没有实现 Collection 接口。您应该将地图转换为列表并进行修改,然后将列表转换回您的地图。这是一个很好的链接,显示了如何做到这一点:
Of course, I won't recommend you to add logic in the getter/setter, instead use 2 different attributes and keep the getter and setter the cleanest possible way.
当然,我不建议你在 getter/setter 中添加逻辑,而是使用 2 个不同的属性,并以最干净的方式保持 getter 和 setter。