JSON的优点众所周知,在此不作叙述.
JSON在AJAX的应该比较的多些,下面结合一下struts2.1.6和大家分享一下
1,需要把jsonplugin-0.33.jar拷贝到Web-INF/lib目录下.
关于这个JAR包是JSON在struts2.1.6插件包.里面有我们要用到的struts-plugin.xml,关于这个JAR包,经过本人的测试和网上的搜索,得出最新的0.7版本在cglib方面存在着问题,建议大家使用0.33版本.
2,在struts2.1.6的配置文件中进行配置,就是返回的类型是json,并且要为JSON数据单独创建一个Package,继承的包不再是struts-default,而是json-default
示例代码如下:
- < package name = "json" namespace = "/json" extends = "json-default" >
- <!-- 测试Json的例子 -->
- < action name = "jsonTest_*" class = "jsonTest" method = "{1}" >
- < result name = "jsonres" type = "json" />
- </ action >
- </ package >
3,前台页面用Ajax接收到的是JSON数据格式,
{"hashMap":{"name":"wangsh","pass":"qihuan"},"pass":"这是密码啊Password","username":"wangsh"}.
本人的前采用的是Jquery1.2.6中的Ajax Post方式异步提交的.
示例代码如下:
- < %@ page language = "java" import = "java.util.*" pageEncoding = "UTF-8" % >
- < %@ page contentType = "text/html; charset=UTF-8" % >
- < %@ taglib prefix = "s" uri = "/struts-tags" % >
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- < html >
- < head >
- < title > < s:text name = "%{getText('regsiterpagetitle')}" />
- </ title >
- < meta http-equiv = "pragma" content = "no-cache" >
- < meta http-equiv = "cache-control" content = "no-cache" >
- < meta http-equiv = "expires" content = "0" >
- < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
- < meta http-equiv = "description" content = "This is my page" >
- < SCRIPT type = "text/javascript"
- src = "${pageContext.request.contextPath}/common/resource/jquery-1.2.6.js" mce_src = "${pageContext.request.contextPath}/common/resource/jquery-1.2.6.js" > </ SCRIPT >
- < SCRIPT type = "text/javascript" >
- function testJosn()
- {
- $.post(
- "${pageContext.request.contextPath}/json/jsonTest_jsontest.html",
- {username:"wangsh"},
- function (data)
- {
- window.alert(data+"=====");
- eval("json ="+data);
- window.alert("----------"+json.username);
- $("#testjson").html(data);
- }
- );
- }
- </ SCRIPT >
- </ head >
- < body >
- < a
- href = "${pageContext.request.contextPath}/json/jsonTest_jsontest.html" mce_href = "${pageContext.request.contextPath}/json/jsonTest_jsontest.html" > 测试Json </ a >
- < button name = "测试JSON" id = "testbut" onclick = "testJosn();" >
- 测试JSON
- </ button >
- < div id = "testjson" > </ div >
- </ body >
- </ html >
可能细心的朋友已看到了:
eval("json="+data);
window.alert("----------"+json.username);
以上的代码是何义呢?
Data是Action返回的数据.
以下是Action的代码:
- package com.wang.struts2.action;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import com.opensymphony.xwork2.ActionSupport;
- public class JosnTestAction extends ActionSupport
- {
- private Log log = LogFactory.getLog(JosnTestAction. class );
- private String username;
- private String pass;
- private Map<String, String> hashMap = new HashMap<String, String>();
- public String getUsername()
- {
- return username;
- }
- public void setUsername(String username)
- {
- this .username = username;
- }
- public Map<String, String> getHashMap()
- {
- return hashMap;
- }
- public void setHashMap(Map<String, String> hashMap)
- {
- this .hashMap = hashMap;
- }
- public String getPass()
- {
- return pass;
- }
- public void setPass(String pass)
- {
- this .pass = pass;
- }
- public String jsontest() throws Exception
- {
- hashMap.put("name" , this .getUsername());
- hashMap.put("pass" , "qihuan" );
- this .setPass( "这是密码啊Password" );
- log.info("===========测试JSON============" );
- return "jsonres" ;
- }
- }
也就是说服务器返加在的数据是所有的Action属性,当然要提供读写器(get和Set方法)的.
我们可以采用简单的两行代码
eval("json="+data);//将JSON转化为一个对象,并且名为joson
window.alert("----------"+json.username);//打印出Username的值
window.alert("----------"+json.hashMap.pass);//拿到的是Map中的某个值.
可以很方便的取得JSON中的任何一个属性.