【发布时间】:2011-07-24 17:51:20
【问题描述】:
基本上,我想在 JSP 页面上的 ArrayList 中显示产品。我已经在 servlet 代码中做到了。但是没有输出。
我还必须将 products.jsp 放在 /WEB-INF 文件夹中吗?当我这样做时,我得到一个请求的非资源错误。
我的 Servlet 代码 (InventoryServlet.java)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
List<Product> products = new ArrayList<Product>();
products = Inventory.populateProducts(); // Obtain all products.
request.setAttribute("products", products); // Store products in request scope.
request.getRequestDispatcher("/products.jsp").forward(request, response); // Forward to JSP page to display them in a HTML table.
} catch (Exception ex) {
throw new ServletException("Retrieving products failed!", ex);
}
}
我的 JSP 页面 (products.jsp)
<h2>List of Products</h2>
<table>
<c:forEach items="${products}" var="product">
<tr>
<td>${product.Description}</td>
<td>${product.UnitPrice}</td>
</tr>
</c:forEach>
</table>
Web.xml
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>Inventory</servlet-name>
<servlet-class>com.ShoppingCart.InventoryServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Inventory</servlet-name>
<url-pattern>/products</url-pattern>
</servlet-mapping>
</web-app>
【问题讨论】:
-
可能你需要把 JSP 放在外面 WEB-INF
-
如果您追踪到 servlet 调用 - 您确定“产品”中有一些价值吗?
-
只要您使用正确的路径来引用它们,JSP 几乎可以去任何地方。您的编译位最终将在 WEB-INF/ 下 - 可能是 classes/ 或类似的
-
只是为了确保:您导航到
/products,而不是/products.jsp,对吧? -
好的。现在我收到了这个错误。 javax.el.PropertyNotFoundException:在类型 com.ShoppingCart.Product 上找不到属性“Id”