p:selectOneMenu问题?

时间:2011-08-21 22:35:48

标签: jsf jsf-2 primefaces selectonemenu

以下代码适用于h:selectOneMenu,但不适用于p:selectOneMenu

p:selectOneMenu

中没有项目

的facelet

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core">
  <h:head>
    <title>Facelet Title</title>
  </h:head>
  <h:body>
    <h:form>
      <h:selectOneMenu value="#{testBean.name}">
        <f:selectItems value="#{testBean.names}"/>
      </h:selectOneMenu>

      <p:selectOneMenu value="#{testBean.name}">
        <f:selectItems value="#{testBean.names}"/>
      </p:selectOneMenu>
    </h:form>
  </h:body>
</html>

package net.footfeed.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class TestBean {

  private String name = "name1";
  private String[] names = new String[] {"name1", "name2", "name2"};

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String[] getNames() {
    return names;
  }

}

1 个答案:

答案 0 :(得分:1)

好奇,它不支持List<T>上的T[]<f:selectItems>。您需要创建List<SelectItem>SelectItem[],或者更好地明确指定var属性。这仅适用于List<T>

<f:selectItems value="#{bean.names}" var="name" itemValue="#{name}" itemLabel="#{name}" />

private List<String> names = Arrays.asList("name1", "name2", "name3");

由于这没有任何意义,我建议将此问题报告给他们的问题跟踪器。

更奇怪的是,当我省略itemLabel(然后应该只显示itemValue作为标签)时,它会在Tomcat上抛出一个相当严重的NPE:

java.lang.NullPointerException
    at org.apache.catalina.connector.CoyoteWriter.write(CoyoteWriter.java:182)
    at com.sun.faces.application.view.WriteBehindStateWriter.write(WriteBehindStateWriter.java:127)
    at com.sun.faces.renderkit.html_basic.HtmlResponseWriter.write(HtmlResponseWriter.java:666)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeSelectItems(SelectOneMenuRenderer.java:261)

PrimeFaces 3.0显然仍处于测试阶段。