xwh445311 2013-12-16 13:53 采纳率: 0%
浏览 8528

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条回答 默认 最新

  • 低调走红 2014-04-14 02:20
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试