六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 254|回复: 0

Jsonplugin结合struts2使用说明

[复制链接]

升级  24%

22

主题

22

主题

22

主题

秀才

Rank: 2

积分
86
 楼主| 发表于 2013-1-29 08:52:27 | 显示全部楼层 |阅读模式
资料:http://clayz.iteye.com/blog/422125


配置中的参数含义:
 
root参数用于指定要序列化的根对象,如果省去这一配置,表示要序列化action中的所有属性
ignoreHierarchy false时表示要序列化根对象的所有基类
excludeProperties表示排除的序列化的属性
includeProperties表示哪些属性被序列化
 
Action配置:
 
<!-- jsonplugin的使用配置 --> 
        <!-- package要继承json-default 以加载json插件 --> 
        <action name="jsonAct" class="cn.enjoylife.prac.action.JsonAction"> 
            <result type="json"> 
                <!-- root参数用于指定要序列化的根对象,如果省去这一配置,表示要序列化action中的所有属性 --> 
                <param name="root">map</param> 
                <!-- ignoreHierarchy false时表示要序列化根对象的所有基类 --> 
                <param name="ignoreHierarchy">false</param> 
                <!-- 排除hello属性,使其不被序列化 --> 
                <param name="excludeProperties">hello</param> 
            </result> 
        </action>
 
JsonAction.java:
 
/**
 * @author Evan
 * 
 */ 
public class JsonAction extends ActionSupport { 
    private static final long serialVersionUID = 3870310687165227565L; 
    private int[] ints = { 100, 200 }; 
    private Map<String, Object> map = new HashMap<String, Object>(); 
    private String hello = "helloWorld"; 
    private String username = "evan";//没有getter方法,不会被序列化  
    private String password = "pwd"; 
    private String validateCode = "123456"; 
    public int[] getInts() { 
        return ints; 
    } 
    public void setInts(int[] ints) { 
        this.ints = ints; 
    } 
    @JSON(name="newMap")//重新命名  
    public Map<String, Object> getMap() { 
        return map; 
    } 
    public void setMap(Map<String, Object> map) { 
        this.map = map; 
    } 
    public String getHello() { 
        return hello; 
    } 
    public void setHello(String hello) { 
        this.hello = hello; 
    } 
    public void setUsername(String username) { 
        this.username = username; 
    } 
    @JSON(serialize=true)//这是默认值,可以省去  
    public String getPassword() { 
        return password; 
    } 
    public void setPassword(String password) { 
        this.password = password; 
    } 
    @JSON(serialize=false)//serialize参数为false,不会被序列化  
    public String getValidateCode() { 
        return validateCode; 
    } 
    public void setValidateCode(String validateCode) { 
        this.validateCode = validateCode; 
    } 
    @Override 
    public String execute() throws Exception { 
        map.put("info", "今天的雨真大啊!"); 
        return SUCCESS; 
    } 

 
使用jquery操作返回的json串:
 
$(document).ready(function(){ 
    $.getJSON( 
        "./jsonAct.action",  
        function(data){ 
            alert(data.info); 
        } 
    ); 
});
 
Annotation:
 

<span style="">

