`

struts2国际化配置(+页面下拉修改)

阅读更多
可以用struts.xml或struts.properties可用于覆盖default.properties的属性配置;
如:
default.properties
### Load custom default resource bundles
# struts.custom.i18n.resources=testmessages,testmessages2

### workaround for some app servers that don't handle HttpServletRequest.getParameterMap()
### often used for WebLogic, Orion, and OC4J
struts.dispatcher.parametersWorkaround = false

那么我们可以在struts.xml里面这样配置进行配置:
   1. <constant name="struts.custom.i18n.resources" value="messageResources"></constant>  

struts.properties
struts.custom.i18n.resources=messageResources

这两种配置都可以,区别就是在服务器在执行类加载的时候会先加载struts.xml后加载struts.properties
注意:
1。在配置他的资源文件明前缀的时候可以指定在具体的那个目录下面,如果我不是放在src目录下面,而是放在com.test.action的目录下,那么可以这么写
struts.custom.i18n.resources=com.test.action.messageResources
2。在命名的时候要加上区域和本地方言,即在这个action目录下面建立
messageResources_zh_CN.properties
messageResources_en_US.properties
3。在配置中文的时候不能保存那么我们可以在终端里面
执行native2ascii 这个程序来进行转码,转为ascii码
当然,也可以对文件进行转换native2ascii -encoding utf-8 /home/soft22/132.txt /home/soft14/456.txt
引用

[soft14@hzserver1 ~]$ native2ascii
我是中国人
\u6211\u662f\u4e2d\u56fd\u4eba

引用
jsp页面可以用struts标签的key属性来调用资源文件里面的key
a.了解什么是国际化、Struts2国际化原理(i18n)
b.JSP如何实现国际化、Action国际化支持
JSP:<s:text name="">标签和表单输入性组件<s:textfield key=""/>,不需要在使用label属性

Action: ActionSupport提供了getText()方法
c.资源文件
(熟悉)全局资源文件:在struts.properties或struts.xml中由struts.custom.i18n.resources指定
临时资源文件:由<s:i18n>指定
包范围的资源文件:在任意包下创建package_语言_区域.properties
Action范围的资源文件:在Action同级目录下创建Action类名_语言_区域.properties

<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@page isELIgnored="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  <body>
   <hr>
   	<s:form action="" method="post">
     	<s:textfield key="login.username"></s:textfield><br>
     	<s:textfield key="login.sex"></s:textfield>
     		<s:submit/><s:reset/>
     </s:form>
  </body>
</html>

然后修改浏览器的使用语言,拦截器就可以修改当前客户端的local属性,服务器会去找相应的配置资源文件信息在客户端显示。

也可以实现页面修改

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%@ taglib uri="/struts-tags" prefix="s" >
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function changeLoc(){
		var s = document.getElementById("loc").value;
		window.location.href = "hello.action?loc="+s;
	}
</script>
</head>
<body>
<s:select id="loc" name="loc"  onchange="changeLoc()" list='#{"zh_CN":"中文","en_US":"英文"}'></s:select>
<hr>
<s:text name="msg.hell"></s:text><br>
<s:textfield key="user" name="name"></s:textfield>
<s:fielderror></s:fielderror>
</body>
</html>

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<package name="myfirst1" extends="struts-default">
		<action name="regist" class="tarena.actions.RegistAction">
			<result name="success">/ok.html</result>
			<result name="input">/regist.jsp</result>
		</action>
		<!-- RegistAction-regist_add-validation.xml -->
		<!-- RegistAction-regist_update-validation.xml -->
		<!-- 如果Action有多个业务方法,可以采取以下方法配置
		<action name="regist_*" class="tarena.actions.RegistAction" method="{1}">
			<result name="success">/ok.html</result>
			<result name="input">/regist.jsp</result>
		</action>
		 -->
	<action name="hello" class="tarena.actions.HelloAction">
		<result name="success">/i18n.jsp</result>
		<result name="input">/i18n.jsp</result>
	</action>
	</package>
</struts>

package tarena.actions;
import java.util.Locale;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{

	private String loc;
	public void validate(){
		System.out.println(loc);
		String[] code = loc.split("_");
		Locale locale = new Locale(code[0],code[1]);
		ActionContext.getContext().setLocale(locale);
		this.addFieldError("msg",this.getText("msg.error"));
	}
	public String execute(){
		System.out.println(loc);
		String[] code = loc.split("_");
		Locale locale = new Locale(code[0],code[1]);
		ActionContext.getContext().setLocale(locale);
		return "success";
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics