如何在JSF页面中显示ArrayList [英] How to display ArrayList in JSF page

查看:137
本文介绍了如何在JSF页面中显示ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create editable h:panelGrid. I created this 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中.你能告诉我我该怎么做吗?

I want to display the data into h:panelGrid. Can you tell me how I can do this?

我想创建可编辑表,但带有h:panelGrid.我想可以用h:panelGrid代替h:dataTable?

I want to create this editable table but with h:panelGrid. I suppose that it's possible to use h:panelGrid instead h:dataTable?

最美好的祝愿

推荐答案

虽然实际上可以用h:panelGrid进行黑客入侵,但您可能不得不在MB中摆弄许多不必要的代码,仅用于创建和绑定组件.

While it may be actually possible to hack something with h:panelGrid, you probably would have to fiddle with a lot of unnecessary code in your MBs, just for creating and binding components.

如果您想为HTML表提供更细粒度的控件,您可能想使用ui:repeat,如下所示:

If you want a finer grained control for your HTML table, what you want to do is probably to use ui:repeat, something like this:

<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:

For this to work you should have a backing bean looking like this:

@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...
}

查看此处提供的教程,其中教导了如何使用ui:repeat,还提供了其他在JSF中循环播放的选项.

Check out the tutorials available here, which teaches how to use ui:repeat and also presents other options for looping in JSF.

这篇关于如何在JSF页面中显示ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