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.
<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
<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?
To use Spring's form tags in our web application, we need to include the below directive at the top of the JSP page. This form tag library comes bundled in spring-webmvc. jar and the library descriptor is called spring-form. tld.
The spring:message tag provides you with internationalization support using Spring's MessageSource concept. The MessageSource is an interface providing functionality for retrieving messages. It closely resembles JSTL's fmt:message -tag, however, the MessageSource classes can be integrated with the Spring context.
The commandName attribute is the most important attribute in the form tag, which specifies the model attribute name that contains a backing object and the properties of this object will be used to populate the generated form.
Use <spring:message>
to store the value in a var, then reference that var using EL, e.g.
<spring:message code="general.submit" var="submitText"/>
<input type="submit" name="login" value="${submitText}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With