How to Iterate through object property inside a bean in my jsp

限于喜欢 提交于 2021-02-08 10:55:35

问题


I have a list products with these attributes :

  • Identifier
  • Color
  • Size
  • Supplier

Supplier attribute is an object that has these attributes :

  • Name
  • Phone

For each product in my list, i'd like to display the identifier & the supplier name. How can i do this with struts / jstl ?

Here is what i'm trying with no success :

<s:iterator value="products">
    <tr>
        <td><s:property value="identifiant"/></td>
        <td><s:property value="supplier.name"</td>
    </tr>
</s:iterator>

回答1:


There should be getters for each attribute. If the products is the property of the Action class then it should have

//default constructor

public List<Product> getPoducts(){...} //getter

In the Product class

//default constructor

public String getIdentifier(){...} //getter
public String getColor(){...} //getter
public String getPhone(){...} //getter
public String getSupplier(){...} //getter

In the Supplier class

//default constructor

public String getName(){...} //getter
public String getPhone(){...} //getter

In JSP

<s:iterator value="products">
    <tr>
        <td><s:property value="identifier"/></td>
        <td><s:property value="supplier.name"/></td>
    </tr>
</s:iterator>



回答2:


Using JSTL you can try to use the <c:forEach> tag like this :

<tr>
<c:forEach items="products" var="product>
    <td>
        <c:out value="${product.identifiant}">
        <c:out value="${product.supplier.name}">
    </td>
</c:forEach>
</tr>



回答3:


You can try this:

<c:forEach items="${products}" var="product">
    <tr>
        <td><c:out value="${product.identifier}" /></td>
        <td><c:out value="${product.supplier.name}" /></td>
    </tr>
</c:forEach>


来源:https://stackoverflow.com/questions/35488265/how-to-iterate-through-object-property-inside-a-bean-in-my-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!