0

感谢任何帮助,因为我在将这些东西放在一起时迷失了方向,并且 JSP 没有按照 URL 参数中传递的语言显示 spring 消息。当在welcome.jsp 中选择不同的语言时,JSP 没有通过SessionLocaleResolver 获取SessionLocale。我在做什么/出了什么问题。

我正在使用 Spring 3.1.1。提前致谢

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:view-controller path="/views/**" view-name="secure/index" />

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="en" />
</bean>

<!-- URL interceptor -->
<bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
</bean>

<bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor" />
    </property>
</bean>

<!-- Application Message Bundle -->
<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

我的欢迎控制器

@Controller
public class WelcomeController {

@Autowired
private MessageSource messageSource;

public void setMessageSource(MessageSource messageSource) {
    this.messageSource = messageSource;
}

@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String welcome(Model model, HttpServletRequest request, HttpServletResponse response) {
    String lang = request.getParameter("lang");
    if (lang == null) {
        lang = "en_US";
    }
    /**
     * Get Message having PARAMETERS in the message {0}, {1}.....{n}
     */
    model.addAttribute(
            "welcome_back_msg",
                messageSource.getMessage("message_welcome_back_user", new Object[] { "(USER)",
                        new DateTime(), new DateTime() }, new Locale(lang)));
    return "welcome";
}
}

消息文件放在/WEB-INF/messages/messages_en.properties

welcome_springmvc=Welcome to Spring MVC
message_welcome_back_user=Welcome back {0} Your Last Successful : {1} Failed : {2}

messages_zh.properties

welcome_springmvc=\u6B22\u8FCE\u5230 Spring \u7684 MVC

message_welcome_back_user=\u6B22\u8FCE\u56DE\u6765 {0} \u4E0A\u6B21\u6210\u529F:{1} \u5931\u8D25:{2}

messages_fr.properties

welcome_springmvc=Bienvenue \u00E0 Spring MVC

message_welcome_back_user=Bienvenue \u00E0 nouveau {0} Votre r\u00E9ussite Derni\u00E8re : {1} manqu\u00E9 : {2}

--- 欢迎.jsp

Spring Message from Property:<b><spring:message code="welcome_springmvc" text="default text" /></b>
Message from Controller:<b>${welcome_back_msg}

JSP 图像 在此处输入图像描述

你可以看到来自 Property 的 Spring Message 总是回退到 locale = en。感谢在这方面的任何帮助。

4

1 回答 1

0

仅实例化 LocaleChangeInterceptor Bean 是不够的。与所有拦截器一样,它必须注册为拦截器:

所以把 bean def 放在一个<mvc:interceptors>标签里:

<!-- URL interceptor -->
<mvc:interceptors>
     <bean id="localeChangeInterceptor"
       class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
       <property name="paramName" value="lang" />
     </bean>
</mvc:interceptors> 

顺便说一句:看看jsp spring消息标签

于 2012-09-14T06:14:53.743 回答