SpringMVC國際化配置


一、什么是國際化:

  國際化是設計軟件應用的過程中應用被使用與不同語言和地區
  國際化通常采用多屬性文件的方式解決,每個屬性文件保存一種語言的文字信息,
    不同語言的用戶看到的是不同的內容

二、springmvc.xml的配置

<?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: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.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.ssm.controller" />
    <mvc:annotation-driven />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 存儲區域設置信息 SessionLocaleResolver類通過一個預定義會話名將區域化信息存儲在會話中 從session判斷用戶語言defaultLocale 
        :默認語言 -->
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="zh_CN" />
    </bean>

    <!-- 國際化資源文件 messageSource配置的是國際化資源文件的路徑, classpath:messages指的是classpath路徑下的 
        messages_zh_CN.properties文件和messages_en_US.properties文件 設置“useCodeAsDefaultMessage”,默認為false,這樣當Spring在ResourceBundle中找不到messageKey的話,就拋出NoSuchMessageException, 
        把它設置為True,則找不到不會拋出異常,而是使用messageKey作為返回值。 -->
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="defaultEncoding" value="UTF-8" />
        <property name="useCodeAsDefaultMessage" value="true" />
        <property name="basenames">
            <list>
                <value>classpath:resource/message</value>
            </list>
        </property>
    </bean>
    <!--該攔截器通過名為”lang”的參數來攔截HTTP請求,使其重新設置頁面的區域化信息 -->
    <mvc:interceptors>
        <bean id="localeChangeInterceptor"
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="lang" />
        </bean>
    </mvc:interceptors>
</beans>

三、國際化語言屬性文件

我的路徑是這樣,其他的都可以

1. message_en_US.properties

TITLE = BEGIN TO START
USERNAME = UserName
PASSWORD = PassWord
LOGIN = Login

2. message_zh_CN.properties

TITLE = 開始冒險之旅
USERNAME = 賬號:
PASSWORD = 密碼:
LOGIN = 登錄

四、編寫Controller

@Controller
public class NewsController {
    
    @RequestMapping(value = "/getLogin.html")
    public String getLogin(){
        return "result";
    }
}

五、前端編寫並測試

1. 文件位置

2. index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="getLogin.html?lang=zh_CN">中文</a>
<br />
<a href="getLogin.html?lang=en_US">英文</a>
</body>
</html>

3. result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
   <div class="login">
      <h1><spring:message code="TITLE" /> </h1>
      <form action="test.do" method="post">
         <input type="text" name="name" placeholder=<spring:message code="USERNAME" /> required="required" value="" />
         <input type="password" name="password" placeholder=<spring:message code="PASSWORD" /> required="required" value="" />
         <button type="submit" class="btn btn-primary btn-block btn-large"><spring:message code="LOGIN" /> </button>
      </form>
   </div>
</body>
</html>

4. 測試

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM