如何在jsp页面中使用jstl打印列表?

时间:2017-04-03 20:51:36

标签: jsp jstl

如何将用java编写的代码转换为JSP页面中的jstl标记。我想在JSP页面上打印它。  感谢

<% List<?> currentUserList = (List<?>)(session.getAttribute("currentSessionUser"));%>
     for(int i=0; i<currentUserList.size(); i++) {
                Object[] row = (Object[]) currentUserList.get(i);
                ContUser cont = (ContUser)row[0];
                Emp emp= (Emp)row[1];
                System.out.println("Cont ID:"+cont.getIdCont()+", USER:"+ cont.getUser()+ ", Emp ID:"+ emp.getIdEmp()+", Name:"+ emp.getName());

     }

1 个答案:

答案 0 :(得分:0)

创建一个jsp页面和这样的servlet。

public class MyServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   HttpSession session=req.getSession();
   session.setAttribute(currentUserList);
   this.getServletContext().getRequestDispatcher("/test.jsp").forward(req, resp);
}

和test.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<c:forEach items="${currentUserList}" var="i" varStatus="status">
       count ${status.count} index: ${status.index} current:      ${status.current} first: ${status.first} last: ${status.last} item: ${i}<br>
</c:forEach>

</body>
</html>

我希望这会对你有所帮助:)。