码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA记录-SpringMVC国际化配置

时间:2017-10-18 11:14:10      阅读:171      评论:0      收藏:0      [点我收藏+]

标签: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=money

messages_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国际化配置

标签:toe   href   map   lan   tps   名称   default   英文   efi   

原文地址:http://www.cnblogs.com/xinfang520/p/7685484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!