日期:2014-05-19  浏览次数:20724 次

jsp页面遍历action里的list
struts2的 action如下:
Java code

package com.jin.action;

import java.util.ArrayList;
import java.util.List;

import com.jin.pojo.Book;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction implements Action
{
    private List<Book> books;
    
    public List<Book> getBooks()
    {
        return books;
    }

    public void setBooks(List<Book> books)
    {
        this.books = books;
    }

    public String execute() throws Exception
    {
            List<Book> list = new ArrayList();
            Book bk1 = new Book();
            bk1.setName("Linux实战指南");
            bk1.setPrice(52.5);
            Book bk2 = new Book();
            bk2.setName("JSP/Servlet实战");
            bk2.setPrice(60.0);
            Book bk3 = new Book();
            bk3.setName("Spring3.0全面解析");
            bk3.setPrice(44.5);
            list.add(bk1);
            list.add(bk2);
            list.add(bk3);
            
            this.setBooks(list);
            
            return Action.SUCCESS;
        
    }

}




books.java
Java code

package com.jin.pojo;

import java.io.Serializable;

public class Book implements Serializable
{
    private static final long serialVersionUID = -7637363221122361901L;
    private String name;
    private double price;
    
    
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public double getPrice()
    {
        return price;
    }
    public void setPrice(double price)
    {
        this.price = price;
    }
}




请问分别使用struts标签和jstl标签在结果页遍历这个action的属性books 应该怎么写?

------解决方案--------------------
<c:forEach items="${books}" var="book" varStatus="status">

<td>
${book.id }
</td>
<td>
${book.name }
</td>
<td>
${book.price }
</td>
</tr>

</c:forEach>

------解决方案--------------------
同意楼上