2 xwh445311 xwh445311 于 2013.12.16 21:53 提问

spring mvc4 动态切换语言不起作用

我利用 SPRING4 MVC配置了动态切换语言,但是在页上点击中文、英文切换的时候不起作用。请高手看看

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value="text/plain;charset=UTF-8"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven> -->



    <!-- 启用Spring MVC注解功能 -->
    <mvc:annotation-driven/>

    <!-- 启动Srping组件自动扫描机制,Spring会自动扫描base-package包及其子包下面的类 -->
    <context:component-scan base-package="com.efs.business"/>

    <!-- 设置视图解析类,使用默认的JSTL解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp"/>
        <property name="suffix" value=".jsp"/>
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    </bean>

    <!-- 系统配置拦截器 -->
    <mvc:interceptors>

        <!-- 切换语言拦截器 -->
        <mvc:interceptor>
            <mvc:mapping path="/*"/>
            <ref bean="localeChangeInterceptor"/>
        </mvc:interceptor>

    </mvc:interceptors>

    <!-- 配置国际化资源文件,包括动态切换 START -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="com.efs.resource.Message"/>
    </bean>

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

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="locale"/>
    </bean>
    <!-- 配置国际化资源文件,包括动态切换 END -->



</beans>

Controller:

    package com.efs.business.locale;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.LocaleResolver;

@Controller
@RequestMapping("/locale")
public class SysLocaleController
{
    @Autowired
    private LocaleResolver localeResolver;

    @RequestMapping("/changeLocale")
    public String changeLocale(String locale,HttpServletRequest request,HttpServletResponse response)
    {
        Locale l = new Locale(locale);
        localeResolver.setLocale(request, response, l);
        return "redirect:/demo/index.do";
    }
}

页面:

<a href="<%=basePath%>/locale/changeLocale.do?locale=zh_CN">中文</a> |
<a href="<%=basePath%>/locale/changeLocale.do?locale=en_US">English</a>

<hr/>
<form action="<%=basePath%>/demo/login.do" method="post" id="form">
    <!-- 页面上使用资源国际化 -->
    <sf:message code="demo.label.userName" />:<input type="text" name="userName" value="${form.userName }" />
    <br/>
    <sf:message code="demo.label.password" />:<input type="password" name="password" /><br/>

3个回答

xuenianxiang
xuenianxiang   2014.04.14 10:20

请问你的问题解决了吗?如何解决?

yaerfeng
yaerfeng   2014.04.27 19:25

请参考代码: 基于浏览器首选语言的springmvc和freemarker国际化配置的实现,下载地址:http://www.zuidaima.com/share/1634244049275904.htm

u010799932
u010799932   2016.08.15 16:13

redirect 改成 forward 试试

Csdn user default icon
上传中...
上传图片
插入图片