当前位置: 代码迷 >> Web前端 >> 施用struts2进行国际化
  详细解决方案

施用struts2进行国际化

热度:364   发布时间:2012-09-24 13:49:41.0
使用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>

  相关解决方案