Spring MVC国际化:内容不会按照所选语言更改

时间:2019-02-17 17:23:24

标签: java spring spring-mvc java-ee internationalization

我正在尝试执行Spring MVC国际化演示程序,该程序将以三种不同的语言(即英语,意大利语和德语)在“登录”页面上显示用户名和密码。但是也单击其他语言,它仅以英语显示内容。请帮助我找出我在此程序中犯了什么错误。

web.xml:

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FirstSpringMVCAnnotations</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>  

</web-app>

spring-dispatcher-servlet.xml:

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

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

    <mvc:annotation-driven enable-matrix-variables="true" />

        <!-- For reading the properties files -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/message" />
    </bean>

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

    </mvc:interceptors>

        <!-- Cookie Resolver -->
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    </bean>

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

InternationalizationDemoController.java:

package com.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class InternationalizationDemoController {

    @RequestMapping(value="/login.html")
    public ModelAndView getAdmissionForm(){
        ModelAndView modelView = new ModelAndView("Login");
        modelView.addObject("headerMsg", "Please enter below details to login");
        System.out.println("Inside InternationalizationDemoController..");
        return modelView;
    }

}

Login.jsp:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

Select Language : <a href="?lang=en">English</a> | <a href="?lang=de">German</a> | <a href="?lang=it">Italian</a>

<form action="/FirstSpringMVCAnnotations/login.html">
  <div class="container">

    <h1 align="center">Login Page</h1>
    <p>${headerMsg}</p>
    <hr>

    <spring:message code="spring.username.label" />
    <input type="text" name="username"><br><br>

    <spring:message code="spring.password.label" />
    <input type="password" name="password"><br><br>

    <button type="submit" class="submitbtn">Submit</button>
    <button type="reset" class="resetbtn">Reset</button>
  </div>

</form>

</body>
</html>

我在/ Web-Inf文件夹中有message_en.properties,message_it.properties和message_de.properties文件。

message_en.properties

spring.username.label=User name
spring.password.label=Password

message_de.properties

spring.username.label=Nutzername
spring.password.label=Passwort

message_it.properties

spring.username.label=Nome utente
spring.password.label=Parola d'ordine

如下图所示,单击德语也会显示英语的用户名和密码标签。

enter image description here

1 个答案:

答案 0 :(得分:0)

You need to change <mvc:mapping path="/init" /> to <mvc:mapping path="/**" />.