使用struts2进行国际化

两种方案

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

day walker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值