JAVA記錄-SpringMVC國際化配置

來源:互聯網
上載者:User

標籤:toe   href   map   lan   tps   名稱   default   英文   efi   

1.搭建SpringMVC架構,不過多闡述

2.spring-mvc.xml加入以下配置:

 <!-- 國際化資源配置,資源檔綁定器-->    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">        <!-- 國際化資源檔配置,指定properties檔案存放位置 -->        <property name="basename" value="classpath:messages/messages" />        <!-- 如果在國際化資源檔中找不到對應代碼的資訊,就用這個代碼作為名稱  -->                       <property name="useCodeAsDefaultMessage" value="true" />    </bean>    <!-- 動態切換國際化 ,國際化放在session中 -->    <mvc:interceptors>      <!-- 國際化操作攔截器 如果採用基於(請求/Session/Cookie)則必需配置 -->     <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />      </mvc:interceptors>       <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />    

3.在resources建立目錄messages,然後建立以下兩個檔案:

messages_en_US.properties:money=moneymessages_zh_CN.properties:money=金錢

4.後台控制器編寫

package com.net.xinfang.controller;import java.util.Locale;import javax.servlet.http.HttpServletRequest;import org.springframework.context.i18n.LocaleContextHolder;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.servlet.i18n.SessionLocaleResolver;@Controller@RequestMapping(value = "/cte")public class CntoEnController {        @RequestMapping(value="/getcte", method = {RequestMethod.GET})    public String test(HttpServletRequest request,Model model, @RequestParam(value="langType", defaultValue="zh") String langType){                        if(langType.equals("zh")){                Locale locale = new Locale("zh", "CN");                 request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);             }            else if(langType.equals("en")){                Locale locale = new Locale("en", "US");                 request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);            }            else{                 request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());            }             return "cte";    }  }

5.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"><%@taglib prefix="spring" uri="http://www.springframework.org/tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>SpringMVC國際化</title></head><body> 選擇語言:<a href="${pageContext.request.contextPath}/cte/getcte?langType=zh">中文</a> | <a href="${pageContext.request.contextPath}/cte/getcte?langType=en">英文</a><br></br><spring:message code="money" /></body></html>

6.測試

 

JAVA記錄-SpringMVC國際化配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.