【问题标题】:How to display a Linkedlist in JSF 2?如何在 JSF 2 中显示链表?
【发布时间】:2014-11-10 09:35:11
【问题描述】:

我有一个包含 3 个产品的链接列表,我想根据我单击的按钮在输入文本中显示其中一个产品的值。我正在使用 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;
}


}

【问题讨论】:

  • 只需使用ui:repeat 标签遍历listprod 元素。
  • 我在索引中包含了一个 ui 重复标签,对吗?以及如何做空?我什至不确定我声明 Linkedlist 的方式是否正确。

标签: jsf jakarta-ee primefaces linked-list


【解决方案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?

【讨论】:

  • 我已经在索引视图中更新了我的代码,现在 ui:repeat 中的所有内容都没有显示在屏幕上(没有标签、没有输入文本、没有按钮)
  • 查看 DOM 树(通常是浏览器的 f12)。找出正在渲染的内容。
【解决方案2】:

您正在使用 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 2012-03-16
    • 1970-01-01
    • 2010-09-22
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多