日期:2014-05-16  浏览次数:20416 次

如何指定struts2返回我们需要的json类型数据?

如何指定struts2返回我们需要的json类型数据?

今天下午做项目的时候,明明可以查到数据,可是就是没有在客户端进行显示,把所有前台代码扫了几遍,都不能找到原因,通过查看返回的json代码,发现有很多多余的返回内容,也就是这些多余的返回内容造成了前台解析的错误,经过上网查询找到了解决的办法:

处理请求的Action:

?

package com.hpu.action;

import java.util.List;

import javax.annotation.Resource;

import com.hpu.pojo.Commodity;
import com.hpu.service.CommodityService;
import com.opensymphony.xwork2.ActionSupport;

public class CommodityAction extends ActionSupport {
	
	private Double agio;
	private List<Commodity> commodities;
	private CommodityService commodityService;
	
	private Integer limit;
	private String msg;
	private String name;
	
	private Double price;
	private Integer recordSize;
	
	private Integer start;
	
	private boolean success;

	/**商品信息录入
	 * @author qinrui
	 * @see commodityService
	 * @return String
	 */
	public String add() {
		// 根据商品名获取商品
		Commodity commodity = commodityService.loadByName(name);
		
		// 商品不存在,将新商品保存起来
		if(commodity == null) {
			// 创建商品对象 
			commodity = new Commodity();
			// 设定商品属性
			commodity.setName(name);
			commodity.setPrice(price);
			commodity.setAgio(agio);
			// 保存商品
			commodityService.save(commodity);
			success = true;
			msg = "商品录入成功";
		} else {
			success = false;
			msg = "商品已经存在";
		}
		
		return SUCCESS;
	}
	
	public Double getAgio() {
		return agio;
	}

	public List<Commodity> getCommodities() {
		return commodities;
	}

	public Integer getLimit() {
		return limit;
	}

	public String getMsg() {
		return msg;
	}

	public String getName() {
		return name;
	}

	public Double getPrice() {
		return price;
	}

	public Integer getRecordSize() {
		return recordSize;
	}

	public Integer getStart() {
		return start;
	}

	public boolean isSuccess() {
		return success;
	}

	public String query() {
		recordSize = commodityService.loadCommodities().size();
		commodities = commodityService.loadCommodities(start , limit);
		
		for(Commodity c : commodities) {
			System.out.println(c.getName());
		}
		return SUCCESS;
	}

	public void setAgio(Double agio) {
		this.agio = agio;
	}

	public void setCommodities(List<Commodity> commodities) {
		this.commodities = commodities;
	}

	@Resource(name="commodityServiceImpl")
	public void setCommodityService(CommodityService commodityService) {
		this.commodityService = commodityService;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public void setRecordSize(Integer recordSize) {
		this.recordSize = recordSize;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}
}

?我需要查询所有的Commodity,所有要调用query方法,也就是我要返回的内容只需要commodities与recordCode,就行了,可是如果不在struts.xml进行相应的配置,它就会返回所有内容,包括值为null的,那怎样才能让它按照自己的需求返回值呢?下面给出相应的strtus.xml配置:

?

?

<action name="commodity_query" class="com.hpu.action.CommodityAction" method="query">
	<result type="json">
		<param name="includeProperties">commodities.*,recordSize</param>
	</result>
</action>

?解释:由于commodities是个集合类型,如果只写上commodities的话,就会返回一个空的集合,而要返回集合里面的元素就要改成commodities.*,而要返回值中间使用逗号作为间隔符,includeProperties表示要返回内容,如果想要去掉不想要的内容使用excludeProperties即可。

?

参考文章:http://biancheng.dnbcw.info/java/408867.html

?