Custom tag that evaluates its body based on the variables of the tag

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-07 03:24:25

问题


Ok, now this is something for the hard core JSTL wizards I guess ;-)

I would like to have a tag whose body does not substitute the variables of the surrounding page. It should only take the variables into account that have been specified in the body of the tag or in the tag itself like this:

<c:set var="outsideVar" value="outside value"/>
<a:component>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
    <div data-component-id="9">
        <c:set var="componentId" value="9"/>
        <c:set var="path" value='${abc:getCurrentPath()}_${componentId}'/>
        <c:set var="resource" value='${abc:getResourceFromPath(path)}'/>
        <c:set var="val" value="${resource.getValue('paragraphValue')"/>
        <p>${fn:escapeXml(val)}</p>    
        ${outsideVar}
    </div>
</a:component>

The outsideVar variable should not be set and the body of the tag should behave like the content of a <jsp:include/>

This is a more specific question of this one: Is it possible to create a local page scope in a JSP? The link also explains the background of this question.


回答1:


Your custom tag can grab and remove all page attributes before evaluating the body, and then clear and restore afterwards.



来源:https://stackoverflow.com/questions/32404445/custom-tag-that-evaluates-its-body-based-on-the-variables-of-the-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!