如何在JSF 2中显示链接列表?

时间:2014-11-10 09:35:12

标签: jsf java-ee primefaces linked-list

我有一个包含3个产品的Linkedlist,我想根据我点击的按钮在输入文本中显示其中一个产品的值。我正在使用Primefaces。 我不知道如何创造这样做的空白。

类产品:

public class Product {

private String idp;
private String brand;
private String price;


public Product() {

}

public Product(String idp, String brand, String price) {
    this.idp =  idp;
    this.brand = brand;
    this.price = price;
}

public String getIdp() {
    return idp;
}

public void setIdp(String idp) {
    this.idp = idp;
}

public String getBrand() {
    return brand;
}

public void setBrand(String brand) {
    this.brand = brand;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

 }

类商店:

public class Shop {


 private LinkedList<Product> listprod;


public Shop() {
    listprod = new LinkedList<>();
    Product product1 = new Product("01","brand1","15");
    Product product2 = new Product("02","brand2","30");
    Product product3 = new Product("03","brand3","60");
    listprod.add(product1);
    listprod.add(product2);
    listprod.add(product3);
}

public LinkedList<Product> getListprod() {
    return listprod;
}

public void setListprod(LinkedList<Product> listprod) {
    this.listprod = listprod;
}

 }

查看索引:

<!DOCTYPE html>
<html 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">


 <h:head>
    <meta charset="utf-8" />
    <title>Products</title>   
</h:head>


<h:body id="mybody">

 <h:form id="myform">
    <ui:repeat var="temp" value="#{mybean.shop.listprod}" >
    <h:panelGrid columns="2" cellpadding="5" id="mygrid">

        <h:outputLabel for="idp" value="Id :"/>
        <p:inputText  id="idp" value="#{temp.idp}"/>


        <h:outputLabel for="brand" value="Brand :"/>
        <p:inputText  id="brand" value="#{temp.brand}"/>

        <h:outputLabel for="price" value="Price :"/>
        <p:inputText id="price" value="#{temp.price}"/>

        <p:commandButton value="Product 1" actionListener="#{mybean.showdata()}" />
        <p:commandButton value="Product 2" actionListener="#{mybean.showdata()}" />
        <p:commandButton value="Product 3" actionListener="#{mybean.showdata()}" />

    </h:panelGrid>
  </ui:repeat>

    </h:form>

</h:body>

</html>

我的豆子:

@SessionScoped
@ManagedBean(name="mybean")
public class Mybean {

private Product product;
private Shop shop;


public void showdata(){



}

public Product getProduct() {
    return product;
}

public void setProduct(Product product) {
    this.product = product;
}

public Shop getShop() {
    return shop;
}

public void setShop(Shop shop) {
    this.shop = shop;
}


}

2 个答案:

答案 0 :(得分:1)

使用ui:repeat标记对其进行迭代。

<ui:repeat var="temp" value="#{mybean.listprod}">
<tr>
    <td>#{temp.idp}</td>
    <td>#{temp.brand}</td>
    <td>#{temp.price}</td>
</tr>
</ui:repeat>

类似问题How to use <ui:repeat> to iterate over a nested list?

答案 1 :(得分:0)

您正在使用primefaces并希望显示列表,为什么不使用http://www.primefaces.org/showcase/ui/data/dataList.xhtml

<p:dataList value="#{mybean.listprod}" var="product" type="ordered">
    <f:facet name="header">
        Products
    </f:facet>
    #{product.idp}, #{product.brand}, #{product.price}
</p:dataList>

或(可编辑)数据表http://www.primefaces.org/showcase/ui/data/datatable/edit.xhtml

<p:dataTable var="product" value="#{mybean.listprod}" editable="true">
   <f:facet name="header">
       Products
   </f:facet>
   <p:column headerText="idp">
       <p:cellEditor>
           <f:facet name="output"><h:outputText value="#{product.idp}" /></f:facet>
           <f:facet name="input"><p:inputText value="#{product.idp}" style="width:100%"/></f:facet>
       </p:cellEditor>
   </p:column>
   <p:column headerText="brand">
       <p:cellEditor>
           <f:facet name="output"><h:outputText value="#{product.brand}" /></f:facet>
           <f:facet name="input"><p:inputText value="#{product.brand}" style="width:100%"/></f:facet>
       </p:cellEditor>
   </p:column>
   <p:column headerText="price">
       <p:cellEditor>
           <f:facet name="output"><h:outputText value="#{product.price}" /></f:facet>
           <f:facet name="input"><p:inputText value="#{product.price}" style="width:100%"/></f:facet>
       </p:cellEditor>
   </p:column>
</p:dataTable>