📄 jsonresultdetail.java
字号:
package cn.com.sdcncsi.lunch.balance.result;import java.io.PrintWriter;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts2.ServletActionContext;import cn.com.sdcncsi.lunch.balance.domain.AccountBook;import cn.com.sdcncsi.lunch.balance.domain.AccountDetail;import cn.com.sdcncsi.lunch.balance.domain.AccountType;import cn.com.sdcncsi.lunch.domain.User;import cn.com.sdcncsi.lunch.result.JSONResult;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.util.ValueStack;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;/* * The JSONResult is a custom result to serlialize an object to the JSON format. This * result type can be used to provide Ajax clients with JSON repsonses. * * This result is basically just to show two things. First, the fundamentals of making * a custom result. And second, how Ajax applications can be easily integrated with the * framework. * * This result takes a single parameter, classAlias, which will be the JSON object name * under which the domain model object will be serialized. As far as what gets serialized, * the JSON result will look for a property on the ValueStack called jsonModel. This * value could arrive on the ValueStack via a variety of methods. */public class JSONResultDetail extends JSONResult { private static Log logger = LogFactory.getLog(JSONResultDetail.class.getName()); String classAlias; public void execute(ActionInvocation invocation) throws Exception { ServletActionContext.getResponse().setContentType("text/plain; charset=UTF-8"); PrintWriter responseStream = ServletActionContext.getResponse().getWriter(); /* Retreive Objects to Serialize to JSON from ValueStack*/ ValueStack valueStack = invocation.getStack(); Object jsonModel = valueStack.findValue("jsonModel"); XStream xstream = new XStream(new JettisonMappedXmlDriver()); /*If there's no parameter passed in, just write the objects under a default name. */ if ( classAlias == null ){ classAlias = "object"; } logger.debug("classAlias:"+classAlias); xstream.alias(classAlias, jsonModel.getClass() ); logger.debug("jsonModel:"+jsonModel.getClass()); xstream.alias("detail", new AccountDetail().getClass() ); xstream.alias("book", new AccountBook().getClass() ); xstream.alias("user", new User().getClass() ); xstream.alias("type", new AccountType().getClass() ); /* Write to the response stream */ String jsonxml = xstream.toXML( jsonModel ); responseStream.println(jsonxml); logger.debug("jsonxml="+jsonxml); //logger.debug("HiJSONUtil="+); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -