感谢任何帮助,因为我在将这些内容放在一起时迷失了,并且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>
我的WelcomeController
@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}
--- welcome.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。 感谢这方面的任何帮助。
答案 0 :(得分: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消息标记