我是 Spring MVC 的新手,我正在尝试在我的网站中添加 i18n。我有以下配置:
servlet-context.xml:
<beans:bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="language" />
</beans:bean>
<beans:bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<beans:property name="interceptors">
<beans:list>
<beans:ref bean="localeChangeInterceptor" />
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<beans:property name="basename" value="messages" />
</beans:bean>
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
和一个控制器:
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "home";
}
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String welcome(Locale locale, Model model) {
return "WelcomePage";
}
}
WelcomePage.jsp包含以下内容:
Language : <a href="?language=en">English</a>|<a href="?language=de_DE">German</a>
<h3>
<spring:message code="welcome.springmvc" text="default text" />
</h3>
当我点击上面显示的链接时,区域设置不会改变,尽管链接的 URL 变成../welcome?language=de_DE
了例如。如果我添加如下所示的控制器,WelcomePage.jsp 会正确更改语言环境。
public class WelcomeController extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("WelcomePage");
return model;
}
}
当我使用第一种类型的控制器(使用 RequestMappping)时,如何更改语言环境?