SSH系类(三)struts国际化(I18N)
2014-12-18 20:38
169 查看
Struts实现国际化和java实现国际化是一样的,那么什么是国际化呢?对于我们浏览的网页,对于同一种网页,我们可以看到不同种语言的形式,而这些网页的制作都是支持了国际化,对于语言的选择,缺省的locale是由操作系统决定的。
java中实现国际化的原理:java中的locale对象可以指定或得到当前系统默认的国际化配置以及语言代码和国家代码,然后通过ResourceBundle类得到对应Lacale对象的国际化配置文件即Properties文件。
国际化支持还需要提供一个国际化资源文件,国际化的资源文件是由baseName+locale构成,如:MessagesBundle_zh_CN.properties,而缺省的国际化资源文件的命名格式为baseName.properties,如下:MessagesBundle.properties。在中文转换中会出现乱码的问题,需要将中文转换成unicode,转换方式可以采用D:\java\jdk1.7.0\bin\native2ascii.exe工具转换。批量转换如下:
native2ascii.exe o.propertiesMessagesBundle_zh_CN.properities,o.properties是转码之前经过备份之后的资源文件
下面通过例子讲解java国际化的支持:
1、编写国际化资源文件:
中文经过转码之前的资源文件(o.properties)
K1=你好,{0}
k2=再见
转码之后的支持中文的资源文件(MessagesBundle_zh_CN.properties)
k1=\u4F60\u597D,{0}
k2=\u518D\u89C1
(Unicode的转码方式上面已经介绍了!)
支持英文的资源文件(MessagesBundle_en_US.properties)
k1=hello,{0}
k2=good bye
当不存在这种语言时默认的资源文件(MessagesBundle.properties)
k1=hello,{0}
k2=good bye
编写java代码:
Locale.getDefault()可以获得操作系统默认的编码,ResourceBundle类可以读取响应的locale中对应的.properties文件。
Struts实现国际化:
和java实现国际化类似,都需要国际化资源文件,不同的是Struts需要进行国际化配置,然后利用struts标签完成国际化消息文本的读取。另外可以利用struts默认将locale放到session的特性,可以完成语言的自动切换。
下面的例子讲解struts是如何支持国际化的:
1、国际化资源文件:
1)默认的国际化资源文件:(MessageResources.properties)
2)支持中文转码前的资源文件:(o.properties)
3)支持中文转码后的资源文件:(MessageResources_zh_CN.properties)
errors.header=<UL>
errors.prefix=<font color="red"><LI>
errors.suffix=</LI></font>
errors.footer=</UL>
login.form.field.username=\u7528\u6237
login.form.field.password=\u5bc6\u7801
login.form.button.login=\u767b\u5f55
login.success={0},\u767b\u5f55\u6210\u529f
login.user.not.found=\u7528\u6237\u4e0d\u80fd\u627e\u5230\uff0c\u7528\u6237\u540d\u79f0=\u3010{0}\u3011
login.password.error=\u5bc6\u7801\u9519\u8bef
4)支持英文的资源文件:(MessageResources_en_US.properties)
错误信息的格式定义:errors.header=<UL> 信息的头部代码
errors.prefix=<LI><fontcolor="red"> 每条信息的开头代码
errors.suffix=</font></LI>每条信息的结尾代码
errors.footer=</UL> 信息的尾部代码
2、编写ActionForm文件:(LoginActionForm)
3、编写Action文件:(LoginAction)
动态文本国际化实现的三个步骤:
1)创建国际化消息文本:
ActionMessage message=newActionMessage("login.success",username);
messages.add("login_success_1", message);
2)传递国际化消息文本:
this.saveMessages(request, messages);
3)显示国际化消息文本:
<html:messages>标签显示提示消息和错误消息,<html:errors>标签只能显示错误消息。调用saveMessage()传递提示消息,调用saveErrors()传递错误消息。
4、UserManager类进行业务逻辑:
这里只是简单的抛出异常,交给两个异常类进行显示:用户不能找到(UserNotFoundException)和密码错误(PasswordErrorException)。
然后通过将locale设置到session中完成语言的自动切换:
下面就是国际化文本的显示:
我们使用struts标签,首先导入标签库:
<html:errors/>标签读取错误信息,<bean:message>输出提示信息文本,其中的key值就是国际化资源文件中对应的值。
<html:messages>读取普通消息文本
<font color="red">
<html:messages id="msg" property="error_1">
<bean:write name="msg"/>
</html:messages>
</font>
<font color="blue">
<html:messages id="msg" property="error_2">
<bean:write name="msg"/>
</html:messages>
</font>
在<html:messages>中的property属性表示从该信息集合中取出名为此属性值的元素的集合,可根据property读出所有是该属性名的错误信息的集合。
以上就是struts的标签库和对国际化的支持的简单总结,还需要深入的了解和应用。
java中实现国际化的原理:java中的locale对象可以指定或得到当前系统默认的国际化配置以及语言代码和国家代码,然后通过ResourceBundle类得到对应Lacale对象的国际化配置文件即Properties文件。
国际化支持还需要提供一个国际化资源文件,国际化的资源文件是由baseName+locale构成,如:MessagesBundle_zh_CN.properties,而缺省的国际化资源文件的命名格式为baseName.properties,如下:MessagesBundle.properties。在中文转换中会出现乱码的问题,需要将中文转换成unicode,转换方式可以采用D:\java\jdk1.7.0\bin\native2ascii.exe工具转换。批量转换如下:
native2ascii.exe o.propertiesMessagesBundle_zh_CN.properities,o.properties是转码之前经过备份之后的资源文件
下面通过例子讲解java国际化的支持:
1、编写国际化资源文件:
中文经过转码之前的资源文件(o.properties)
K1=你好,{0}
k2=再见
转码之后的支持中文的资源文件(MessagesBundle_zh_CN.properties)
k1=\u4F60\u597D,{0}
k2=\u518D\u89C1
(Unicode的转码方式上面已经介绍了!)
支持英文的资源文件(MessagesBundle_en_US.properties)
k1=hello,{0}
k2=good bye
当不存在这种语言时默认的资源文件(MessagesBundle.properties)
k1=hello,{0}
k2=good bye
编写java代码:
Locale.getDefault()可以获得操作系统默认的编码,ResourceBundle类可以读取响应的locale中对应的.properties文件。
package com.bjpowernode.i18n; import java.text.MessageFormat; import java.util.Locale; import java.util.ResourceBundle; public class I18nSample { public static void main(String[] args) { //得到系统当前默认的编码 Locale defaultLocale=Locale.getDefault(); System.out.println("country="+defaultLocale.getCountry()); System.out.println("language="+defaultLocale.getLanguage()); //Locale currentLocale=new Locale("zh","CN");//支持中文的编码 Locale currentLocale=new Locale("en","US"); //支持英文的编码 //Locale currentLocale=new Locale("essdsn","UsadgS");//在没有的情况下选择缺省的国际化支持MessagesBundle.properties //ResourceBundle类可以读取相应的Locale 对应的property文件 ResourceBundle rb=ResourceBundle.getBundle("com.bjpowernode.resources.MessagesBundle", currentLocale); MessageFormat mf=new MessageFormat(rb.getString("k1")); System.out.println(mf.format(new Object[]{"张三"})); } }
Struts实现国际化:
和java实现国际化类似,都需要国际化资源文件,不同的是Struts需要进行国际化配置,然后利用struts标签完成国际化消息文本的读取。另外可以利用struts默认将locale放到session的特性,可以完成语言的自动切换。
下面的例子讲解struts是如何支持国际化的:
1、国际化资源文件:
1)默认的国际化资源文件:(MessageResources.properties)
errors.header=<UL> errors.prefix=<font color="red"><LI> errors.suffix=</LI></font> errors.footer=</UL> login.form.field.username=User Name login.form.field.password=Password login.form.button.login=Login login.success={0},Login Success login.user.not.found=User Not Found,UserName\=[{0}] login.password.error=Password Error
2)支持中文转码前的资源文件:(o.properties)
errors.header=<UL> errors.prefix=<font color="red"><LI> errors.suffix=</LI></font> errors.footer=</UL> login.form.field.username=用户 login.form.field.password=密码 login.form.button.login=登录 login.success={0},登录成功 login.user.not.found=用户不能找到,用户名称=【{0}】 login.password.error=密码错误
3)支持中文转码后的资源文件:(MessageResources_zh_CN.properties)
errors.header=<UL>
errors.prefix=<font color="red"><LI>
errors.suffix=</LI></font>
errors.footer=</UL>
login.form.field.username=\u7528\u6237
login.form.field.password=\u5bc6\u7801
login.form.button.login=\u767b\u5f55
login.success={0},\u767b\u5f55\u6210\u529f
login.user.not.found=\u7528\u6237\u4e0d\u80fd\u627e\u5230\uff0c\u7528\u6237\u540d\u79f0=\u3010{0}\u3011
login.password.error=\u5bc6\u7801\u9519\u8bef
4)支持英文的资源文件:(MessageResources_en_US.properties)
errors.header=<UL> errors.prefix=<font color="red"><LI> errors.suffix=</LI></font> errors.footer=</UL> login.form.field.username=User Name login.form.field.password=Password login.form.button.login=Login login.success={0},Login Success login.user.not.found=User Not Found,UserName\=[{0}] login.password.error=Password Error
错误信息的格式定义:errors.header=<UL> 信息的头部代码
errors.prefix=<LI><fontcolor="red"> 每条信息的开头代码
errors.suffix=</font></LI>每条信息的结尾代码
errors.footer=</UL> 信息的尾部代码
2、编写ActionForm文件:(LoginActionForm)
package com.bjpowernode.struts; import org.apache.struts.action.ActionForm; /** * 登录ActionForm,负责表单收集数据 * 表单的属性必须和ActionForm中的get和set的属性一致 * @author Administrator * */ @SuppressWarnings("serial") public class LoginActionForm extends ActionForm { private String username; private String password; 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; } }
3、编写Action文件:(LoginAction)
package com.bjpowernode.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; /** * 登录Action * 负责取得表单数据,调用业务逻辑,返回转向信息 * @author xiao * */ public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginActionForm laf=(LoginActionForm)form; String username=laf.getUsername(); String password=laf.getPassword(); UserManager userManager=new UserManager(); //消息文本放到一个消息集合中 ActionMessages messages=new ActionMessages(); try{ userManager.login(username, password); //创建国际化消息文本 //ActionMessage message=new ActionMessage("login.success",new Object[]{username}); ActionMessage message=new ActionMessage("login.success",username); messages.add("login_success_1", message); // ActionMessage message1=new ActionMessage("login.success",username); // messages.add("login_success_2", message1); //传递国际化消息 this.saveMessages(request, messages); return mapping.findForward("success"); }catch(UserNotFoundException e){ e.printStackTrace(); //创建国际化消息文本 ActionMessage error=new ActionMessage("login.user.not.found",username); messages.add("error_1", error); //传递国际化消息 this.saveErrors(request, messages); }catch(PasswordErrorException e){ e.printStackTrace(); //创建国际化消息文本 ActionMessage error=new ActionMessage("login.password.error"); messages.add("error_2", error); //传递国际化消息 this.saveErrors(request, messages); } return mapping.findForward("error"); } }
动态文本国际化实现的三个步骤:
1)创建国际化消息文本:
ActionMessage message=newActionMessage("login.success",username);
messages.add("login_success_1", message);
2)传递国际化消息文本:
this.saveMessages(request, messages);
3)显示国际化消息文本:
<body> <html:messages id="msg" message="true"> <bean:write name="msg"/> </html:messages> </body>
<html:messages>标签显示提示消息和错误消息,<html:errors>标签只能显示错误消息。调用saveMessage()传递提示消息,调用saveErrors()传递错误消息。
4、UserManager类进行业务逻辑:
package com.bjpowernode.struts; public class UserManager { public void login(String username,String password){ if(!"admin".equals(username)){ throw new UserNotFoundException(); } if(!"admin".equals(password)){ throw new PasswordErrorException(); } } }
这里只是简单的抛出异常,交给两个异常类进行显示:用户不能找到(UserNotFoundException)和密码错误(PasswordErrorException)。
然后通过将locale设置到session中完成语言的自动切换:
package com.bjpowernode.struts; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.Globals; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * 完成语言的手动切换 * @author xiao * */ public class ChangeLanguageAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String lang=request.getParameter("lang"); Locale locale=Locale.getDefault(); if("zh".equals(lang)){ locale=new Locale("zh","CN"); }else if("en".equals(lang)){ locale=new Locale("en","US"); } //将locale设置到session中 //request.getSession().setAttribute(Globals.LOCALE_KEY, locale); this.setLocale(request, locale); return mapping.findForward("index"); } }
下面就是国际化文本的显示:
我们使用struts标签,首先导入标签库:
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html:errors/> <form action="login.do" method="post"> <bean:message key="login.form.field.username"/>:<input type="text" name="username"><br> <bean:message key="login.form.field.password"/>:<input type="password" name="password"><br> <input type="submit" value="<bean:message key="login.form.button.login"/>"> </form>
<html:errors/>标签读取错误信息,<bean:message>输出提示信息文本,其中的key值就是国际化资源文件中对应的值。
<html:messages>读取普通消息文本
<font color="red">
<html:messages id="msg" property="error_1">
<bean:write name="msg"/>
</html:messages>
</font>
<font color="blue">
<html:messages id="msg" property="error_2">
<bean:write name="msg"/>
</html:messages>
</font>
在<html:messages>中的property属性表示从该信息集合中取出名为此属性值的元素的集合,可根据property读出所有是该属性名的错误信息的集合。
以上就是struts的标签库和对国际化的支持的简单总结,还需要深入的了解和应用。
相关文章推荐
- Struts(14)本地化/国际化(i18n)
- 在struts中使用国际化(i18n)
- 在Struts 2.0中国际化(i18n)您的应用程序
- 在Struts 2.0中国际化(i18n)您的应用程序
- 在Struts 2.0中国际化(i18n)您的应用程序
- Struts Internationalization (i18n 国际化)
- 在Struts 2.0中国际化(i18n)您的应用程序
- Struts(14)本地化/国际化(i18n)
- 在Struts 2.0中国际化(i18n)您的应用程序
- 在Struts 2.0中国际化(i18n)您的应用程序
- 菜鸟学Struts——I18N对国际化的支持
- 在Struts 2.0中国际化(i18n)您的应用程序
- Struts2.0 (开发之四)--(i18n)国际化--CTO
- 在Struts 2.0中国际化(i18n)
- 在Struts 2中国际化(i18n)您的应用程序
- 在struts中完美的使用i18n-国际化原来如此简单
- 在Struts 2.0中国际化(i18n)您的应用程序
- 在Struts 2.0中国际化(i18n)您的应用程序
- 在struts2.0中国际化(i18n)应用程序
- 菜鸟学Struts——I18N对国际化的支持