自定义Struts 返回json的2中方式

这是我们自己定义的。第一种方式

首先我们配置struts.xml

<struts>

<package name="myresult" extends="struts-default">

<result-types>

<result-type name="myjson" class="com.res.myResult"></result-type>

</result-types>

</package>

</struts>   

 返回类型的名字=myjson   相当于type=”myjson”

返回的JSon传到相应的类进行处理  

 

package com.res;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.dispatcher.ServletRedirectResult;

import com.opensymphony.xwork2.ActionInvocation;

public class myResult extends ServletRedirectResult {

//这个类用来接受传过来JSON并进行处理。

@Override

protected void doExecute(String arg0, ActionInvocation arg1)

throws Exception {

// TODO Auto-generated method stub

HttpServletResponse   

response=(HttpServletResponse) arg1.getInvocationContext().get(ServletRedirectResult.HTTP_RESPONSE);

response.setCharacterEncoding("UTF-8");

response.setContentType("text/html");

PrintWriter out= response.getWriter();

out.print("-------------out------------------>>");

out.print(arg1.getResultCode());

out.flush();

out.close();

super.doExecute(arg0, arg1);

}

}

 

 

接受请求(url,也就是入口,我们从这里可以返回一个JSon格式

package com.act;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.ParentPackage;

import org.apache.struts2.convention.annotation.Result;

import pojo.Voteinfo;

 

import biz.IVoteBiz;

import biz.VoteBiz;

import com.google.gson.Gson;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("myresult")

public class MyResultAction extends ActionSupport{

private IVoteBiz iVoteBiz = new VoteBiz();

private Gson gson = new Gson();

@Override

public String execute() throws Exception {

// TODO Auto-generated method stub

return super.execute();

}

@Action(value="test" ,results={@Result(name="*",type="myjson")})

public String Test(){

System.out.println("test---------------->>");

Voteinfo voteinfo = iVoteBiz.findVoteById(1);

return gson.toJson(voteinfo);

}

}

 

返回格式 只会返回对象里面的内容 不会将对象也返回过来

{"actTime":"2015-05-30T00:00:00","outTime":"2015-05-29T00:00:00","suid":1,"vid":1,"voteFlag":1,"voteSub":"周末活动"}

 

 

 

 

这是系统自带的。第二种方式

 

 

我采用注释配置法,也就是元数据 也就注释

package com.act;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.ParentPackage;

import org.apache.struts2.convention.annotation.Result;

import pojo.Voteinfo;

import biz.IVoteBiz;

import biz.VoteBiz;

import com.google.gson.Gson;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("json-default")

public class jsonResultAction extends ActionSupport {

Voteinfo voteinfo ;

private IVoteBiz iVoteBiz = new VoteBiz();

private Gson gson = new Gson();

@Override

public String execute() throws Exception {

// TODO Auto-generated method stub

return super.execute();

}

@Action(value="test2" ,results={@Result(type="json")})

public String Test(){

System.out.println("test---------------->>");

 voteinfo = iVoteBiz.findVoteById(1);

return SUCCESS;

}

//一定要有get方法才可以将voteinfo 转成json格式

public Voteinfo getVoteinfo() {

return voteinfo;

}

}

 

返回格式 会返回对象里面的内容 并且也会将对象名字也返回过来

{"voteinfo":{"actTime":"2015-05-30T00:00:00","outTime":"2015-05-29T00:00:00","suid":1,"vid":1,"voteFlag":1,"voteSub":"周末活动"}}

要接受从URL传过来的数据我们需要set方法,不然总是为空值

 

 

 

package com.act;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.ParentPackage;

import org.apache.struts2.convention.annotation.Result;

import pojo.Voteinfo;

import biz.IVoteBiz;

import biz.VoteBiz;

import com.google.gson.Gson;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("json-default")

public class jsonResultAction extends ActionSupport {

Voteinfo voteinfo ;

private int id;

private IVoteBiz iVoteBiz = new VoteBiz();

private Gson gson = new Gson();

@Override

public String execute() throws Exception {

// TODO Auto-generated method stub

return super.execute();

}

@Action(value="test2" ,results={@Result(type="json",params={"excludeProperties","id"})})

public String Test(){

System.out.println("test---------------->>");

 voteinfo = iVoteBiz.findVoteById(1);

return SUCCESS;

}

public Voteinfo getVoteinfo() {

return voteinfo;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

}

 

若是你get了某值又不想他作为json格式输出,那么我们可以通过配置@Action(value="test2" ,results={@Result(type="json",params={"excludeProperties","id"})})

输出为

{"voteinfo":{"actTime":"2015-05-30T00:00:00","outTime":"2015-05-29T00:00:00","suid":1,"vid":1,"voteFlag":1,"voteSub":"周末活动"}}

 

若是我们不需要对象名,但是我们又必须使用get方法,那么我们可以将对象名作为根,那么打印的就是

{"actTime":"2015-05-30T00:00:00","outTime":"2015-05-29T00:00:00","suid":1,"vid":1,"voteFlag":1,"voteSub":"周末活动"}

那么我们到底进行什么操作呢

其实很简单,我们就是在params里面添加了多一个属性,那就是根,root具体写法如下

@Action(value="test2" ,results={@Result(type="json",params={"excludeProperties","id","root","voteinfo"})})

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值