用jstl在jsp接收后台传输数据并显示

本文介绍了如何在JSP中利用JSTL处理后台传递的数据。首先,需要将standard.jar和jstl.jar添加到项目的lib目录并加入到build path。接着,配置web.xml以启用JSTL支持。在JSP页面中,通过引入核心标签库,使用c:forEach标签迭代集合数据,var定义变量名,items指定要遍历的集合对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先将数据库的数据获取,用session将数据存起来。
HttpSession session = request.getSession();
session.setAttribute("userDetail", userDetailDao.userDetail());
public class UserDetailAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public UserDetailAction() {
        super();

    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 UserDetailDao userDetailDao = new UserDetailDao();
		 HttpSession session = request.getSession();
		 session.setAttribute("userDetail", userDetailDao.userDetail());
		response.sendRedirect("managerForUserDetail.jsp");
	
  		
	}

用jstl先导入两个jar包(standard.jar和jstl.jar),放在lib文件夹下并add build path

在web.xml配置(来自菜鸟)

<jsp-config>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
	<taglib-location>/WEB-INF/fmt.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri>
	<taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
	<taglib-location>/WEB-INF/c.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri>
	<taglib-location>/WEB-INF/c-rt.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
	<taglib-location>/WEB-INF/sql.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri>
	<taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
	<taglib-location>/WEB-INF/x.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri>
	<taglib-location>/WEB-INF/x-rt.tld</taglib-location>
	</taglib>
	</jsp-config>


在jsp中引用核心标签库的语法引用核心标签库的语法

注释部分是没有使用jstl时,在jsp页面添加Java代码,

使用jstl的c:forEach是迭代一个集合中的对象,var表示变量名,items是将被循环的集合。

<%@ page language="java" contentType="text/html; charset=GBK"
 import="java.util.*,com.qingzheng.entity.User,com.qingzheng.servlet.UserDetailAction,
	com.qingzheng.dao.UserDetailDao"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!-- jstl的核心标签 -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css">
<style>
	th,td{
		width:200px;
	}
</style>
</head>
<body>
	<h1>User Infomation</h1>
		<table class="table table-striped">
		<thead>
		<tr>
			<th>UserAccount</th>
			<th>User name</th>
			<th>Password</th>
			<th>Sex</th>
			<th>Telephone Number</th>
			<th>Operation</th>
		</tr>
		</thead>
		</table>
	<table class="table table-striped">	
	<%
	/*	 ArrayList<User> users = (ArrayList<User>) session.getAttribute("userDetail");
  		 for(User user:users){
  			int userid = user.getUserid();
  			 String username = user.getUsername();
  			 String password= user.getPassword();
  			 String sex = user.getSex();
  			 String tel = user.getTel();*/
	%>	 
		<c:forEach var="user" items="${userDetail}">
			  <tr>
				<td><c:out value="${user.userid}"></c:out> </td>
				<td><c:out value="${user.username}"></c:out></td>
				<td><c:out value="${user.password}"></c:out></td>
				<td><c:out value="${user.sex}"></c:out></td>
				<td><c:out value="${user.tel}"></c:out></td>
				<td>
					<a href="ManagerCheckUserAllDetailtAction?userid=${user.userid}">详情</a>
					<a href="ManagerChangeUserDetailAction?userid=${user.userid}">修改</a>
					<a href="">删除</a>
				</td>
			</tr>
	</c:forEach>

	<%	
  		// }		
	%>
	</table>

</body>
</html>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值