Struts2之灵活返回Json,摆脱注解和配置文件

简单继承StrutsResultSupport以便之后扩展
package com.wesley.framework.struts;

import org.apache.struts2.dispatcher.StrutsResultSupport;

public abstract class BaseResult extends StrutsResultSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -589433181928883640L;

	public BaseResult() {
		super();
	}

	public BaseResult(String location) {
		super(location);
	}

	public boolean isAjax = false;

	public boolean isAjax() {
		return isAjax;
	}

	public void setAjax(boolean isAjax) {
		this.isAjax = isAjax;
	}

}

重新返回操作方法doExecute
package com.wesley.framework.struts;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONUtil;

import com.opensymphony.xwork2.ActionInvocation;
import com.wesley.framework.action.BaseAction;
import com.wesley.framework.commen.json.StrutsJsonBean;

public class JsonResult extends BaseResult {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3291151711598040681L;
	protected boolean isAjax = true;

	@Override
	protected void doExecute(String arg0, ActionInvocation invocation)
			throws Exception {
		// 定义上下文的环境
		// ServletContext sc = ServletActionContext.getServletContext();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding(request.getCharacterEncoding());
		System.out.println(request.getCharacterEncoding());
		// 设置返回类型
		response.setContentType("application/json");
		PrintWriter output = response.getWriter();// 获取响应打印
		BaseAction baseAction = (BaseAction) invocation.getAction();// 获得基础Action类
		StrutsJsonBean strutsJsonBean = baseAction.getStrutsJsonBean();
		JSONUtil.serialize(output, strutsJsonBean.getRoot(),
				strutsJsonBean.getExcludeProperties(),
				strutsJsonBean.getIncludeProperties(),
				strutsJsonBean.getExcludeNullProperties());
		output.close();
	}
}

StrutsJsonBean封装了主要的输出Json的几个属性,放在BaseAction中传输参数
package com.wesley.framework.commen.json;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.json.JSONUtil;

import com.wesley.framework.commen.StringFormat;

public class StrutsJsonBean {
	public static final String REGEXP_PATTERN = "regexp";

	private Object root;
	private java.util.Collection<Pattern> excludeProperties;
	private java.util.Collection<Pattern> includeProperties;
	private Boolean excludeNullProperties = true;

	public StrutsJsonBean() {
		super();
	}

	public StrutsJsonBean(Object root, String excludeProperties,
			String includeProperties) {
		super();
		this.root = root;
		this.setExcludeProperties(excludeProperties);
		this.setIncludeProperties(includeProperties);
	}

	StrutsJsonBean(Object root, String excludeProperties,
			String includeProperties, Boolean excludeNullProperties) {
		super();
		this.root = root;
		this.setExcludeProperties(excludeProperties);
		this.setIncludeProperties(includeProperties);
		this.excludeNullProperties = excludeNullProperties;
	}

	public StrutsJsonBean(Object root, Collection<Pattern> excludeProperties,
			Collection<Pattern> includeProperties, Boolean excludeNullProperties) {
		super();
		this.root = root;
		this.excludeProperties = excludeProperties;
		this.includeProperties = includeProperties;
		this.excludeNullProperties = excludeNullProperties;
	}

	public Object getRoot() {
		return root;
	}

	public void setRoot(Object root) {
		this.root = root;
	}

	public java.util.Collection<Pattern> getExcludeProperties() {
		return excludeProperties;
	}

	public Collection<Pattern> getIncludeProperties() {
		return includeProperties;
	}

	public void setIncludeProperties(String commaDelim) {
		includeProperties = JSONUtil.processIncludePatterns(
				JSONUtil.asSet(commaDelim), REGEXP_PATTERN);
	}

	public Boolean getExcludeNullProperties() {
		return excludeNullProperties;
	}

	public void setExcludeProperties(String commaDelim) {
		Set<String> excludePatterns = JSONUtil.asSet(commaDelim);
		if (excludePatterns != null) {
			this.excludeProperties = new ArrayList<Pattern>(
					excludePatterns.size());
			for (String pattern : excludePatterns) {
				this.excludeProperties.add(Pattern.compile(pattern));
			}
		}
	}

	public static StrutsJsonBean newRowInstance(Object list,
			String... beanPoperties) {
		StrutsJsonBean struts = new StrutsJsonBean(list, null, null);
		if (ArrayUtils.isEmpty(beanPoperties))
			beanPoperties = new String[0];
		String[] temp = new String[beanPoperties.length];
		for (int i = 0; i < beanPoperties.length; i++) {
			temp[i] = StringFormat.softLink("Rows\\[?\\d*\\]?\\.",
					StringUtils.replace(beanPoperties[i], ".", "\\."));
		}
		String[] baseInfo = { "pagesize", "Total", "page" };
		temp = (String[]) ArrayUtils.addAll(temp, baseInfo);
		struts.setIncludeProperties(StringFormat.formatByComma(temp));
		return struts;
	}

}

在Struts2的配置文件中添加返回类型
<result-types>
			<result-type name="jsonresult"
				class="com.wesley.framework.struts.JsonResult" />
		</result-types>

完成之后就可以在Action中调用返回类型了
@Action(value = "json", results = { @Result(name = SUCCESS, type = JSON_RESULT) })

你可能感兴趣的:(java,json,struts,struts2,result)