SpringMVC语言环境不会切换语言

时间:2018-04-10 04:20:04

标签: spring model-view-controller

我在SpringMVC中面临语言区域设置更改的问题。当我打开fr等新语言时,浏览器会显示参数名称。但是,它并没有改变语言。我还用$ {pageContext.response.locale}测试。它没有改变。在我的代码下面。

spring-web-servlet.xml



<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

  <context:component-scan base-package="com.phyow.web.controller" /> 	
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/views/jsp/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
	
	 <!-- Spring Internationalization -->
    
	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
		<property name="defaultLocale" value="en" />
	</bean>
	
	<mvc:interceptors>
	<bean id="localeChangeInterceptor"	class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
		<property name="paramName" value="lang" />
	</bean>
	</mvc:interceptors>
	
	<!-- Register the bean 
   <bean class="com.phyow.web.controller.HelloController"></bean> 	-->
	<!-- Register the welcome.properties --> 
   <bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename" value="i18n/messages" /> 
	</bean>
		
	<!--Closed Spring Internationalization  -->
	
	<mvc:resources mapping="/resources/**" location="/resources/" />
 
 	<mvc:annotation-driven />  
	
</beans>
&#13;
&#13;
&#13;

HelloController.java

&#13;
&#13;
package com.phyow.web.controller;

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String printWelcome() {
		System.out.println("hello world/");
		
		return "hello";

	}
	@RequestMapping(value = "/contact", method = RequestMethod.GET)
	public String Contact() {
		System.out.println("/contact");
		//model.addAttribute("message", "Spring 3 MVC Hello World");
		return "contact";

	}
}
&#13;
&#13;
&#13;

header.jsp中

&#13;
&#13;
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title><spring:message code="label.title" /></title>
        <meta name="description" content="">
        <meta name="author" content="">
        <meta name="keyword" content="html5, css, bootstrap, property, real-estate theme , bootstrap template">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700,800' rel='stylesheet' type='text/css'>

        <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
        <link rel="icon" href="favicon.ico" type="image/x-icon">

        <link rel="stylesheet" href="<c:url value="/resources/client/css/normalize.css" />" >
        <link rel="stylesheet" href="<c:url value="/resources/client/css/font-awesome.min.css" /> ">
        <link rel="stylesheet" href="<c:url value="/resources/client/css/fontello.css" /> ">       
        <link rel="stylesheet" href="<c:url value="/resources/client/css/bootstrap-select.min.css" /> "> 
        <link rel="stylesheet" href="<c:url value="/resources/client/css/bootstrap.min.css" /> ">
        <link rel="stylesheet" href="<c:url value="/resources/client/css/icheck.min_all.csss" /> ">
        <link rel="stylesheet" href="<c:url value="/resources/client/css/price-range.css" /> ">
        <link rel="stylesheet" href="<c:url value="/resources/client/css/owl.carousel.css" /> ">  
        <link rel="stylesheet" href="<c:url value="/resources/client/css/owl.theme.css" /> ">
        <link rel="stylesheet" href="<c:url value="/resources/client/css/owl.transitions.css" /> ">
        <!--  For Language Flag -->
         <link rel="stylesheet" href="<c:url value="/resources/client/css/flag-icon.min.css" /> ">
         <link rel="stylesheet" href="<c:url value="/resources/client/css/flag-icon.css" /> ">
        <!--  For Property -->
        <link rel="stylesheet" href="<c:url value="/resources/client/css/lightslider.min.css" />">
        
        <link rel="stylesheet" href="<c:url value="/resources/client/css/style.css" /> ">
        <link rel="stylesheet" href="<c:url value="/resources/client/css/responsive.css" /> ">
        <!--  For Submit property -->
        <link rel="stylesheet" href="<c:url value="/resources/client/css/wizard.css" /> "> 
        
        <link href="<c:url value="/resources/client/fonts/icon-7-stroke/css/pe-icon-7-stroke.css" /> " rel="stylesheet">
        <link href="<c:url value="/resources/client/fonts/icon-7-stroke/css/helper.css" /> " rel="stylesheet">
        <link href="<c:url value="/resources/client/css/animate.css" /> " rel="stylesheet" media="screen">
    </head>
    <body>

        <div id="preloader">
            <div id="status">&nbsp;</div>
        </div>
        <!-- Body content -->

        <div class="header-connect">
            <div class="container">
                <div class="row">
                    <div class="col-md-5 col-sm-8  col-xs-12">
                        <div class="header-half header-call">
                            <p>
                                
                               
                                Current Locale : ${pageContext.response.locale}
                            </p>
                        </div>
                    </div>
                    <div class="col-md-2 col-md-offset-5  col-sm-3 col-sm-offset-1  col-xs-12">
                        <div class="header-half header-social">
                            <ul class="list-inline">
                          
                                
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="200"><spring:message code="label.language" /> <b class="caret"></b></a>
                            <ul class="dropdown-menu">
                             <li>
                                    <div class="yamm-content">
                                        <div class="row">
                                            <div class="col-sm-3">                                                                                              
                                                	<li>                                                   	
                                                        <a href="?lang=en"><span class="flag-icon flag-icon-us"></span> <spring:message code="label.english" /></a>
                                                    </li>
                                                    <li>                                                   	
                                                       <a href="?lang=fr"><span class="flag-icon flag-icon-fr"></span><spring:message code="label.french" /></a>
                                                    </li>
                                                    <li>                                                   	
                                                        <a href="?lang=de"><span class="flag-icon flag-icon-de"></span><spring:message code="label.german" /></a>
                                                    </li>                                                                                                                                                   
                                            </div>                                           
                                        </div>
                                    </div>                                   
                             </li>
                            </ul>
</ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>    
&#13;
&#13;
&#13;

web.xml

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

  <display-name>Spring3 MVC Application</display-name>

  <servlet>
    <servlet-name>spring-web</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>spring-web</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-web-servlet.xml</param-value>
  </context-param>
  
   
</web-app>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

那是因为你还没有注册你的拦截器。

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

由于您使用<mvc:annotation-driven />,因此该bean定义无法执行任何操作。

您应该使用<mvc:interceptors />标记正确注册拦截器。

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

这将使用HandlerMapping标记配置的所有已配置<mvc:annotation-driven />实例注册您的拦截器。另请参阅参考指南中的Configuring Interceptors