【问题标题】:Cannot access f:selectItems variable for passthrough attribute无法访问 f:selectItems 变量的传递属性
【发布时间】:2016-05-27 06:24:37
【问题描述】:

我正在使用 JSF 2.2,并且我想通过使用 f:selectItems 变量的属性在 h:selectOneMenu 生成的每个 option 元素上显示一个 title 属性,并使用 passthrough

似乎我无法访问 f:selectItems 变量来自定义我的 passthrough 属性

这是我到目前为止所做的事情

我要显示的实体

public class ItemBean {
    private int id;
    private String strName;
    private String strDescription;

    public ItemBean(int id, String strName, String strDescription) {
        this.id = id;
        this.strName = strName;
        this.strDescription = strDescription;
    }

    // Getters and Setters
}

我的 backbean 方法来检索实体列表

public List<ItemBean> getItems() {
    return new ArrayList<ItemBean>(){
        {
            add(new ItemBean(1, "Java", "Java programming language"));
            add(new ItemBean(2, "PHP", "Yet another language"));
            add(new ItemBean(3, "Python", "Not a snake at all"));
        }
    };
}

我的h:selectOneMenu在视图中

<h:selectOneMenu>
    <f:selectItems value="#{bean.items}" var="item"
                           itemValue="#{item.id}"
                           itemLabel="#{item.strName}"
                           p:title="Description : #{item.strDescription}"/>
</h:selectOneMenu>

问题是我无法访问p:titleitem 变量,那里的输出只是空的。

这是生成的代码

<select>
    <option title="Description : " value="1">Java</option>
    <option title="Description : " value="2">PHP</option>
    <option title="Description : " value="3">Python</option>
</select>

是否可以这样做或有其他方法?

【问题讨论】:

    标签: jsf selectonemenu passthrough-elements


    【解决方案1】:

    我终于从这篇帖子Using f:selectItems var in passtrough attribute 中使用jstl c:forEach 循环和f:selectItem 找到了我的问题的解决方案@

    代码如下:

    <h:selectOneMenu>
        <c:forEach items="#{bean.items}" var="item">
            <f:selectItem itemValue="#{item.id}"
                          itemLabel="#{item.strName}"
                          p:title="Description : #{item.strDescription}"/>
        </c:forEach>
    </h:selectOneMenu>
    

    【讨论】:

      猜你喜欢
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 2017-11-17
      • 2021-11-14
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多