单击本地更改链接时,Spring MVC local不会更改

时间:2013-11-27 13:59:04

标签: spring spring-mvc

下面是我的Spring上下文文件,但是当尝试更改本地lang时,它没有切换....我在google上做了很多搜索+在stackoverflow上引用其他问题,但没有什么是有用的..大多数地方建议在bean标记localeChangeInterceptor周围添加<mvc:interceptors>标记。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="com.technicalkeeda.controller" />
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="fr" />
    </bean>
    <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.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>


</beans>

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>springexamples</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springexamples</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
     <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/springexamples-servlet.xml</param-value>
  </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file> index.jsp </welcome-file>
    </welcome-file-list>
</web-app>

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Title Here</title>
<link type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.css" rel="stylesheet"/>
</head>
<body>
    <div class="container-fluid">
        <h2>Select Your Language</h2>
        <div class="row-fluid">
            <div class="span12">
                <a href="<%=request.getContextPath() %>?lang=en" class="btn">English</a>&nbsp;<a href="<%=request.getContextPath() %>?lang=fr" class="btn">French</a>
            </div>
        </div>
        <div class="row-fluid">
            <div class="span12">
                <fieldset>
                <legend><spring:message code="employee.form.title" text="default text" /></legend>
                <form class="form-horizontal" method="post" action='employee/add.htm' name="employeeForm" id="employeeForm">
                    <div class="control-group">
                        <label class="control-label">First Name</label>
                        <div class="controls">
                            <input type="text" name="firstName" id="firstName" title="First Name" value="">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Last Name</label>
                        <div class="controls">
                            <input type="text" name="lastName" id="lastName" title="Last Name" value="">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Email</label>
                        <div class="controls">
                            <input type="text" name="email" id="email" title="Email" value="">
                        </div>
                    </div>
                    <div class="form-actions">
                        <button type="submit" class="btn btn-success">Submit</button>
                        <button type="button" class="btn">Cancel</button>
                    </div>
                </form>
                </fieldset>
            </div>
        </div>
    </div>      
</body>
</html>

2 个答案:

答案 0 :(得分:0)

您正在使用<mvc:annotation-driven />,那么您还必须使用命名空间来注册拦截器。使用<mvc:interceptors />注册您的拦截器,而不是声明(另一个未使用的)DefaultAnnotationHandlerMapping

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

由于使用了<context:annotation-config />,我们暗示了另一件事<context:component-scan />

答案 1 :(得分:0)

很抱歉,我没有足够的评分来评论,但正如M.Denim指出的那样,很可能你的index.jsp在WEB-INF文件夹之外。 您可以将index.jsp移动到WEB-INF / jsp /文件夹中,并在配置中添加静态视图以进行渲染,而无需使用显式控制器:

<mvc:view-controller path="/" view-name="index"/>

整个事情将是:

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    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">

<context:component-scan base-package="com.technicalkeeda.controller" />  

<!-- Turns on support for mapping requests to Spring MVC @Controller methods
     Also registers default Formatters and Validators for use across all @Controllers -->
<mvc:annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>

<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource 
requests to the container's default Servlet -->
<mvc:default-servlet-handler/>

<!-- Register "global" interceptor beans to apply to all registered HandlerMappings -->
<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/>
</mvc:interceptors>

<!-- Selects a static view for rendering without the need for an explicit controller -->
<mvc:view-controller path="/" view-name="index"/>

<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource" p:basenames="classpath:messages" p:fallbackToSystemLocale="false"/>

<!-- Store preferred language configuration in a cookie -->
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver" p:cookieName="locale"/>


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
</bean>

相关问题