Introduction:
For: Make web app internationalization.
Skill: Jsp Tag Library.
注: 本文讲解的是struts内部的国际化详细解析,从底层的tag搭建
1. Create Tag
src\com\taglibs\messageTag.java
2. Config tld file
WEB-INF/struts-bean.tld
3. 定义资源文件
在/WEB-INF/classes下面添加UTF-8资源束文件。每一个资源文件是“键-值”对的集合。在JSP页面里面可以通过键来找到相应的数据值。本例子的文件名是ApplicationResources,所以相应的资源文件束是(包括e文,简体中文,繁体中文)
language.properties : 默认资源文件。当在其他资源文件里面找不到某个资源的时候,就使用该资源文件里面的定义。
language_zh_CN.properties:简体中文资源文件。
language_zh_TW.properties:繁体中文资源文件。
资源文件的格式为:默认资源文件名_语言_国别.properties。其中每个文件都是通过%JAVA_HONE%/BIN/native2ascii.exe工具转换而来。
1) 准备文件
//ApplicationResources.properties ;默认资源文件,通常里面的内容是英文的。
label.username=USERNAME :
label.password=PASSWORD :
label.login.submit=Submit
//ApplicationResources_zh_CN.bak ;简体中文的资源文件。里面的内容是中文的。它需要工具将其中的内容处理成UTF-8
label.username=用户名 :
label.password=密 码 :
label.login.submit=登录
//ApplicationResources_zh_TW.bak : 繁体中文的资源文件。里面的内容是中文的。它需要工具将其中的内容处理成UTF-8,下面的内容是繁体码。
label.username=ノめ?W :
label.password=ノめ?W :
label.login.submit=めノ?W
2)准备完成以后,使用如下的命令创建UTF-8资源文件束
native2ascii -encoding gb2312 ApplicationResources_zh_CN.bak ApplicationResources_zh_CN.properties
native2ascii -encoding big5 Applica tionResources_zh_TW.bak ApplicationResources_zh_TW.properties
3. Test the taglib
index.jsp
4. Config for Web App1)定义web.xml的动ActionServlet的参数
5. 表单的数据的处理。
对于表单数据的处理,我们是通过添加一个Filter来实现的。所有提交的请求,都需要做字符处理。然后在web.xml里面定义该Filter。这样我们就不需要在程序里面做任何的字符处理。
1) 定义Filter。下面是一个例子。
2) 在web.xml里面声明使用该Filter
For: Make web app internationalization.
Skill: Jsp Tag Library.
注: 本文讲解的是struts内部的国际化详细解析,从底层的tag搭建
1. Create Tag
src\com\taglibs\messageTag.java
package com.taglibs;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class messageTag extends TagSupport
{
private String key="";
private ResourceBundle bundle;// default.properties
private Locale locale=Locale.SIMPLIFIED_CHINESE;
private String name="";
private String property="";
private String scope="";
private String arg0="";
private String arg1="";
private String arg2="";
private String arg3="";
private String arg4="";
private String arg5="";
private static String ProfileName = "language";
@Override
public int doStartTag() throws JspException
{
super.doStartTag();
try{
JspWriter out = pageContext.getOut();
//Locale locale = new Locale("zh", "CN");
if(bundle==null)
bundle = ResourceBundle.getBundle(ProfileName, locale);
String format = bundle.getString(key);
String value = String.format(format, arg0, arg1, arg2, arg3, arg4, arg5 );
if(value==null)
out.print(key);
else
out.print(value);
}catch(IOException e){
throw new JspTagException(e.getMessage());
}
return SKIP_BODY;
}
public static String getProfileName()
{
return ProfileName;
}
public static void setProfileName(String profileName)
{
ProfileName = profileName;
}
public void setBundle(ResourceBundle bundle)
{
this.bundle = bundle;
}
public ResourceBundle getBundle()
{
return bundle;
}
public void setBundle(String language)
{
this.locale = new Locale(language);
this.bundle = ResourceBundle.getBundle(ProfileName, locale);
/*
* ResourceBundle bundle = ResourceBundle.getBundle(PROPERTIES_FILE_NAME, Locale.ENGLISH);
这行代码初始化了一个ResourceBundle,Locale.ENGLISH用于指明本地化情况,
因此会从"property_en.properties"中去读取配置项。如果是Locale.CHINA,
则会从property_zh.properties中读取。这种机制使得程序的本地化变得简单。
*/
}
public Locale getLocale()
{
return locale;
}
public void setLocale(Locale locale)
{
this.locale = locale;
}
public String getKey()
{
return key;
}
public void setKey(String key)
{
this.key = key;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getProperty()
{
return property;
}
public void setProperty(String property)
{
this.property = property;
}
public String getScope()
{
return scope;
}
public void setScope(String scope)
{
this.scope = scope;
}
public String getArg0()
{
return arg0;
}
public void setArg0(String arg0)
{
this.arg0 = arg0;
}
public String getArg1()
{
return arg1;
}
public void setArg1(String arg1)
{
this.arg1 = arg1;
}
public String getArg2()
{
return arg2;
}
public void setArg2(String arg2)
{
this.arg2 = arg2;
}
public String getArg3()
{
return arg3;
}
public void setArg3(String arg3)
{
this.arg3 = arg3;
}
public String getArg4()
{
return arg4;
}
public void setArg4(String arg4)
{
this.arg4 = arg4;
}
public String getArg5()
{
return arg5;
}
public void setArg5(String arg5)
{
this.arg5 = arg5;
}
public static String readValue(String filePath,String key) {
Properties props = new Properties();
File f = new File(filePath);
if(!f.exists()){
System.out.println("file not exist!");
return null;
}
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
String value = props.getProperty (key);
System.out.println(key+"="+value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//读取properties的全部信息
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty (key);
System.out.println(key+Property);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//写入properties信息
public static void writeProperties(String filePath,String parameterName,String parameterValue) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
prop.load(fis);
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
prop.store(fos, "Update '" + parameterName + "' value");
} catch (IOException e) {
System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
}
}
}
2. Config tld file
WEB-INF/struts-bean.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<shot-name>messageTag</shot-name>
<description>my struts lib!</description>
<tag>
<description></description>
<name>message</name>
<tag-class>com.taglibs.messageTab</tag-class>
<bodycontent>empty</bodycontent>
<attribute>
<name>arg0</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>arg1</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>arg2</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>arg3</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>arg4</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>bundle</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>key</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>locale</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>property</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>scope</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
3. 定义资源文件
在/WEB-INF/classes下面添加UTF-8资源束文件。每一个资源文件是“键-值”对的集合。在JSP页面里面可以通过键来找到相应的数据值。本例子的文件名是ApplicationResources,所以相应的资源文件束是(包括e文,简体中文,繁体中文)
language.properties : 默认资源文件。当在其他资源文件里面找不到某个资源的时候,就使用该资源文件里面的定义。
language_zh_CN.properties:简体中文资源文件。
language_zh_TW.properties:繁体中文资源文件。
资源文件的格式为:默认资源文件名_语言_国别.properties。其中每个文件都是通过%JAVA_HONE%/BIN/native2ascii.exe工具转换而来。
1) 准备文件
//ApplicationResources.properties ;默认资源文件,通常里面的内容是英文的。
label.username=USERNAME :
label.password=PASSWORD :
label.login.submit=Submit
//ApplicationResources_zh_CN.bak ;简体中文的资源文件。里面的内容是中文的。它需要工具将其中的内容处理成UTF-8
label.username=用户名 :
label.password=密 码 :
label.login.submit=登录
//ApplicationResources_zh_TW.bak : 繁体中文的资源文件。里面的内容是中文的。它需要工具将其中的内容处理成UTF-8,下面的内容是繁体码。
label.username=ノめ?W :
label.password=ノめ?W :
label.login.submit=めノ?W
2)准备完成以后,使用如下的命令创建UTF-8资源文件束
native2ascii -encoding gb2312 ApplicationResources_zh_CN.bak ApplicationResources_zh_CN.properties
native2ascii -encoding big5 Applica tionResources_zh_TW.bak ApplicationResources_zh_TW.properties
3. Test the taglib
index.jsp
<%@ page language="java" contentType="text/html;charset=GBK" %>
<%@ page isELIgnored="false" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<body>
<form name=myform action="test.do" >
<bean:message key="label.username" bundle="en"/><input type="text" id="userName"> <br>
<bean:message key="label.password" bundle="en"/><input type="text" id="passWord"> <br>
<button οnclick="myform.submit();"><bean:message key="label.login.submi" bundle="en"/></button>
</form>
<body>
4. Config for Web App1)定义web.xml的动ActionServlet的参数
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>application</param-name>
<param-value>language</param-value> <!-- 默认资源文件名 -->
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
可以在IE的工具->Internet选项->语言的地方,来选择,定义IE的语言。
5. 表单的数据的处理。
对于表单数据的处理,我们是通过添加一个Filter来实现的。所有提交的请求,都需要做字符处理。然后在web.xml里面定义该Filter。这样我们就不需要在程序里面做任何的字符处理。
1) 定义Filter。下面是一个例子。
package com.webapps.commons;
import java.io.*;
import javax.servlet.*;
public class CharsetEncodingFilter implements Filter{
private FilterConfig config = null;
private String defaultEncode = "UTF-8";
public void init(FilterConfig config) throws ServletException {
this.config = config;
if(config.getInitParameter("Charset")!=null){
defaultEncode=config.getInitParameter("Charset");
}
}
public void destroy() {
this.config = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
ServletRequest srequest=request;
srequest.setCharacterEncoding(defaultEncode);
chain.doFilter(srequest,response);
}
}
2) 在web.xml里面声明使用该Filter
<filter>
<filter-name>Character Encoding</filter-name>
<filter-class>com.webapps.commons.CharsetEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>