JSTL 标签中的 Spring 消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17949382/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:07:42  来源:igfitidea点击:

Spring message in JSTL Tag

springjspspring-mvcjstl

提问by zmanc

According to this post from 3 years agothe only way to display a spring message in a jstl tag is to wrap it in a <c:set var="someVar">which does "work" but it seems very far from ideal.

根据3 年前的这篇文章,在 jstl 标签中显示 spring 消息的唯一方法是将它包装在一个<c:set var="someVar">可以“工作”但看起来很不理想的地方。

Fast forward 3 years, is this still the only way to handle this?

快进3年,这仍然是处理这个问题的唯一方法吗?

Here is my code

这是我的代码

Works, but not "ideal"

有效,但不是“理想的”

<c:set var="closeMessage">
    <spring:message code='lman.ilr.closeItemDetail'/>
</c:set>
<dsg:sidePanelContent closePanelText="${closeMessage}">

Doesn't work, returns a string of <spring:message code='lman.ilr.closeItemDetail'/>

不起作用,返回一串 <spring:message code='lman.ilr.closeItemDetail'/>

<dsg:sidePanelContent closePanelText="<spring:message code='lman.ilr.closeItemDetail'/>">

回答by JB Nizet

The spring message tag, just as fmt:message, has a var attribute that can be used to store the message instead of displaying it.

spring 消息标签,就像 一样fmt:message,有一个 var 属性,可以用来存储消息而不是显示它。

It always helps to read the documentation.

阅读文档总是有帮助

Also, your wrong message probably comes from forgettin to declare the spring taglib at the top of your JSP.

此外,您的错误消息可能来自忘记在 JSP 顶部声明 spring 标记库。

回答by Subin Chalil

In case for reference,

以供参考,

<c:choose>
  <c:when test="${serviceVO.id eq 0}"> 
     <spring:message code="label.service.createservice" var="buttonName"/> 
  </c:when> 
  <c:otherwise>
    <spring:message code="label.updateservice" var="buttonName"/> 
  </c:otherwise>
</c:choose>

<c:out value="${buttonName}"> //Prints the desired value...

回答by Franz Joseph

I think what you wanna do is.

我想你想做的是。

<spring:message code='lman.ilr.closeItemDetail' var="closeMessage"/>

Then

然后

<dsg:sidePanelContent closePanelText="${closeMessage}">