一、国际化是什么--I18N

即internationalization

首字母i-结束字母n之间有18个字母

特征:在程序不做修改的情况下,可以根据不同的语言环境显示相应内容

二、Java内置国际化

message_en_US.properties

demo = hello world!
userName = user name
password = password
welcome = welcome!

message_zh_CN.properties

demo = \u60A8\u597D\u4E16\u754C!
userName = \u7528\u6237\u540D
password = \u5BC6\u7801
welcome = \u6B22\u8FCE!

message.properties

demo = \u60A8\u597D\u4E16\u754C!
userName = \u7528\u6237\u540D
password = \u5BC6\u7801
welcome = \u6B22\u8FCE!

实现代码

<%@page import="java.util.ResourceBundle"%>
<%@page import="java.util.Locale"%>
<%@ 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>测试</title>
</head>
<body>
<h1>测试页面</h1>
<%
//获取当前已经安装的语言数组
Locale [] locals=Locale.getAvailableLocales();
//遍历数组
for(Locale local:locals){
//国家+国家代码
out.print("国家: "+local.getDisplayCountry()+" 国家代码:"+local.getCountry()+" ");
//语言+语言代码
out.println("语言: "+local.getDisplayLanguage()+" 语言代码:"+local.getLanguage()+"<hr/>");
}
//引用语言 此处可以更改
Locale ls=Locale.getDefault();
//使用哪个资源文件
ResourceBundle rb=ResourceBundle.getBundle("message",ls);
//输出
out.println(rb.getString("demo"));
out.println(rb.getString("userName"));
out.println(rb.getString("password"));
out.println(rb.getString("welcome"));
%>
</body>
</html>

三、Struts2实现

准备全局资源文件:通常至少三个文件

命名规则:前缀名_语言_国家.properties

内容格式:key=value

指定资源文件(设置常量)

1.struts.xml文件中:

    <constant name="struts.custom.i18n.resources" value="message" />

2.struts.properties中:

struts.custom.i18n.resources=message
<s:text name="demo"></s:text>
<s:text name="userName" />
<s:text name="password" />
<s:text name="welcome" />

更改资源文件

message.properites

demo = 您好世界!
userName = 用户名
password = 密码
welcome = 欢迎!
submit =提交
reset=重置

message_zh_CN.properties

demo = 您好世界!
userName = 用户名
password = 密码
welcome = 欢迎!
submit =提交
reset=重置

message_en_US.properties

demo = hello world!
userName = user name
password = password
welcome = welcome!
submit =submit
reset=reset
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>测试</title>
</head>
<body>
<s:text name="demo"></s:text>
<s:text name="userName" />
<s:text name="password" />
<s:text name="welcome" />
<s:form action="" method="post">
<s:textfield key="userName" />
<s:password key="password"/>
<s:submit key="submit"/>
<s:reset key="reset" />
</s:form>
</body>
</html>

四、Action类实现国际化

message.properties

demo = 您好世界!
userName = 用户名
password = 密码
welcome = {0}欢迎!{1}
submit =提交
reset=重置
fail=登录失败
usermsg=用户名不能为空
usermsglength=用户名的长度为2-16位
pwdmsg=密码不能为空
pwdmsglength=密码的长度为2-16位

message_zh_CN.properties同上

message_en_US.properties

demo = hello world!
userName = user name
password = password
welcome = {0}welcome!{1}
submit =submit
reset=reset
fail=login failed
usermsg=user name do not empty
usermsglength=user name length is 2 to 16!
pwdmsg=password do not empty
pwdmsglength=password length is 2 to 16!

struts.xml

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="message" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="action.LoginAction" method="login">
<result name="success">
/demo.jsp
</result>
<result name="input">
/demo.jsp
</result>
</action>
</package>
</struts>

demo.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>测试</title>
</head>
<body>
<%-- <s:text name="demo"></s:text>
<s:text name="userName" />
<s:text name="password" />
<s:text name="welcome" /> --%>
<s:form action="login.action" method="post">
<s:textfield name="username" key="userName" />
<s:password name="password" key="password"/>
<s:submit key="submit"/>
<s:reset key="reset" />
</s:form>
<hr/><br/>
<s:actionmessage />
<s:debug/>
</body>
</html>

LogionAction

