使用struts2进行国际化
来源:互联网 发布:中国象棋开局软件 编辑:程序博客网 时间:2024/06/05 13:35
两种方案
1.action中进行国际化
写一个Person 代码如下:
public class Person{ private String firstName; private String lastName; private String email; private int age;//geters and setters...}
action中代码:
public class Register extends ActionSupport { private static final long serialVersionUID = 1L; private Person personBean;
@Override public String execute() throws Exception { return SUCCESS; } public Person getPersonBean() { return personBean; } public void setPersonBean(Person person) { personBean = person; } public void validate(){ if ( personBean.getFirstName().length() == 0 ){ addFieldError( "personBean.firstName", "First name is required." ); } if ( personBean.getEmail().length() == 0 ){ addFieldError( "personBean.email", "Email is required." ); } if ( personBean.getAge() < 18 ){ addFieldError( "personBean.age", "Age is required and must be 18 or older" ); } }}
struts.xml中配置action:
<package name="basicstruts2" extends="struts-default">
<action name="index"> <result>/index.jsp</result> </action> <action name="register" method="execute"> <result name="success">/thankyou.jsp</result> <result name="input">/register.jsp</result> </action> <action name="registerInput" method="input" > <result name="input">/register.jsp</result> </action> </package>
web.xml中配置struts:
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter>
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
在Register包下建立如下两个资源文件,一个写英文一个中文,
Register_en_US.properties 内容如下:
personBean.firstName=First namepersonBean.lastName=Last namepersonBean.age=AgepersonBean.email=Emailthankyou=Thank you for registering %{personBean.firstName}.
Register_zh_CN.properties 内容如下(在eclipse里写中文,会自动转码):
personBean.firstName=\u540d\u5b57personBean.lastName=\u59d3personBean.age=\u5e74\u9f84personBean.email=\u90ae\u7bb1thankyou=\u8c22\u8c22\u6ce8\u518c %{personBean.firstName}.
文件结构如图:
页面代码,使用struts标签:
<s:form action="register"> <s:textfield key="personBean.firstName" /> <s:textfield key="personBean.lastName" /> <s:textfield key="personBean.email" /> <s:textfield key="personBean.age" /> <s:submit/></s:form>
记得页面中加入:
<%@ taglib prefix="s" uri="/struts-tags" %>
效果展示:
切换浏览器语言为英文,如下图方式进行切换,IE下打开internet设置:
打开页面,看结果:
2.在界面使用标签切换
struts.xml中增加:
<constant name="struts.custom.i18n.resources" value="message"/>
创建资源文件
message_zh_CN.properties
message.properties
按照上面的举例写入内容
也在页面中如下方式引入即可:
<s:label name="wiz_fir_title" value="%{getText('wiz_fir_title')}"/>
记得加入struts表头
其他常见问题:
报错:The Struts dispatcher cannot be found.
This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
增加
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
- 使用struts2进行国际化
- Struts2使用23 -- 国际化
- 使用Struts2国际化支持
- 使用Struts2国际化
- 使用Struts2(三)国际化
- Struts2中使用国际化
- 使用GTK+进行国际化
- native2ascii工具使用 struts2 国际化
- struts2--struts2国际化(使用占位符)
- 【Struts2】(3)Struts2国际化、Struts2上传下载、Struts2标签使用
- JAVA_WEB Struts2学习:使用验证框架进行校验JSP页面元素 反编译工具的配置 Strtuts2 国际化学习
- extremecomponents使用struts2国际化信息的方法
- extremecomponents使用struts2国际化信息的方法
- struts2的国际化的配置与使用
- 使用struts2实现国际化小例子
- Struts2国际化
- struts2 国际化
- struts2国际化
- Objective-C-类(static)方法、实例方法、overwrite(覆写)、属性(property)
- ControlTemplate in WPF —— Calendar
- 【?人际交往----分寸 你做到了吗?】
- ControlTemplate in WPF —— Shared in all file
- 超实用的8个Linux命令行性能监测工具
- 使用struts2进行国际化
- Determining and Monitoring the Connectivity StatusAndroid 电源管理专题之获取和监测网络连接状态
- Linux 核心之硬件基础
- 关于For循环中定义的变量在循环外的引用
- ASP.NET文本编辑器(FCKeditor)详细介绍
- URL的getFile()和getPath()方法的区别
- 基于cocos2d-x引擎的游戏框架设计
- ASP.NET配置文件有那些秘密? (转)
- [转]Delphi 几个读写注册表的函数