在jsp中使用JSTL定义的变量

本文介绍了一种使用JSP与JSTL标签库进行页面内容展示的方法,特别是如何利用<c:forEach>标签循环遍历并显示数据集合中的每一项。此示例还展示了如何设置和获取页面上下文中的变量。

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

<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@taglib uri="http://beehive.apache.org/netui/tags-html-1.0" prefix="netui"%>
<%@taglib uri="http://beehive.apache.org/netui/tags-databinding-1.0" prefix="netui-data"%>
<%@taglib uri="http://beehive.apache.org/netui/tags-template-1.0" prefix="netui-template"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="com.bea.content.*" %>
<netui:html>
    
<head>
        
<netui:base/>
    
</head>
    
<netui:body>
        
<!-- for loop to display all the items -->   
    
<table border="1">
        
<tr>
            
<td>Title</td>
            
<td>Author</td>
            
<td>E-mail</td>
        
</tr>
        
<c:forEach var="node" items="${pageFlow.currentPage}">
        
<c:set var="nodeValue" scope="page" value="${node}"/>
        
<%
        Node node
= (Node)pageContext.getAttribute("nodeValue");
        
Property titleProp = node.getProperty("Title");
        
%>
        
<tr>
            
<td><c:if test="<%=titleProp != null %>"><%=titleProp.getValue().getStringValue()%></c:if></td>
            
<td></td>
            
<td></td>
        
</tr>
        
</c:forEach>
      
</table>
    
</netui:body>
</netui:html>
 
使用forEach中的变量node:
用<c:set var="nodeValue" scope="page" value="${node}"/>将变量"node" set 到pageContext中,再通过pageContext.getAttribute("nodeValue");获得。
<%
        Node node= (Node)pageContext.getAttribute("nodeValue");
        Property titleProp = node.getProperty("Title");
        %>

注意:在set 标签中,如果属性scope="request"即通过request.getAttribute("nodeValue");获得,如果scope="session"即通过request.getSession().getAttribute("nodeValue");获得。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值