JAVA记录-SpringMVC国际化配置
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国际化配置的更多相关文章
- JAVA记录-SpringMVC+Mybatis几个核心注意的地方
1.DispatcherServlet -- 前置控制器 DispatcherServlet是一个Servlet,所以可以配置多个DispatcherServlet. DispatcherServ ...
- springMVC国际化配置和使用
下面是基于session的,springMVC国际花的一个例子: 需求是 输入url:展示中文界面 http://localhost:8080/MySSM/user?lang=zh 输入url: 展 ...
- SpringMVC国际化配置
一.什么是国际化: 国际化是设计软件应用的过程中应用被使用与不同语言和地区 国际化通常采用多属性文件的方式解决,每个属性文件保存一种语言的文字信息, 不同语言的用户看到的是不同的内容 二.spr ...
- JAVA记录-SpringMVC集成redis
1.redis.properties #主机地址 redis.host=127.0.0.1 #端口号 redis.port=6379 #当池内没有返回对象时,最大等待时间毫秒数 redis.pool. ...
- JAVA记录-SpringMVC scope属性的两种模式
singleton作用域:当把一个Bean定义设置为singleton作用域是,Spring IoC容器中只会存在一个共享的Bean实例,并且所有对Bean的请求,只要id与该Bean定义相匹配,则只 ...
- JAVA记录-maven JDK配置和常用操作
1.pom.xml加入(JDK编译器配置) <build> <finalName>项目名</finalName> <plugins> <plugi ...
- Java记录-SpringMVC整合Echarts画地图加散点图
1.搭建eclipse+JDK+Maven+SpringMVC+Spring+Mybatis后台架构,详细就不过多阐述了 2.下载百度eharts插件:http://echarts.baidu.com ...
- springmvc国际化 基于请求的国际化配置
springmvc国际化 基于请求的国际化配置 基于请求的国际化配置是指,在当前请求内,国际化配置生效,否则自动以浏览器为主. 项目结构图: 说明:properties文件中为国际化资源文件.格式相关 ...
- springmvc国际化 基于浏览器语言的国际化配置
当前标签: springmvc springmvc国际化 基于浏览器语言的国际化配置 苏若年 2013-10-09 13:03 阅读:305 评论:0 SpringMVC中应用Ajax异步通讯 ...
随机推荐
- Node 连接池pool
//1:加载相应的模块 http url fs mysqlconst http = require("http");const url = require("url&qu ...
- Node require
var user = require("./module_user");//使用模块 module_userconsole.log(user.userCount);user.use ...
- mysql常用增删改查命令(纯纪录.orm用得基本功都没了。)
更新表数据: update table_name set xxx=xxx where condition; 增加字段: alter table table_name add field type ot ...
- 理解 Delphi 的类(十) - 深入方法[18] - 在接口区声明的方法都相当于提前声明了
//要点18: 如果函数在接口区定义了, 就无需用 forward 提前声明了 unit Unit1; interface uses Windows, Messages, SysUtils, Va ...
- maven依赖有一个步长原则 如果a 对 b和c都有依赖 如果b的步长近则使用b的
maven依赖有一个步长原则 如果a 对 b和c都有依赖 如果b的步长近则使用b的
- 最新更新的Windows 10切换蓝牙连接开关会导致蓝屏死机
目前已经升级到 Windows 10 Version 1809 版的用户们正在努力发掘新版本还没有被公开发现的错误问题. 比如此前的.ZIP 格式的压缩包解压时无法正常替换,微软已承认该问题并称将在后 ...
- python中 Lambda,Map,Filter,Itertools,Generator高级函数的用法
Lambda 函数 Lambda 函数是一种比较小的匿名函数--匿名是指它实际上没有函数名. Python 函数通常使用 def a_function_name() 样式来定义,但对于 lambda ...
- nginx-日志统计
#!/bin/bash fd=/tmp/log# pv 点击量echo "###################点击量 --$pv-- #########################&q ...
- Luogu4238 【模板】多项式求逆(NTT)
http://blog.miskcoo.com/2015/05/polynomial-inverse 好神啊! B(x)=B'(x)·(2-A(x)B'(x)) 注意ntt的时候防止项数溢出,即将多项 ...
- POJ2492-A Bug's Life-并查集扩展应用
维护一个relation数组,保留着此元素和根元素之间的性别关系.之后就可以判断gay了. #include <cstdio> #include <algorithm> #in ...