Struts2自定义Result处置JSON

曾经在选用Struts2开发的项目中,对JSON的处置一向都在Action里处置的,在Action中直接Response,比来研读了一下Struts2的源码,发现了一个愈加高雅的解决办法,个人界说一个ResultType, 首要咱们先看下Struts2中的源码 包com.opensymphony.xwork2下的DefaultActionInvocation 472行     /**
     * http://www.star1111.info/linked/20130309.do  Save the result to be used later.
     * @param actionConfig current ActionConfig
     * @param methodResult the result of the action.
     * @return the result code to process.
     */
    protected String saveResult(ActionConfig actionConfig, Object methodResult) {
        if (methodResult instanceof Result) {
            this.explicitResult = (Result) methodResult;

            // Wire the result automatically
            container.inject(explicitResult);
            return null;
        } else {
            return (String) methodResult;
        }
    } 若是resultType完成了Result接口,则履行 this.explicitResult = (Result) methodResult;

            // Wire the result automatically
            container.inject(explicitResult);
            return null; 如今咱们来界说一个接口(JsonResult)来处置通常的POJO目标 package com.kiloway.struts;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;

import com.opensymphony.xwork2.ActionInvocation;

public class JsonResult extends StrutsResultSupport {

	private Object result;
	private JsonConfig jsonConfig;

	public Object getResult() {
		return result;
	}

	public JsonResult(JsonConfig jsonConfig) {
		super();
		this.jsonConfig = jsonConfig;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	private static final long serialVersionUID = 7978145882434289002L;

	@Override
	protected void doExecute(String finalLocation, ActionInvocation invocation)
			throws Exception {
		HttpServletResponse response = null;
		try {
			response = ServletActionContext.getResponse();
			PrintWriter printWriter = response.getWriter();
			if (jsonConfig != null) {
				printWriter.write(JSONObject.fromObject(result).toString());
			} else {
				printWriter.write(JSONObject.fromObject(result, jsonConfig)
						.toString());
			}
		}catch(Exception e){
			throw new Exception("json parse error!");
		} finally {
			response.getWriter().close();
		}

		

	}
}
 JsonReulst界说好了该怎么让Struts处置呢? 咱们在struts.xml里边能够这样界说 
	    
	        
	    

		
			
		
	 reuslt的name能够恣意,但type有必要和你注册的ResultType一样。 Action 中直接这样调用 public JsonResult getJson()
	{
		UserInfo f = new UserInfo();
		f.setName("小睿睿");
		f.setPassword("哈哈");
		JsonResult jsonResult  = new JsonResult();
		jsonResult.setResult(f);
		return jsonResult;
	} 在咱们的Action代码中就不必response.write了,彻底交给了Reuslt目标去向置了(doExecute) 这样就很便利的处置了JSON格局的数据 在我下载的最新的struts的开发包里,发现了一个JSON处置插件 struts2-json-plugin-2.3.8.jar 该插件供给了更完善的JSON处置解决方案,下篇文章会分析该插件的运用方法 http://www.haofapiao.com/linked/20130309.do

转载于:https://my.oschina.net/u/947963/blog/112818

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值