struts2国际化在js里面怎么使用

JavaScript013

struts2国际化在js里面怎么使用,第1张

一、struts2国际化比较简单,过程如下:

1. 建立资源文件

中文资源:language_zh_CN.properties (language-自定义名称,zh-中文,CN-中国)

英文资源:language_en_US.properties (language保持一致,en-英文,US-美国)

其内容就是键值对,俩资源文件的键名对应保持一致,值分别对应中文与英文

其中中文在MyEclipse环境下,用Properties进行编辑,直接输入中文

如:password 密码

那么从Source看时,就可看到中文是用Unicode表示的

如:password=\u5BC6\u7801

2. 配置

struts.xml文件中,配置i18n的源,值取资源文件的自定义名称部分(如果资源文件不是在src下,记得加上包路径):

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

web.xml文件中,配置Struts2:

<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>

3. jsp中使用资源

加入标签引用:<%@ taglib prefix="s" uri="/struts-tags" %>

s:text标签用健名取资源值:<s:text name="password"/>

或者表单元素用:key="password"

4. 在action中使用资源

用ActionSupport类中的方法取资源值:getText("password")

二、js中要获取以上的资源值,就比较麻烦了

单独建立一套资源的话,就需要维护两套资源,重复且维护麻烦

1. js中使用量较少时,可在jsp页面中定义变量先取出资源值

用js代码如:var curPassword='<s:text name="password"/>'

或:var curLang = {

password : '<s:text name="password"/>',

user : '<s:text name="user"/>'

}

那单独的js文件中就可直接使用了

2. 另外想到的是从Java资源中全部取出,在js中再建立一个语言的对象

struts国际化,是建立在java国际化之上,做了一次封装

java国际化,用到的有两个类:Locale、ResourceBundle

Locale是语言类,选定当前的语言与地域,如:Locale locale = new Locale("zh","CN")

ResourceBundle是资源类,加载的是资源

如:ResourceBundle bundle = new ResourceBundle.getBundle("language",locale)

有个方法可取键值:bundle.getString("password")

找到一个方法可取全部键名:Set<String>keys=bundle.keySet()

好,再看Struts2中如何取ResourceBundle对象:

ActionSupport类中方法:ResourceBundle bundle=getTexts("langauge")

Action中添加方法:

protected String getCurLang(String resourceName){

ResourceBundle bundle=getTexts(resourceName)

if(null != bundle){

Set<String>keys=bundle.keySet()

Iterator<String>it = keys.iterator()

StringBuffer curLang = new StringBuffer()

curLang.append("{")

int id=0

String key

while(it.hasNext()){

key=it.next()

if(id>0){

curLang.append(",")

}

curLang.append(key)

curLang.append(":\'")

curLang.append(getText(key))

curLang.append("\'")

id++

}

curLang.append("}")

return curLang.toString()

}

return ""

}

然后放入session中:session.setAttribute("curLang",getCurLang("langauge"))

或前台调用Ajex通讯取得

最后,前台把它转为对象,如:

var strvar curLang="<%=session.getAttribute("curLang")%>"

那么js文件中就可直接使用了:curLang["password"]

<script type="text/javascript">

function dofocus(obj) {

if (obj.value == "请输入数字类型的编号") {

obj.value = ""

}

}

function doblur(obj) {

if (obj.value == "") {

obj.value = "请输入数字类型的编号"

}

}

</script>

<s:textfield label="编号" name="userid" value="请输入数字类型的编号" onfocus="dofocus(this)" onblur="doblur(this)" />

以上代码测试可用。

如果不用jquery的话,那就用原生的javascript的ajax吧。

我把代码贴给你,自己看

/**

* ajax请求

*/

function ajax(url) {

var xmlhttp = null

if (window.ActiveXObject) {

//IE5 IE6是以activexobject的方式引入xmlhttprequset对象的

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")

} else if (window.XMLHttpRequest) {

//除ie5 IE6以外的浏览器xmlhttprequset是window的子对象

xmlhttp = new XMLHttpRequest()

}

if (!xmlhttp) {

alert("创建xmlhttp对象异常")

return false

}

// xmlhttp.open("POST", "user.action?id=33", false)//准备向服务器的Getdate1发送post请求

xmlhttp.open("GET", url, false)//中文要用encodeURI

xmlhttp.onreadystatechange = function () {

if (xmlhttp.readyState == 4) { //请求完成加载

if (xmlhttp.status == 200) {//响应已经成功

if(xmlhttp.responseText == "success"){ //xmlhttp.responseText这里面是你返回的字符串

alert("成功")

}

} else {

alert("没有查询到信息!")

}

}

}

xmlhttp.send() // 这时才开始发送请求

}

应该看得懂的