Struts2国际化如何实现

本文讲解"Struts2国际化怎么实现",希望能够解决相关问题。

”国际化“是指一个应用程序在运行时能够根据客户端请求所来自国家或者地区语言的不同而显示不同的用户界面。

引入国家化机制的目的在于提供自适应的、更友好的用户界面,而不必改变程序的其他功能或者业务逻辑。

而Struts2实现国际化的流程具体流程如下所示:

1.不同地区使用的操作系统环境不同,如中文操作系统,英文操作系统等。获得客户端地区的语言环境后在,struts.xml文件中会找到相应的国际化资源文件,如果操作系统环境是中文环境,就加载中文国际化资源文件。所以国际化需要编写支持多个语言的国际化资源文件,并且在struts.xml文件中配置。

2.根据选择的语言加载相应的国际化资源文件,试图通过struts标签读取国家化资源文件并把数据输出到页面上,完成页面的显示。

下面介绍在国际化流程中用到的文件

国际化资源文件或者资源文件

国际化资源文件又称为资源文件,是以properties为扩展名的文件,该文件以”键==值“对的形式存储资源数据。

例如:

key=value

loginName=用户名称

loginPassword=用户密码

例子(中英文登录系统):

文件框架:

Struts2国际化如何实现

login.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags" %><html>
	<head>
		<title><s:text name="loginTitle"></s:text></title>
	</head>
	<body>
		<s:form action="checkLogin" method="post">
				<!-- 表单元素的key值与资源文件的key对应 -->
				<s:textfield name="UserName" key="loginName" size="20" />
				<s:password name="UserPassWord" key="loginPassword" size="20" />
				<s:submit key="loginSubmit" />
		</s:form>
	</body></html>

loginSuccess.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags" %><html>
	<head>
		<title><s:text name="successPage"></s:text></title>
	</head>
	<body>
		<hr>
		<s:text name="loginName" />:<s:property value="userName" /><br>
		<s:text name="loginPassword" />:<s:property value="userPassWord" /><br>
	</body></html>

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
	http://xmlns.jcp.org/xml/ns/javaeee/web-app_3_1.xsd" 
	id="WebApp_ID" 
	version="3.1" >
		<filter>
		<!-- 配置Struts2核心控制器的名字 -->
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>	
	<filter-mapping>
		<!-- Struts2控制器的名字 -->
		<filter-name>struts2</filter-name>
		<!-- 拦截所有的URL请求-->
		<url-pattern>/*</url-pattern>
	</filter-mapping>	
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>		</web-app>

struts.xml

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd"><struts>
	<!-- 使用Struts2中的I18N拦截器,并通过constant元素配置常量,指定国际资源文件名字,
			value的值就是常量值,即国际化资源文件的名字 -->
	<constant name="struts.custom.i18n.resources" value="globalMessages"></constant>
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	<package name="I18N" extends="struts-default">
		<action name="checkLogin" class="loginAction.LoginAction">
			<result name="success">/I18N/loginSuccess.jsp</result>
			<result name="error">/I18N/login.jsp</result>
		</action>
	</package></struts>

LoginAction.java

package loginAction;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.sun.net.httpserver.Authenticator.Success;public class LoginAction extends ActionSupport{	private static final String SUCCESS = null;	private static final String ERROR = null;	private String userName;	private String userPassWord;	private String tip;	public String getUserName() {		return userName;
	}	public void setUserName(String userName) {		this.userName = userName;
	}	public String getUserPassWord() {		return userPassWord;
	}	public void setUserPassWord(String userPassWord) {		this.userPassWord = userPassWord;
	}	public String getTip() {		return tip;
	}	public void setTip(String tip) {		this.tip = tip;
	}	
	public String execute() throws Exception{
		ActionContext ac=ActionContext.getContext();		if(getUserName().equals("hjw")&&getUserPassWord().equals("123")) {
			ac.getSession().put("userName", getUserName());			this.tip=getText("login.success",new String[] {this.userName});			return SUCCESS;
		}		else {			return ERROR;
		}
	}
}

globalMessages_en_US.properties

loginTitle=UserLogin
loginName=Username
loginPassword=UserPassword
loginSubmit=login

globalMessages_zh_CN.properties

loginTitle=\u7528\u6237\u767b\u5f55
loginName=\u7528\u6237\u540d\u79f0
loginPassword=\u7528\u6237\u5bc6\u7801
loginSubmit=\u767b\u5f55

关于 "Struts2国际化怎么实现" 就介绍到此。希望多多支持编程宝库

Struts2漏洞如何检测:本文讲解"Struts2漏洞怎么检测",希望能够解决相关问题。 测试方法:访问地址:http://Host/login.action?redirect:%25{3-4}建议修复前使用此链接测试s ...