【问题标题】:JSTL table creation on JSP Page Load- no data in JSP在 JSP 页面加载上创建 JSTL 表 - JSP 中没有数据
【发布时间】:2014-02-18 09:38:20
【问题描述】:

我正在尝试创建一个包含不同大小表格的 JSP 主页。我想使用 forEach JSTL 标记。 servlet 工作正常,并且正在生成两个产品的列表。但是,此信息不会出现在最终的 HTML 中。

如果我打电话:

http://localhost:8080/JavaIntoJSP/products 

输出不错。

如果我打电话:

http://localhost:8080/JavaIntoJSP/default.jsp 

输出丢失

代码如下(default.jsp):

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

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

<!DOCTYPE html>
<html>
    <head>
        <title>default</title>
    </head>
    <body>  
        <form action="products" method="get">
        <table>
            <c:forEach items="${products}" var="product" >
                <tr> 
                    <td><c:out value="${product.name}" /></td> 
                    <td><c:out value="${product.price}" /></td>
                    <td><c:out value="${product.category}" /></td>
                    <td><c:out value="${product.units}" /></td> 
                </tr>
            </c:forEach>
        </table>
        </form>
        <h1>try again</h1>

    </body>
</html>

Servlet 有以下内容

@WebServlet("/products")
public class ProductServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Product> products = new ArrayList<Product>();
        Product prod1   = new Product();
        prod1 = prod1.returnProduct();
        products.add(prod1);
        Product prod2   = new Product();
        prod2 = prod2.returnProduct();
        prod2 = prod2.returnProduct();
        products.add(prod2);


        request.setAttribute("products", products);
        request.getRequestDispatcher("default.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }    
}

谁能告诉我为什么在 JSP 上找不到这些产品?

【问题讨论】:

  • 尝试显示页面时,浏览器地址栏中显示的 URL 是什么?

标签: jsp servlets foreach jstl


【解决方案1】:
http://localhost:8080/JavaIntoJSP/products

它调用ProductServlet,其中products列表被填充并转发到default.jsp

http://localhost:8080/JavaIntoJSP/default.jsp 

如果您直接调用default.jsp,那么products 列表将是null & 因此您在屏幕上看不到任何输出。

更新

从评论中回答您的问题

不要在 JSP 中编写 scriptlet &lt;% %&gt;,因为在 10 年内不应在 JSP 中使用 scriptlet。学习 JSP ELJSTL,并将 servlet 用于 Java 代码。 How to avoid Java Code in JSP-Files?

使用empty operator 检查products 列表是否为null 并且为空。 EL中没有if-else,所以必须使用&lt;c:choose&gt;&lt;c:when&gt;...

<c:choose>
  <c:when test="${!empty products}">
     //print the list 
  </c:when>
  <c:otherwise>
     //call the servlet
    <jsp:forward page="products"></jsp:forward> 
  </c:otherwise>
</c:choose>  

相关

【讨论】:

  • 感谢 Aniket 和 AdolAurion。问题是我没有从 default.jsp 调用 Servlet,我已经修改了我的代码以包含对 servlet 的条件调用。如果有人知道一种更清洁的方法来做到这一点,我会很高兴听到。
【解决方案2】:

http://localhost:8080/JavaIntoJSP/default.jsp 不调用 servlet。因此,您不会将“产品”添加到请求的属性中。

你应该检查“default.jsp”是否有“products”可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2011-03-03
    • 1970-01-01
    • 2015-09-22
    相关资源
    最近更新 更多