  • package com.zheshou.action;   
  •   
  • import javax.annotation.Resource;   
  •   
  • import org.apache.struts2.convention.annotation.Action;   
  • import org.apache.struts2.convention.annotation.Namespace;   
  • import org.apache.struts2.convention.annotation.ParentPackage;   
  • import org.apache.struts2.convention.annotation.Result;   
  • import com.opensymphony.xwork2.ActionSupport;   
  • import com.zheshou.model.Dept;   
  • import com.zheshou.model.User;   
  • import com.zheshou.service.DeptService;   
  • import com.alibaba.fastjson.JSON;   
  •   
  • @ParentPackage(value="json-default")   
  • @Namespace(value="/")   
  • public class SelectDeptAction extends ActionSupport {//2.ActionSupport必须有这个才行.但是可以不用Execute   
  •        
  •     private DeptService deptService;   
  •        
  •     private String beginTime;   
  •     private String endTime;   
  •     private String Emps;   
  •     private String result;   
  •        
  •     public DeptService getDeptService() {   
  •         return deptService;   
  •     }   
  •     @Resource(name="deptService")   
  •     public void setDeptService(DeptService deptService) {   
  •         this.deptService = deptService;   
  •     }      
  •        
  •     public String getBeginTime() {   
  •         return beginTime;   
  •     }   
  •   
  •     public void setBeginTime(String beginTime) {   
  •         this.beginTime = beginTime;   
  •     }   
  •   
  •     public String getEndTime() {   
  •         return endTime;   
  •     }   
  •   
  •     public void setEndTime(String endTime) {   
  •         this.endTime = endTime;   
  •     }   
  •   
  •     public String getEmps() {   
  •         return Emps;   
  •     }   
  •   
  •     public void setEmps(String emps) {   
  •         Emps = emps;   
  •     }   
  •     public String getResult() {   
  •         return result;   
  •     }   
  •   
  •     public void setResult(String result) {   
  •         this.result = result;   
  •     }   
  •   
  •     @Action(value="selectdept"  
  •         ,results={@Result(   
  •                 type="json"  
  •                 ,params={"contentType","text/html","root","result"}//root<会带外引号>    
  • //includeProperties Result不带字符引号   
  • //excludeProperties 返回所有的属性   
  •                     )}   
  •                )   
  •     public String execute(){   
  •         //result= JSON.toJSONString(user).replaceAll("\"", "'");   
  •         result=deptService.Select(Emps);   
  •         System.out.print(result);   
  •         return SUCCESS;   
  •     }   
  • }
***********************************************************************************
 

在Struts 2中使用JSON Ajax支持

  JSON插件提供了一种名为jsonResultType,一旦为某个Action指定了一个类型为jsonResult,则该Result无需映射到任何视图资源。将下载到的jsonplugin-0.7.jar文件复制到Web应用的WEB-INF路径下,即可完成JSON插件的安装。
  JSON插件提供了一种名为jsonResultType,一旦为某个Action指定了一个类型为jsonResult,则该Result无需映射到任何视图资源。因为JSON插件会负责将Action里的状态信息序列化成JSON格式的数据,并将该数据返回给客户端页面的JavaScript
  简单地说,JSON插件允许我们在JavaScript中异步调用Action,而且Action不再需要使用视图资源来显示该Action里的状态信息,而是由JSON插件负责将Action里的状态信息返回给调用页面——通过这种方式,就可以完成Ajax交互。
  Struts2提供了一种可插拔方式来管理插件,安装Struts2JSON插件与安装普通插件并没有太大的区别,一样只需要将Struts2插件的JAR文件复制到Web应用的WEB-INF/lib路径下即可。
  安装JSON插件按如下步骤进行:
  (1)登陆http://code.google.com/p/jsonplugin/downloads/list站点,下载Struts2JSON插件的最新版本,当前最新版本是0.7,我们可以下载该版本的JSON插件。
  (2)将下载到的jsonplugin-0.7.jar文件复制到Web应用的WEB-INF路径下,即可完成JSON插件的安装。
  实现Actio逻辑
  假设wo,en输入页面中包含了三个表单域,这三个表单域对于三个请求参数,因此应该使用Action来封装这三个请求参数。三个表单域的name分别为field1field2field3
  处理该请求的Action类代码如下:  
public class JSONExample 
  
  //封装请求参数的三个属性 
  private String field1; 
  private transient String field2; 
  private String field3; 
  //封装处理结果的属性 
  private int[] ints = {10, 20}; 
  private Map map = new HashMap(); 
  private String customName = "custom"; 
  //三个请求参数对应的settergetter方法 
  public String getField1() 
  
  return field1; 
  
  public void setField1(String field1) 
  
  this.field1 = field1; 
  
  //此处省略了field1field2两个字段的settergetter方法 
  ... 
  //封装处理结果的属性的settergetter方法 
  public int[] getInts() 
  
  return ints; 
  
  public void setInts(int[] ints) 
  
  this.ints = ints; 
  
  public Map getMap() 
  
  return map; 
  
  public void setMap(Map map) 
  
  this.map = map; 
  
  //使用注释语法来改变该属性序列化后的属性名 
  @JSON(name="newName") 
  public String getCustomName() 
  
  return this.customName; 
  
  public String execute() 
  
  map.put("name", "yeeku"); 
  return Action.SUCCESS; 
  
  }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表