2
我有一个需要动态命名页面作用域变量的标记。用动态名称创建变量
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>
是否还有其他的方法来做到这一点?
[动态变量名称Java](http://stackoverflow.com/questions/5805843/dynamic-variable-names-java) – Raedwald 2014-09-02 11:45:24
@Rededwald,这个问题是远不及我的 – 2014-09-02 12:16:00