java 将另一个 JSTL 标记的结果分配为一个 JSTL 标记的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2945223/
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
Assigning outcome of another JSTL tag as value of one JSTL tag
提问by NoozNooz42
I've got this, which is working:
我有这个,这是有效的:
<c:choose>
<c:when test="${sometest}">
Hello, world!
</c:when>
<c:otherwise>
<fmt:message key="${page.title}" />
</c:otherwise>
</c:choose>
And I want to change it to this:
我想把它改成这样:
<c:choose>
<c:when test="${sometest}">
<c:set var="somevar" scope="page" value="Hello, world!"/>
</c:when>
<c:otherwise>
<c:set var="somevar" scope="page" value="<fmt:message key="${page.title}">"
</c:otherwise>
</c:choose
But of course the following line ain't correct:
但当然以下行是不正确的:
<c:set var="somevar" scope="page" value="<fmt:message key="${page.title}">"
How can I assign to the somevarvariable the string resulting from a call to fmt:message?
如何将调用 fmt:message 产生的字符串分配给somevar变量?
回答by BalusC
The fmt:messagehas a varattribute as well which does effectively what you want.
该fmt:message有一个var属性,以及有效地你想什么呢。
<fmt:message key="${page.title}" var="somevar" />
That's all. Bookmark the JSTL tlddoc, it may come in handy.
就这样。将JSTL tlddoc 加入书签,它可能会派上用场。
回答by Timo Westk?mper
It is also possible to specify the value to set using the contents of the body, rather than through the value attribute:
也可以使用正文的内容指定要设置的值,而不是通过 value 属性:
<c:set var="somevar" scope="page">
<fmt:message key="${page.title}"/>
</c:set>
回答by André van Toly
You'll have to do with:
你将不得不做:
<c:set var="title"><fmt:message key="${page.title}"></c:set>
<c:set var="somevar" scope="page" value="${title}" />
Since you can't use <fmt:message .. />on that spot is my experience, has to do with nesting like you suggested. Or go with @balusc suggestion ;-)
由于您不能<fmt:message .. />在那个地方使用是我的经验,因此与您建议的嵌套有关。或者使用@balusc 的建议 ;-)

