java 在 Spring Web 应用程序中使用 spring:message 定义表单标签属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1560305/
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
Using spring:message to define form tag attribute in Spring web application
提问by simon
I'm developing a Java/Spring web application. The problem I'm currently facing is that I'd like to have message from message.resources shown as an attribute in an HTML.
我正在开发一个 Java/Spring Web 应用程序。我目前面临的问题是我希望将 message.resources 中的消息显示为 HTML 中的一个属性。
<input type="submit" name="login" value="login" />
So instead of the hardcoded value "login" I need to the value of
因此,我需要的不是硬编码值“登录”
<spring:message code="general.submit" />as the value attribute of that input tag. As the pages are all xml, it's no option to nest tags like
<spring:message code="general.submit" />作为该输入标签的值属性。由于页面都是 xml,因此无法嵌套标签,例如
<input type="submit" name="login" value="<spring:message code="general.submit" />" />
as it does not compile. I could, of course, read the value in the Java controller and use a JSTL variable to display the value, but I think it would be too hackish and complicated, especially for pages with large amount of submit buttons. Is there some kind of elegant way of accomplishing what I want to do?
因为它不编译。当然,我可以读取 Java 控制器中的值并使用 JSTL 变量来显示该值,但我认为这太hackish 和复杂,尤其是对于具有大量提交按钮的页面。是否有某种优雅的方式来完成我想做的事情?
回答by skaffman
Use <spring:message>to store the value in a var, then reference that var using EL, e.g.
使用<spring:message>存储在一个变种的值,然后使用EL引用该变种,例如
<spring:message code="general.submit" var="submitText"/>
<input type="submit" name="login" value="${submitText}" />

