【发布时间】:2025-12-05 22:55:01
【问题描述】:
我有一个标签需要动态命名的页面范围变量。
someTag.tag
<%@ tag language="java" pageEncoding="UTF-8" dynamic-attributes="expressionVariables" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="expression" required="true" type="java.lang.String" %>
<c:out value="${expression}" /> <%-- this is just an example, I use expressions differently, they are not jsp el actually --%>
及用法示例
<%@ taglib prefix="custom_tags" tagdir="/WEB-INF/tags/custom_tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="someAttr" value="someValue" />
<custom_tags:someTag expression="someMethod(#localAttr)" localAttr="${someAttr}" />
我需要将localAttr 放到标签的页面范围内,但是jstl <c:set var='${....}'... /> 不接受动态名称。
我目前使用以下脚本:
<c:forEach items="${expressionVariables}" var="exprVar">
<% jspContext.setAttribute(((java.util.Map.Entry)jspContext.getAttribute("exprVar")).getKey().toString(), ((java.util.Map.Entry)jspContext.getAttribute("exprVar")).getValue()); %>
</c:forEach>
有没有其他方法可以做到这一点?
【问题讨论】:
-
Dynamic variable names Java 的可能重复项
-
@Raedwald,这个问题与我的相差甚远
标签: java jsp jstl jsp-tags scriptlet