`
zhangfeilo
  • 浏览: 390812 次
  • 性别: Icon_minigender_1
  • 来自: 昆明
社区版块
存档分类
最新评论

struts2,json,prototype异步请求

阅读更多

1、index.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!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="prototype-1.6.0.3.js" type="text/javascript">
</script>
<script src="json2.js" type="text/javascript">
</script>
<script type="text/javascript">
 function gotClick()
 {
                alert('ss');
  //请求的地址
  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 = JSON.parse(request.responseText);
  //遍历JSON对象的每个属性
  for(var propName in res)
  {
   $("show").innerHTML += propName + " --> " 
    + res[propName] + "<br />";
  }  
 } 
</script>
</head>
<body>
<form id="form1" name="form1" method="post">
field1:<input type="text" name="field1" id="field1"/><br />
field2:<input type="text" name="field2" id="field2"/><br />
field3:<input type="text" name="field3" id="field3"/><br />
<input type="button" value="提交" onClick="gotClick();"/>
</form>
<div id="show">
</div>
</body>
</html>

 2、S<?xml version="1.0" encoding="GBK"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <constant name="struts.i18n.encoding" value="UTF-8"/>
 <package name="example"  extends="json-default">
  <action name="JSONExample" class="lee.JSONExample">
   <result type="json"/>
  </action>
  <action name="">
   <result>.</result>
  </action>
 </package>
</struts>

3、JSONExample Action类

 

package lee;

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

import com.opensymphony.xwork2.Action;
import com.googlecode.jsonplugin.annotations.JSON;
 
public class JSONExample
{
 //模拟处理结果的属性
    private int[] ints = {10, 20};
    private Map map = new HashMap();
    private String customName = "顾客";
 //封装请求参数的三个属性
    private String field1;
    private String field2;
    //没有setter和getter方法的字段不会被序列化
    private String field3;
 public String execute()
 {
  map.put("name", "李刚");
  return Action.SUCCESS;
 }
 //使用注释语法来改变该属性序列化后的属性名
 @JSON(name="newName")
 public Map getMap()
 {
  return this.map;
 }
 //ints属性的getter方法
 public int[] getInts()
 {
  return this.ints;
 }

 //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;
 }
 

 

}

 5.如果按照3中的配置。你会发现前台返回的json字符串,是把action中的所有属性全部转化为json字符串返回给浏览器了(甚至有时候返回不了结果,也不报错,后台执行了,但前台执行不到callback function),但是我们有时候需要根据实际情况返回部分结果,如何对json的结果进行定制输出呢?result提供了一些参数替你解决这个问题,一般情况下用的最多的就是includeProperties 参数和excludeNullProperties参数。当然还有其他的方法,如给pojo的属性加json注解。 
6.includeProperties 参数:输出结果中需要包含的属性值,这里正则表达式和属性名匹配,可以用“,”分割填充多个正则表达式。这个参数直接返回对象的json数据,前台不需要eval转换,<param name="root">result</param>则不同,需要前台进行eval转换 
如:输出person的所有属性 
<result type="json">   
   <param name="includeProperties">person.*, person\.name</param>   
</result>   
7.excludeProperties 参数:输出结果需要剔除的属性值,也支持正则表达式匹配属性名,可以用“,”分割填充多个正则表达式,类同includeProperties 
8.输出一个JSON List列表 
<action name="list" class="testAction" method="list">     
<result name="success" type="json"> 
<param name="includeProperties"> 
list\[\d+\]\.Id,list\[\d+\]\.user\.userName 
</param> 
</result>        
</action>   
其中list是action中的一个List类型的属性 
list\[\d+\]\.Id表示,list中存储的对象0..end的Id属性(list中存储的对象必须有Id属性)。 
list\[\d+\]\.user\.userName就表示list中的对象中的user对象的userName属性 
9.为什么要用includeProperties或者excludeProperties 参数: 
主要是为了过滤掉接口,pojo的set、list、其他对象等不需要的数据防止循环取其他对象或找不到。如果不配置,默认是处理action中的所有属性,如果action中有接口注入,json拦截器可能找不到返回不了结果,还有如果action中有一个对象,这个对象与好多对象都有关联,json拦截器会将相关联的所有对象的属性全部转换成json格式,如果其他对象有list、set,其返回结果...有可能是死循环,无法返回 
10.总结: 
action中避免使用get开头的action方法,去掉action中的接口的get方法 
为json类型的result配置includeProperties, excludeProperties等参数. 

 

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics