0

我在前端使用 primefaces 并selectOneList在我的支持 bean 中使用 arraylist填充

我想让数组列表用新行分隔以进行一些逻辑分隔

如何将元素添加到ArrayList将呈现为空行的元素?

4

1 回答 1

1

下面的代码将帮助您实现您想要的功能,即在<p:selectOneListBox>. 现在我不确定挤出空字符串是否是该组件的一个功能。所以""行不通。

这是一个示例托管 bean:

package app.so.dev.web.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import app.so.dev.web.model.Student;

@ManagedBean(name = "so15350373")
@ViewScoped
public class SO15350373 implements Serializable{

    private static final long serialVersionUID = 2190171095461884759L;
    private List<Student> students;
    private int selected;

    @PostConstruct
    public void init() {
        students = new ArrayList<Student>();
        students.add(new Student(1, "Student 1"));
        students.add(new Student(2, "&nbsp;"));
        students.add(new Student(3, "Student 3"));
        students.add(new Student(4, "&nbsp;"));
        students.add(new Student(5, "Student 5"));
        students.add(new Student(6, "Student 6"));
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public int getSelected() {
        return selected;
    }

    public void setSelectedStudent(int selected) {
        this.selected = selected;
    }   
}

Student是一个简单的 POJO,有两个字段idname. 为了简洁起见,我没有展示这个类:P。

这是xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" template="/WEB-INF/templates/globalTemplate.xhtml">

    <ui:define name="title">15350373</ui:define>
    <ui:define name="content">
        <h:form>
            <p:selectOneListbox value="#{so15350373.selected}" id="list">
                <f:selectItems value="#{so15350373.students}" var="student" itemLabel="#{student.name}" itemValue="#{student.id}" />
            </p:selectOneListbox>
        </h:form>
    </ui:define>

</ui:composition>
于 2013-03-12T07:28:06.820 回答