2012-07-18 16 views
3

尝试显示下面的数据表时,出现错误javax.el.PropertyNotFoundException: /member/apps/cms/edit.xhtml @228,49 value="#{props.key}": Property 'key' not found on type java.util.HashMap$Values如何访问jsf数据表中的Map键

<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> 

这里是我的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 + ""; 
     } 

    } 
} 

我如何可以访问我的属性的关键值映射?

+0

发布了XHTML文件的大部分内容。 – 2012-07-18 21:26:37

+0

对不起,数据表标签被切断。它现在在那里。 – Catfish 2012-07-18 21:30:39

回答

6

直到即将到来的JSF 2.2,<h:dataTable>/<p:dataTable>不支持Collection<E>。它只支持其他List<E>

你需要

public Collection<Properties> getProperties() throws Exception{ 
    return properties.values(); 
} 

通过

private List<Properties> propertiesAsList; 

public List<Properties> getProperties() throws Exception{ 
    return propertiesAsList; 
} 

,并直接某处地图的初始化后更换做到这一点

propertiesAsList = new ArrayList<Properties>(properties.values()); 

(注:不这样做的getter中!)

1

你的属性的类必须实现地图界面点击这里:http://docs.oracle.com/javase/6/docs/api/java/util/Map.html

更好的问题是,虽然,你为什么要创建自己的类? Java提供的SDK一个属性类:http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html

编辑:更新每个操作的要求

更改以下行我的答案,然后固定在Eclipse编译错误:

public static class Properties{ 

到:

public static class Properties implements Map<String, String> { 

编辑#2:其实我的答案可能是错误的。 OP没有发布他的整个xhtml文件,我做了一个不正确的假设。

+0

这是一种不同的属性。 – Catfish 2012-07-18 21:14:18

+0

你能否提供关于如何实现Map接口的代码?我实际上没有编写这段代码,我只是想对页面进行更新,而且比我的技术更先进,所以我正在努力学习它。 – Catfish 2012-07-18 21:15:24

1

dataTable必须接收一个Collection(例如,一个List)。 Map没有实现Collection接口。您应该将地图转换为列表并进行修改,然后将列表转换回您的地图。下面是展示如何做到这一点的好链接:

当然,我不会建议你在的getter/setter添加逻辑,而不是使用2个不同的属性,并保持吸气并设置最干净的可能方式。

2

在JSF代码中,您试图访问由getProperties()方法返回的事件的关键属性,该方法是整个集合。你需要遍历props变量。