struts2国际化

struts2国际化

1:什么是国际化?

国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式。它要求从产品中抽离所有的与语言,国家/地区和文化相关的元素。换言之,应用程序的功能和代码设计考虑在不同地区运行的需要,其代码简化了不同本地版本的生产。开发这样的程序的过程,就称为国际化。

2:在什么样的状况下使用国际化?

国际化是指的多语言支持,比方说你登录一个系统后,页面上的标签可以默认显示中文,或者默认显示英文。国际化在web开发中比较常用,以便于不同母语的用户使用系统。

3:为什么使用国际化?

原因:因为语言种类繁多

国际化需要在原来的基础上添加需要的资源文件,现在要实现 中文和英文之间的转换为例

首先创建出两个资源文件,分别是message.properties 和message_cn.properties

message_cn.properties  如下:(英文)

register.page=HouserRent-User Register
register.title=New User Register
name=Name
password=Password
repassword=RePassword
telephone=Telephone
username=UserName
submit=Register Now

name.null=Name cannot be null

message.properties如下:(中文)

register.page=\u7528\u6237\u6CE8\u518C
register.title=\u65B0\u7528\u6237\u6CE8\u518C
name=\u59D3\u540D
password=\u5BC6\u7801
repassword=\u786E\u8BA4\u5BC6\u7801
telephone=\u7535\u8BDD\u53F7\u7801
username=\u7528\u6237\u540D
submit=\u7ACB\u5373\u6CE8\u518C

name.null=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A

之后再struts.xml中添加constant 国际化标签

 <constant name="struts.custom.i18n.resources" value="message"></constant>

完成。

然后也可以提升约束的方法,利用xml文件,name不能为空进行约束,配置的是  languageActionvalidation.xml

<field name="name">
<!-- 不能为空 -->
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="name.null"/>
</field-validator>
</field>

具体代码如下:

准备界面:register.jsp

注:使用ognl表达式展示国际化!<s:text>,也可以放在<s:i81n>标签中。

<html>
<head>
    <title><s:text name="register.page"></s:text></title>
</head>
<body>
    <s:fielderror></s:fielderror>
    <h2><s:text name="register.title"></s:text></h2>
    <s:form action="loignAction">
    <table>
    <tr>
    <td><s:text name="name" ></s:text></td>
    <td><s:textfield name="name" key="name"></s:textfield><td>
    </tr>
    <tr>
    <td><s:text name="password"></s:text></td>
    <td><s:textfield name="password"></s:textfield></td>
    </tr>
    <tr>
    <td><s:text name="repassword"></s:text></td>
    <td><s:textfield name="repassword"></s:textfield></td>
    </tr>
    <tr>
    <td><s:text name="telephone"></s:text></td>
    <td><s:textfield name="telephone"></s:textfield></td>
    </tr>
    <tr>
    <td><s:text name="username"></s:text></td>
    <td><s:textfield name="username"></s:textfield></td>
    </tr>
    <tr>
    <td colspan="2"><s:submit
    value="%{getText('submit')}"></s:submit></td>
    </tr>
    </table>
    </s:form>
</body>
</html>

      在struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

     <!-- 在struts2.xml中配置指定资源文件的基名 -->
    <constant name="struts.custom.i18n.resources" value="message"></constant>
    <!-- 指定编码方案,如果想要转换国际化 必须指定为UTF-8 默认就是u8-->
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <!--  -->
    <constant name="struts.ui.theme" value="simple"></constant>
     <!-- 修改该文件,tomcat不用重启 -->
    <constant name="struts.devMode" value="true"/>
    <package name="default" namespace="/" extends="struts-default">
        <action name="loignAction" class="cn.Action.languageAction">
        <result name="success">/index.jsp</result>
          <result name="input">/register.jsp</result>
        </action>
    </package>
</struts>

  创建Action类,并继承自ActionSupport类,重写execute(),封装对应的属性:

package cn.Action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class languageAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private String name;
	private String password;
	private String repassword;
	private String telephone;
	private String username;
	
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	
	public String getName() {
		return name;
	}
	public String getPassword() {
		return password;
	}
	public String getRepassword() {
		return repassword;
	}
	public String getTelephone() {
		return telephone;
	}
	public String getUsername() {
		return username;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
}

  

 

 

posted @ 2016-10-13 17:03  锋锐之殇!  阅读(168)  评论(0编辑  收藏  举报