在 JSTL 中显示 bean

问题描述 投票:0回答:3

我在使用 JSTL 显示 bean 的详细信息时遇到问题。我的servlet代码如下

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    
    String username =request.getParameter("username");
    String password = request.getParameter("password");
    try {
        
        con = connection.connectionfile();
        
        pstmt=con.prepareStatement("select * from Login where Username=? and Password=?");
        pstmt.setString(1,username);
        pstmt.setString(2, password);
        rs = pstmt.executeQuery();
        
        if(rs.next())
        {
            ListGrabber grabber = new ListGrabber();
            List<DvdDetail> list = new ArrayList<DvdDetail>();
            list=grabber.getBeanList();
            
            request.setAttribute("listbean",list);
            RequestDispatcher view = request.getRequestDispatcher("listdetail.jsp");
            
            view.forward(request, response);
            
            
        }
        else
        {
            response.sendRedirect("loginerror.html");
        }
        
    } 
     catch(Exception ex)
     {
        ex.printStackTrace();
     }
    
    finally {            
        out.close();
        try
        {
            rs.close();
            con.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

Bean如下:

   public class DvdDetail {

private int id;
private String film_name;
private String released_date;
private String directed_by;
private String actor_name;
private String actress_name;
private int price;
private String banner;

public DvdDetail(int id,String film_name,String released_date,String directed_by,String actor_name,String actress_name,int price,String banner)
{
    
    this.id=id;
    this.film_name=film_name;
    this.released_date=released_date;
    this.directed_by=directed_by;
    this.actor_name=actor_name;
    this.actress_name=actress_name;
    this.price=price;
    this.banner=banner;
    
}

public String getActor_name() {
    return actor_name;
}

public void setActor_name(String actor_name) {
    this.actor_name = actor_name;
}

public String getActress_name() {
    return actress_name;
}

public void setActress_name(String actress_name) {
    this.actress_name = actress_name;
}

public String getBanner() {
    return banner;
}

public void setBanner(String banner) {
    this.banner = banner;
}

public String getDirected_by() {
    return directed_by;
}

public void setDirected_by(String directed_by) {
    this.directed_by = directed_by;
}

public String getFilm_name() {
    return film_name;
}

public void setFilm_name(String film_name) {
    this.film_name = film_name;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getPrice() {
    return price;
}

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

public String getReleased_date() {
    return released_date;
}

public void setReleased_date(String released_date) {
    this.released_date = released_date;
}

        

}

如何在 JSP 页面中打印多个电影名称,但仅使用 JSTL。在 JSP 中我通常将其解决为

List<DvdDetail> list = request.getParameter("listbean");
for(int i=0;i<list.size();i++)
{
  DvdDetail bean = list.get(i);
  ///displaying usng out.print(bean.getProperty());
}

我需要实现同样的目标,但是使用 JSTL。

java jsp model-view-controller jstl
3个回答
2
投票

单击您在问题中添加的 JSTL 标签上弹出窗口中的 info 链接,以获取有关 JSTL 的信息和教程链接。

你想要实现的目标可以这样实现:

<ul>
    <c:forEach var="dvd" items="${listbean}">
        <li><c:out value="${dvd.film_name}"/></li>
    </c:forEach>
</ul>

1
投票

您可以使用:

<c:forEach var="dvdDetail" items="${listbean}">
      ${dvdDetail.film_name}
      ${dvdDetail.actor_name}
     ...

</c:forEach>

0
投票

如果您是 JSTL 新手,您应该检查this。上面有一些链接。希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.