Spring locale messages don't work only within

2019-08-26 19:37发布

问题:

Hello I have a form input button as follows :

<form:input path="creationUsr.lastName" type="text" name="creationUsr.lastName" id="creationUsr.lastName" class="login-text" placeholder="<spring:message code='tile.form.lastName'/>" value=""/>

Here is my spring context locale setting :

  <mvc:interceptors>   

                <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>

        </mvc:interceptors>



        <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">

                <property name="defaultLocale" value="en" />

        </bean>

If I put this out of form tag :

<spring:message code='tile.form.lastName'/>

The value is as I should be.

I have a way around this but it's kind of ugly. I could revert to normal forms without using spring and regular <input type="text" name="lastname"> instead of <form:input .. and then serialize form to json and do a post with javascript.

I'm sure there must be a way to do this with spring mvc.

回答1:

You can't use a JSP tag inside an attribute of another JSP tag. To do what you want, save the message in a page-scope attribute, and use the EL to pass the message to the input tag:

<spring:message code='tile.form.lastName' var="lastNameMessage"/>
<form:input path="creationUsr.lastName" 
            type="text" 
            name="creationUsr.lastName" 
            id="creationUsr.lastName" 
            class="login-text" 
            placeholder="${lastNameMessage}" 
            value=""/>