Json插件与json类型的Result

2 篇文章 0 订阅
1 篇文章 0 订阅

json插件下载

Json插件提供了一中json类型的Result,一旦为某个Action指定了一个类型为json 的Result,则该Result无序映射到任何视图资源,因为Json插件会负责将Action里的状态信息序列化为Json格式的字符串,并将该字符串返回给客户端浏览器。

   简单的说,Json插件允许我们再客户端页面的Javascript中异步调用Action,而且action不再需要使用视图资源来显示该Action 里面的状态信息,而是有Json插件负责将Action里的状态信息返回给调用页面------通过这种方式可以完成Ajax交互。

处理类的Action代码:

import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.Action;
import org.apache.struts2.json.annotations.JSON;
/**

 */
public class JSONExample
{
	//模拟处理结果的属性
	private int[] ints = {10, 20};
	private Map<String , String> map
		= new HashMap<String , String>();
	private String customName = "顾客";
	//封装请求参数的三个属性
	private String field1;
	//'transient'修饰的属性不会被序列化
	private transient String field2;
	//没有setter和getter方法的字段不会被序列化
	private String field3;
	
	public String execute()
	{
		map.put("name", "疯狂Java讲义");
		return Action.SUCCESS;
	}

	//使用注释语法来改变该属性序列化后的属性名
	@JSON(name="newName")
	public Map getMap()
	{
		return this.map;
	}
	
	//customName属性的setter和getter方法
	public void setCustomName(String customName)
	{
		this.customName = customName;
	}
	public String getCustomName()
	{
		return this.customName;
	}

	//field1属性的setter和getter方法
	public void setField1(String field1)
	{
		this.field1 = field1;
	}
	public String getField1()
	{
		return this.field1;
	}

	//field2属性的setter和getter方法
	public void setField2(String field2)
	{
		this.field2 = field2;
	}
	public String getField2()
	{
		return this.field2;
	}

	//field3属性的setter和getter方法
	public void setField3(String field3)
	{
		this.field3 = field3;
	}
	public String getField3()
	{
		return this.field3;
	}
}


    配置该Action 的struts.xml文件如下;

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	<package name="example"  extends="json-default">
		<action name="JSONExample" class="org.crazyit.app.action.JSONExample">
			<!-- 配置类型的json的Result -->
			<result type="json">
				<!-- 为该Result指定参数 -->
				<param name="noCache">true</param>
				<param name="contentType">text/html</param>
				<!-- 设置只序列Action的map属性 -->
				<!--  param name="root">map</param -->
			</result>
		</action>
		<action name="*">
			<result>/WEB-INF/content/{1}.jsp</result>
		</action>
	</package>
</struts>
在上面的配置文件中要 注意两个地方;

     1.配置struts。i18n.encoding时,不再使用GBK编码,而是使用UTF-8编码,这是因为Ajax的post请求都是以UTF-8的方式进行编码的。

2.配置包时,自己的包继承了json-default包,而不再继承默认的default包,这是因为只有在该包先才有json类型的Result。

下面是用prototype.js框架编写的jsp页面。当然先要导入Prototype的代码库。

   


<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>使用JSON插件</title>
	<script src="${pageContext.request.contextPath}/prototype-1.6.1.js" 
	type="text/javascript">
	</script>
	<script type="text/javascript">
		function gotClick()
		{
			//请求的地址
			var url = 'JSONExample.action';
			//将favorite表单域的值转换为请求参数
			var params = Form.serialize('form1');
			//创建Ajax.Request对象,对应于发送请求
			var myAjax = new Ajax.Request(
			url,
			{
				//请求方式:POST
				method:'post',
				//请求参数
				parameters:params,
				//指定回调函数
				onComplete: processResponse,
				//是否异步发送请求
				asynchronous:true
			});
		}
		function processResponse(request)
		{
			//使用JSON对象将服务器响应解析成JSON对象
			var res = request.responseText.evalJSON();
			alert(res);
			//遍历JSON对象的每个属性
			for(var propName in res)
			{
				$("show").innerHTML += propName + " --> " 
					+ res[propName] + "<br/>";
			}		
		}	
	</script>
</head>
<body>
<s:form id="form1">
	<s:textfield name="field1" label="Field 1"/>
	<s:textfield name="field2" label="Field 2"/>
	<s:textfield name="field3" label="Field 3"/>
	<tr><td colspan="2">
	<input type="button" value="提交" οnclick="gotClick();"/>
	</td></tr>
</s:form>
<div id="show">
</div>
</body>
</html>



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值