以下代码适用于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>
Bean
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;
}
}
奇怪的是,它不支持<f:selectItems>
上的List<T>
或T[]
。您需要创建一个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显然仍处于测试阶段。