使用struts2进行国际化
2012-09-20 10:22
288 查看
两种方案
1.action中进行国际化
写一个Person 代码如下:
action中代码:
struts.xml中配置action:
web.xml中配置struts:
在Register包下建立如下两个资源文件,一个写英文一个中文,
Register_en_US.properties 内容如下:
Register_zh_CN.properties 内容如下(在eclipse里写中文,会自动转码):
文件结构如图:
页面代码,使用struts标签:
记得页面中加入:
效果展示:
切换浏览器语言为英文,如下图方式进行切换,IE下打开internet设置:
打开页面,看结果:
2.在界面使用标签切换
struts.xml中增加:
创建资源文件
message_zh_CN.properties
message.properties
按照上面的举例写入内容
也在页面中如下方式引入即可:
记得加入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>
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 name personBean.lastName=Last name personBean.age=Age personBean.email=Email thankyou=Thank you for registering %{personBean.firstName}.
Register_zh_CN.properties 内容如下(在eclipse里写中文,会自动转码):
personBean.firstName=\u540d\u5b57 personBean.lastName=\u59d3 personBean.age=\u5e74\u9f84 personBean.email=\u90ae\u7bb1 thankyou=\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>
相关文章推荐
- JAVA_WEB Struts2学习:使用验证框架进行校验JSP页面元素 反编译工具的配置 Strtuts2 国际化学习
- 使用struts2进行文件下载以及下载权限控制的例子
- 学习笔记: Struts2中使用validate框架对Action的不同方法进行验证
- struts2中使用xml进行validate验证
- Struts2中使用validate框架对Action的不同方法进行验证
- Struts2表单标签初步使用可以对表单进行回显
- extremecomponents使用struts2国际化信息的方法
- struts2使用XML框架进行输入校验
- 使用struts2进行文件下载以及下载权限控制的例子
- 在Struts2中,使用绑定select进行页面查询
- Struts2中使用拦截器进行权限控制
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(八)——Struts与EasyUI使用JSON进行交互
- 使用native2ascii命令处理Struts2国际化资源文件
- struts2 使用json与extjs进行数据交互
- struts2的国际化的配置与使用
- struts2中使用xml进行validation验证的两种方式
- Angular2中如何使用ngx-translate进行国际化
- struts2中使用jquery进行ajax提交时乱码解决办法
- 使用Qt Designer生成的界面进行国际化编程
- Struts2使用23 -- 国际化