Servlet集属性不在jsp中打印

时间:2014-08-05 08:54:59

标签: java html jsp java-ee

我已经设置了一个book列表并设置为request.setAttribute(" booksa",allbooks);在jsp中我尝试在表中打印List,但没有值只打印空表。

这是我的Servelet

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {


    String userPath = request.getServletPath();

     if (userPath.equals("/index")) {
        // TODO: Implement category request

       userPath = "/index";

    }
     else if (userPath.equals("/books"))
     {
        List<Book> allbooks = bookFacade.findAll();

        userPath = "/books";
        request.setAttribute("booksa", allbooks);
        //System.out.print(allbooks);
     }
     else
     {

     }

     String url = userPath + ".jsp";

      try {
        request.getRequestDispatcher(url).forward(request, response);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

这是我的Book.jsp页面

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>
<body>




<table border="1">
    <!-- column headers -->
    <tr>
        <td>ISBN</td>
        <td>TITLE</td>
        <td>PRICE</td>
        <td>YEARS</td>
        <td>LANGUAGE</td>
    </tr>


<!-- column data -->

    <c:forEach var="vehicle" items="${booksa}">
            <tr>
                <td><c:out value="${vehicle.isbn}"  /></td>
                <td><c:out value="${vehicle.title}" /></td>
                <td><c:out value="${vehicle.price}" /></td>
                <td><c:out value="${vehicle.years}"  /></td>
                <td><c:out value="${vehicle.languages}"  /></td>
            </tr>

   </c:forEach>
</table>   
</body>

但是当我重定向到此页面时,视图页面源显示如下

<!-- column data -->

    <c:forEach var="vehicle" items="[ejb.Book[ isbn=SR001 ]]">
            <tr>
                <td><c:out value=""  /></td>
                <td><c:out value="" /></td>
                <td><c:out value="" /></td>
                <td><c:out value=""  /></td>
                <td><c:out value=""  /></td>
            </tr>

   </c:forEach>

My Book课程

public Book() {
}

public Book(String isbn) {
    this.isbn = isbn;
}

public Book(String isbn, String title, double price, int years, String languages) {
    this.isbn = isbn;
    this.title = title;
    this.price = price;
    this.years = years;
    this.languages = languages;
}

public String getIsbn() {
    return isbn;
}

public void setIsbn(String isbn) {
    this.isbn = isbn;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public double getPrice() {
    return price;
}

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

public int getYears() {
    return years;
}

public void setYears(int years) {
    this.years = years;
}

public String getLanguages() {
    return languages;
}

public void setLanguages(String languages) {
    this.languages = languages;
}

@XmlTransient
public Collection<Author> getAuthorCollection() {
    return authorCollection;
}

public void setAuthorCollection(Collection<Author> authorCollection) {
    this.authorCollection = authorCollection;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (isbn != null ? isbn.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Book)) {
        return false;
    }
    Book other = (Book) object;
    if ((this.isbn == null && other.isbn != null) || (this.isbn != null && !this.isbn.equals(other.isbn))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "ejb.Book[ isbn=" + isbn + " ]";
}

}

价值观即将到来,但我不知道如何打印请给我一个帮助

2 个答案:

答案 0 :(得分:0)

如果您没有将jstl添加到构建路径,只需从此处的引用添加

即可

1。Adding jstl support

2。Getting JSTL to run within Tomcat and Eclipse

希望这会有所帮助!!

答案 1 :(得分:0)

在JSP中包含以下行。这应该可以解决你的问题

<%@ taglib prefix="c" 
       uri="http://java.sun.com/jsp/jstl/core" %>