Java 如何在 scriptlet 中使用 JSTL 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1969281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i use JSTL variable in scriptlet?
提问by Rakesh Juyal
I have to access the JSTL variable which is calculated inside the iterator.
Excerpt of code:
我必须访问在迭代器内计算的 JSTL 变量。
代码摘录:
<c:forEach var="resultBean" items="${resultList}" varStatus="status">
card: ${resultBean.cardNum}
</c:forEach>
i would like to access ${resultBean.cardNum}
in the scriptlet code. what i am doing right now is:
我想${resultBean.cardNum}
在脚本代码中访问。我现在正在做的是:
<c:forEach var="resultBean" items="${resultList}" varStatus="status">
card: ${resultBean.cardNum}
<c:set var="currentCardNum">${resultBean.cardNum}</c:set>
<%out.write( StringUtils.mask( (String)pageContext.getAttribute("currentCardNum") ) );%>
</c:forEach>
I want to skip 3rd line where i am setting the variable in pageContext. Is it possible to achieve the same result without setting it? Or is there other way round which i can use?
我想跳过我在 pageContext 中设置变量的第三行。是否可以在不设置的情况下达到相同的结果?或者我可以使用其他方法吗?
采纳答案by David Rabinowitz
You can try the following:
您可以尝试以下操作:
<%
ResultBean resultBean = (ResultBean) pageContext.getAttribute("resultBean");
out.write( StringUtils.mask( resultBean.getCardNum() ) );
%>
BTW - you can add another method to resultBean - getMaskedCardNum()
, and then just put in the page ${resultBean.maskedCardNum}
which is more readable.
顺便说一句 - 您可以向 resultBean - 添加另一个方法getMaskedCardNum()
,然后将其放入${resultBean.maskedCardNum}
更具可读性的页面中。
回答by Bozho
I'd advise creating a custom JSTL function (check thisfor example), so that you can omit the scriptlet. So instead of the ugly
我建议创建一个自定义的 JSTL 函数(例如检查这个),这样你就可以省略 scriptlet。所以,而不是丑陋
<%out.write( StringUtils.mask( (String)pageContext.getAttribute("currentCardNum") ) );%>
you will have something like:
你会有类似的东西:
<c:out value="${fnPrefix:maskString(currentCardNum)}" />