p:selectOneMenu 有问题吗?

发布于 2024-11-30 17:06:20 字数 1507 浏览 0 评论 0原文

以下代码适用于 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;
  }

}

The following code works fine for h:selectOneMenu but doesn't work for p:selectOneMenu

There is no item in 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;
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃气十足 2024-12-07 17:06:20

奇怪的是,它不支持 上的 ListT[]。您需要创建一个 ListSelectItem[],或者更好的是显式指定 var 属性。这仅适用于 List

<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 显然仍处于测试阶段阶段。

Curious, it doesn't support a List<T> or T[] on <f:selectItems>. You'd need to to create a List<SelectItem> or SelectItem[] or, better, to explicitly specify the var attribute. This works with List<T> only.

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

with

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

Since this makes no sense, I'd suggest to report this issue to their issue tracker.

More curious, when I omit the itemLabel (which should then just display the itemValue as label), it throws a pretty serious NPE on Tomcat:

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 is clearly still in beta stage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文