以下代码适用于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;
}
}
答案 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显然仍处于测试阶段。