package action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
private String username;
private String password; public String login() {
if ("admin".equalsIgnoreCase(username)
&& "admin".equalsIgnoreCase(password)) { this.addActionMessage(this.getText("welcome", new String[] {
username,password}));
return SUCCESS;
}
this.addActionMessage(this.getText("fail"));
return INPUT;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

LogionAction.login()验证文件

LogionAction-login-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>${getText("usermsg")}</message>
</field-validator>
<field-validator type="stringlength">
<param name="trim">true</param>
<param name="minLength">2</param>
<param name="maxLength">16</param>
<message>${getText("usermsglength")}</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>${getText("pwdmsg")}</message>
</field-validator>
<field-validator type="stringlength">
<param name="trim">true</param>
<param name="minLength">2</param>
<param name="maxLength">16</param>
<message>${getText("pwdmsglength")}</message>
</field-validator>
</field> </validators>

五、Jsp页面国际化传参数

<!-- First Example -->
<s:i18n name="struts.action.test.i18n.Shop">
<s:text name="main.title"/>
</s:i18n> <!-- Second Example -->
<s:text name="main.title" /> <!-- Third Examlpe -->
<s:text name="i18n.label.greetings">
<s:param >Mr Smith</s:param>
</s:text>
<s:text name="welcome">
<s:param>admin</s:param>
<s:param>ffff</s:param>
</s:text>

六、资源查找顺序

七、在JSP中使用国际化

使用UI表单标志时,getText可以用来设置label属性,例如: <s:textfield name="name" label="%{getText('UserName')}"/>

八、资源文件里使用OGNL表达式

许多情况下,我们都需要在动行时(runtime)为国际化字符插入一些参数,例如在输入验证提示信息的时候。在Struts 2.0中,我们可以通过在资源文件的国际化字符串中使用OGNL做到这点: 格式为${表达式},例如:

validation.require=${fileName} is required

九、资源文件查找顺序

Struts2(十三)国际化-internationalization的更多相关文章

  1. 浅谈struts2的国际化----i18n

    可能大家在使用struts框架的时候,偶尔会看到这个词: i18n.也就是 Internationalization    i 开头,n 结尾. 总共18个字母,今天的主要内容就是环绕这 四个字母. ...

  2. (十四)struts2的国际化

    一.国际化的概念 国际化是指web程序在运行时,根据客户端请求的国家.语言的不同而显示不同的界面. 例如,如果请求来自中文客户端,则页面的显示,提示信息等都是中文,如果是英文客户端,则显示英文信息.  ...

  3. 十四、Struts2的国际化

    十四.Struts2的国际化 1.配置全局国际化消息资源包 配置全局消息资源包 <!--配置全局消息资源包 -->     <constant name="struts.c ...

  4. JavaWeb框架_Struts2_(八)----->Struts2的国际化

    这一篇博文拖了蛮久了,现在先把它完成,结束struts2这个版块,当然这只是最基础的部分,做项目还需要更深的理解.下一个web后端的版块准备做Spring框架的学习-嗯,加油! 1. Struts2的 ...

  5. struts2使用拦截器完成登陆显示用户信息操作和Struts2的国际化

    其实学习框架,就是为了可以很好的很快的完成我们的需求,而学习struts2只是为了替代之前用的servlet这一层,框架使开发更加简单,所以作为一个小菜鸟,特别感谢那些超级无敌变态开发的框架供我们使用 ...

  6. struts2 资源国际化

    web.xml: <?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp ...

  7. Struts2的国际化入门

    Struts2的国际化入门 Struts2国际化是建立在Java国际化的基础上的,一样是通过提供不同国家/语言环境的消息资源,然后通过ResourceBundle加载指定Locale对应的资源文件,再 ...

  8. 【Struts2】 国际化

    一.概述 二.Struts2中国际化: 2.1 问题1 全局 局部 2.2 问题2 2.3 问题3 2.4 问题4 在Action中怎样使用 在JSP页面上怎样使用 一.概述 同一款软件 可以为不同用 ...

  9. Struts2之国际化

    时间:2017-1-11 11:12 --国际化Struts2已经对国际化进行了封装,我们只需要根据其提供的API进行访问即可.要使用国际化的Action必须继承ActionSupport.1.什么是 ...

随机推荐

  1. Lambda动态创建

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 理解 JS 回调函数中的 this

    任何变量或对象都有其赖以生存的上下文.如果简单地将对象理解为一段代码,那么对象处在不同的上下文,这段代码也会执行出不同的结果. 例如,我们定义一个函数 getUrl 和一个对象 pseudoWindo ...

  3. 关于js中的回收机制,通俗版

    在前面的几篇文章中,我讲解过了js中的回收机制,但是对于当时的我来说,我自己对回收机制的这个概念也有些懵懵懂懂,现在对回收机制有了更深入的理解,所以特此发布此文给于总结,也好加深记忆. 如果你想学习闭 ...

  4. 斐波那契堆(二)之 C++的实现

    概要 上一章介绍了斐波那契堆的基本概念,并通过C语言实现了斐波那契堆.本章是斐波那契堆的C++实现. 目录1. 斐波那契堆的介绍2. 斐波那契堆的基本操作3. 斐波那契堆的C++实现(完整源码)4.  ...

  5. nodejs 核心模块crypto

    crypto用于加密解密 'use strict' var crypto=require('crypto'); var data={age:18} var key='dt';//定义一个钥匙 var ...

  6. python 在sublime 中的配置

    首先可以先装package control 方法——按ctrl+`,然后在命令行里复制粘贴以下代码, import urllib2,os;pf='Package Control.sublime-pac ...

  7. ASP.NET MVC 在控制器中接收视图表单POST过来的数据方法

    方法一:通过Request.Form [HttpPost]        public ActionResult Test()        {            string id=Reques ...

  8. UIScrollView子控件的布局

    scorllView内部子控件添加约束的注意点: 1.子控件的尺寸不能通过UIScrollView来计算 *比如可以设置固定值 (width==100 height ==100) *比如可以相对于UI ...

  9. Scrum团队

    5.Scrum团队成立 5.1 团队名称,团队目标.团队口号.团队照: 团队名称:@four! 团队目标:做出像“数学口袋精灵”那么棒的软件 团队口号:多劳多得 团队照: 5.2 角色分配 产品负责人 ...

  10. C#利用NPOI导出Excel类(简单版)

    代码: using System.Data; using System.IO; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; namespac ...