Json插件与json类型的Result

来源:互联网 发布:道亨软件说明书 编辑:程序博客网 时间:2024/04/28 20:44

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,{//请求方式:POSTmethod:'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="提交" onclick="gotClick();"/></td></tr></s:form><div id="show"></div></body></html>



1 0
原创粉丝点击