在jsp中显示数据 [英] Display data in jsp
问题描述
我是jsp的新手,遇到一个问题.我需要创建jsp页面,该页面显示servlet中的数据. Servlet代码:
I am new in jsp and faced with one problem. I need to create jsp page, that displays data from servlet. Servlet code:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
CustomersDao customersDao = new CustomersDaoImpl();
List<Customers> custList = customersDao.getAllCustomers();
request.setAttribute("customersList", custList);
request.getRequestDispatcher("/test.jsp").forward(request, response);
}
jsp页面代码:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Test Page</title>
</head>
<body>
<%-->
<table border="1">
<tr>
<td>${requestScope['customers'].name}</td>
<td>${requestScope['customers'].lastname}</td>
<td>${requestScope['customers'].totalAmount}</td>
</tr>
</table>
<--%>
<table border="1">
<c:forEach var="element" items="${requestScope['customersList']}">
<tr>
<td><c:out value="${element.name}" /> TEST.name</td>
<td>${element.lastname} TEST.lastname</td>
<td>${element.totalAmount} TEST.totalAmount</td>
<td> ololo </td>
</tr>
</c:forEach>
</table>
</body>
</html>
当我将单个对象发送到jsp时,它可以正常工作(代码的注释部分).但是,当我尝试发送列表时,我无法分离对象,并且浏览器仅向我显示测试消息.那我该如何解决呢?
When I send single object to jsp it's work normally (commented part of code). But when I try to send list, I can not separate object and browser shows me only test messages. So how do I fix it?
推荐答案
当我将单个对象发送到jsp时,它可以正常工作(代码的注释部分).但是当我尝试发送列表时,我无法分离对象,浏览器仅显示测试消息.
When I send single object to jsp it's work normally (commented part of code). But when I try to send list, I can not separate object and browser shows me only test messages.
您忘记了包括核心标签库.这就是为什么<c:forEach>
不起作用,但是如果您仅传递单个对象也可以起作用的原因.
You have forgotten to include the core tag library. that's why <c:forEach>
is not working but it works if you pass just single object.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
这篇关于在jsp中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!