我正在开发一个基于 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?
发布评论
评论(2)
没有经常使用
标签,但据我所知,你别无选择,只能将消息设置为变量,然后像label= 一样使用它“${varname}”
。另一种选择是放弃标签属性并仅手动打印标签,例如:
当然,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 likelabel="${varname}"
.One other option would be to loose the label attribute and just print a label by hand, something like:
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="..."
.可以设置 spring:message 标签的 'var' 属性:
It is possible, set the 'var' attribute of the spring:message tag: