Spring FORM 标签库 - 单选按钮,从消息源加载其 LABEL 属性

发布于 2024-12-09 20:07:43 字数 997 浏览 0 评论 0 原文

我正在开发一个基于 spring MVC 的应用程序,并使用 spring 标签库进行表单数据绑定。我很高兴地使用了该标签,直到我意识到与各个无线电关联的标签不是从消息源中获取的,而是我将它们硬编码到了 JSP 中。我之前不知何故没有注意到,所以现在我想解决这个问题,在这里我遇到了一个愚蠢的小问题,我希望你能够帮助我。

好的,这可行:

<form:radiobutton path="metric" value="0" label="inches" />
<form:radiobutton path="metric" value="1" label="centimeters" />

这不行。

<form:radiobutton path="metric" value="0" label="<fmt:message key="label.calculator.units.imperial" />" />
<form:radiobutton path="metric" value="1" label="<fmt:message key="label.calculator.units.metric" /> " />

问题是 fmt:message 标签嵌套在 format:radiobutton 标签内。我在堆栈跟踪中得到 Caused by: org.apache.jasper.JasperException: /WEB-INF/jsp/calculator/calculator.jsp(15,100) PWC6212: equal symbol expected

有没有其他方法可以使单选按钮的标签从消息源加载?我想我可以使用 首先将消息源中的消息文本设置为变量,然后将 ${varname} 设置为 < code>label="" 属性...但这似乎有点啰嗦。有什么建议吗?

I am working on a spring MVC-based application and use the spring tag library for form data binding. I happily used the tag until I realised that the labels associated with individual radios were not picked up from the message source but I had them hard-coded in the JSP. I somehow failed to notice before, so now I was like going to fix this and here I encounter a silly little problem, which I hope you will be able to help me with.

Ok, this works:

<form:radiobutton path="metric" value="0" label="inches" />
<form:radiobutton path="metric" value="1" label="centimeters" />

This does not.

<form:radiobutton path="metric" value="0" label="<fmt:message key="label.calculator.units.imperial" />" />
<form:radiobutton path="metric" value="1" label="<fmt:message key="label.calculator.units.metric" /> " />

The problem is the nesting of the fmt:message tag within the format:radiobutton tag. I get Caused by: org.apache.jasper.JasperException: /WEB-INF/jsp/calculator/calculator.jsp(15,100) PWC6212: equal symbol expected in the stack trace.

Is there any other way to make the radio buttons' labels to load from the message source? I guess I could use <c-rt:set> to first set the message text from the message source into a variable and then ${varname} it into the label="" attribute... but that seems a little long-winded. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始终不够 2024-12-16 20:07:43

没有经常使用 标签,但据我所知,你别无选择,只能将消息设置为变量,然后像 label= 一样使用它“${varname}”

另一种选择是放弃标签属性并仅手动打印标签,例如:

<spring:message code="label.calculator.units.imperial" /><form:radiobutton path="metric" value="0" />
<spring:message code="label.calculator.units.metric" /><form:radiobutton path="metric" value="1" />

当然,HTML 结果会有所不同。您必须自己控制标签,而不是生成

Haven't used the <form:radiobutton> tag very often, but as far as I know you have no choice but to set the message into a variable and then use it like label="${varname}".

One other option would be to loose the label attribute and just print a label by hand, something like:

<spring:message code="label.calculator.units.imperial" /><form:radiobutton path="metric" value="0" />
<spring:message code="label.calculator.units.metric" /><form:radiobutton path="metric" value="1" />

Off course the HTML result will differ. You'll have to control the label yourself instead of the radio button tag generating a <label for="...".

似梦非梦 2024-12-16 20:07:43

可以设置 spring:message 标签的 'var' 属性:

<spring:message var="inches" code="label.calculator.units.imperial"/>
<spring:message var="centimeters" code="label.calculator.units.imperial"/>

<form:radiobutton path="metric" value="0" label="${inches}"/>
<form:radiobutton path="metric" value="1" label="${centimeters}"/>

It is possible, set the 'var' attribute of the spring:message tag:

<spring:message var="inches" code="label.calculator.units.imperial"/>
<spring:message var="centimeters" code="label.calculator.units.imperial"/>

<form:radiobutton path="metric" value="0" label="${inches}"/>
<form:radiobutton path="metric" value="1" label="${centimeters}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文