mercredi 6 mai 2015

Showing data using JSP

I'm making an ordering system using Java servlet and JSP. The problem is I can't view the menu list on JSP page after passed it from a servlet. Here is my code.

Menu.java

public class Menu implements Serializable {

    private static final long serialVersionUID = -8072555192585991919L;

    private int menuId;
    private String menuName;
    private double menuPrice;

    public Menu(String menuName,double menuPrice) {
        this.setMenuName(menuName);
        this.setMenuPrice(menuPrice);
    }

    public Menu() {
        setMenuName("");
        setMenuPrice(0);
    }

    public double getMenuPrice() {
        return menuPrice;
    }

    public void setMenuPrice(double menuPrice) {
        this.menuPrice = menuPrice;
    }

    public String getMenuName() {
        return menuName;
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }

    public int getMenuId() {
        return menuId;
    }

    public void setMenuId(int menuId) {
        this.menuId = menuId;
    }

}

MenuDAO.java

public List<Menu> getAllMenu() {
    List<Menu> menus = new ArrayList<>();
    try {
        Statement statement = con.createStatement();
        ResultSet rs = statement.executeQuery("select * from cafemenu");

        while(rs.next()){
            Menu menu = new Menu();
            menu.setMenuId(rs.getInt("menu_id"));
            menu.setMenuName(rs.getString("menu_name"));
            menu.setMenuPrice(rs.getDouble("menu_price"));
            menus.add(menu);
        }

    } catch(Exception e) {
        e.printStackTrace();
    }
    return menus;
}

ListMenuServlet.java

public class MenulistServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    List<Menu> listMenu = new ArrayList<>();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        listMenu = new MenuDao().getAllMenu();
        request.setAttribute("list", listMenu);
        request.getRequestDispatcher("welcome.jsp").forward(request, response); 
    }
}

welcome.jsp

<%@ taglib prefix="c" uri="http://ift.tt/QfKAz6" %>

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Welcome</title>
</head>
<body>
  <table border="1" width="100%">
      <tr>
        <th>Menu ID</th>
        <th>Menu Name</th>
        <th>Menu Price</th>
      </tr>
    <c:forEach var="row" items="${list}">
      <tr>
        <td>${row.menu_id}</td>
        <td>${row.menu_name}</td>
        <td>${row.menu_price}</td>
      </tr>
    </c:forEach>
  </table>
</body>
</html>

I tried to check few times but still can't get the table to work. It only showing the header of the table, and nothing more.

Thanks for the help!

Aucun commentaire:

Enregistrer un commentaire