java - 如何在JSF页面中显示ArrayList

标签 java jsf jsf-2

我想创建可编辑的 h:panelGrid。我创建了这个ArrayList:

public ArrayList<userdata> dataList = new ArrayList<>();

    public class userdata
    {

        int userid;
        int groupid;
        String specialnumber;
        String username;
        String passwd;
        Date datetochangepasswd;
        String address;
        String stateregion;
        String country;
        String userstatus;
        String telephone;
        Date dateuseradded;
        Date userexpiredate;
        Date dateuserlocked;
        String city;
        String email;
        String description;

        public userdata(int userid, int groupid, String specialnumber, String username, String passwd, Date datetochangepasswd,
                String address, String stateregion, String country, String userstatus, String telephone, Date dateuseradded,
                Date userexpiredate, Date dateuserlocked, String city, String email, String description)
        {

            this.userid = userid;
            this.groupid = groupid;
            this.specialnumber = specialnumber;
            this.username = username;
            this.passwd = passwd;
            this.datetochangepasswd = datetochangepasswd;
            this.address = address;
            this.stateregion = stateregion;
            this.country = country;
            this.userstatus = userstatus;
            this.telephone = telephone;
            this.dateuseradded = dateuseradded;
            this.userexpiredate = userexpiredate;
            this.dateuserlocked = dateuserlocked;
            this.city = city;
            this.email = email;
            this.description = description;
        }

        public String getAddress()
        {
            return address;
        }

        public void setAddress(String address)
        {
            this.address = address;
        }

        public String getCity()
        {
            return city;
        }

        public void setCity(String city)
        {
            this.city = city;
        }

        public String getCountry()
        {
            return country;
        }

        public void setCountry(String country)
        {
            this.country = country;
        }

        public Date getDatetochangepasswd()
        {
            return datetochangepasswd;
        }

        public void setDatetochangepasswd(Date datetochangepasswd)
        {
            this.datetochangepasswd = datetochangepasswd;
        }

        public Date getDateuseradded()
        {
            return dateuseradded;
        }

        public void setDateuseradded(Date dateuseradded)
        {
            this.dateuseradded = dateuseradded;
        }

        public Date getDateuserlocked()
        {
            return dateuserlocked;
        }

        public void setDateuserlocked(Date dateuserlocked)
        {
            this.dateuserlocked = dateuserlocked;
        }

        public String getDescription()
        {
            return description;
        }

        public void setDescription(String description)
        {
            this.description = description;
        }

        public String getEmail()
        {
            return email;
        }

        public void setEmail(String email)
        {
            this.email = email;
        }

        public int getGroupid()
        {
            return groupid;
        }

        public void setGroupid(int groupid)
        {
            this.groupid = groupid;
        }

        public String getPasswd()
        {
            return passwd;
        }

        public void setPasswd(String passwd)
        {
            this.passwd = passwd;
        }

        public String getSpecialnumber()
        {
            return specialnumber;
        }

        public void setSpecialnumber(String specialnumber)
        {
            this.specialnumber = specialnumber;
        }

        public String getStateregion()
        {
            return stateregion;
        }

        public void setStateregion(String stateregion)
        {
            this.stateregion = stateregion;
        }

        public String getTelephone()
        {
            return telephone;
        }

        public void setTelephone(String telephone)
        {
            this.telephone = telephone;
        }

        public Date getUserexpiredate()
        {
            return userexpiredate;
        }

        public void setUserexpiredate(Date userexpiredate)
        {
            this.userexpiredate = userexpiredate;
        }

        public int getUserid()
        {
            return userid;
        }

        public void setUserid(int userid)
        {
            this.userid = userid;
        }

        public String getUsername()
        {
            return username;
        }

        public void setUsername(String username)
        {
            this.username = username;
        }

        public String getUserstatus()
        {
            return userstatus;
        }

        public void setUserstatus(String userstatus)
        {
            this.userstatus = userstatus;
        }
    }


    // Getter for the data list
    public ArrayList<userdata> getuserdata(){

        return dataList;
    }

我想将数据显示到h:panelGrid中。你能告诉我如何做到这一点吗?

我想创建this可编辑表格,但带有 h:panelGrid。我想可以使用 h:panelGrid 代替 h:dataTable

最美好的祝愿

最佳答案

虽然实际上可以使用 h:panelGrid 破解某些内容,但您可能不得不在 MB 中摆弄大量不必要的代码,只是为了创建和绑定(bind)组件。

如果您想要对 HTML 表格进行更细粒度的控制,您可能需要使用 ui:repeat,如下所示:

<table>
    <ui:repeat var="elem" value="#{yourMB.yourDataList}">
        <tr>
            <td>#{elem.userid}</td>
            <td>
               <h:outputText value="#{elem.name}" 
                    rendered="#{not elem.editable}" />
               <h:inputText value="#{elem.name}" rendered="#{elem.editable}" />
            </td>
            <td>
               <h:outputText value="#{elem.telephone}" 
                    rendered="#{not elem.editable}" />
               <h:inputText value="#{elem.telephone}"
                            rendered="#{elem.editable}" />
            </td>
            <td>
              <h:commandLink value="Edit" rendered="#{not elem.editable}"
                 action="#{yourMB.editAction(elem)}" />
            </td>
        </tr>
    </ui:repeat>
</table>
<h:commandButton value="Save Changes" action="#{yourMB.saveAction}" />

为此,您应该有一个如下所示的支持 bean:

@ManagedBean
@ViewScoped
public class YourMB {
    private List<Elem> dataList;

    @PostConstruct
    public void init() {
        // initialize dataList here
    }
    public void editAction(Elem e) {
        e.setEditable(true);
    }
    public void saveAction() {
        // do your thing here to update in the database,
        // and then reset the editable property for all items:
        for (Elem e : dataList) {
            e.setEditable(false);
        }
    }
    // getters and setters...
}

查看the tutorials available here ,它介绍了如何使用 ui:repeat 并提供了在 JSF 中循环的其他选项。

关于java - 如何在JSF页面中显示ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11675369/

相关文章:

java - 如果提前 1、2、3、4 分钟,如何将其四舍五入到最接近的 5 分钟间隔?

java - 如何 "anchor"SWT 小部件?

javascript - 等待慢速 JSF 页面的消息

java - 从 JSF 1.2 迁移到 JSF 2.0

java - Junit期望值异常

java - SonarLint super 慢

java - 使用 JSF 中的 Tomahawk 将网页数据转换为 PDF 格式?

java - 用户可以设置 JSF 支持 bean 中的哪些属性?

java - 为什么阿拉伯字母没有插入数据库?

java - 是否不支持 meta http-equiv 值缓存控制?