在上篇博客中的图片可以看到,Struts有一个基本的抽象类,我们也可以继承他来自定义Result
继承他之后,只需要实现一个方法就可以了:
@Override
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
}
这里简单说一下,怎样实现一个返回JSON串的Result
方法1:
自定义的Result
package org.ygy.demo.result;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import com.opensymphony.xwork2.ActionInvocation;
/**
*
* @author yuguiyang
* @description 返回JSON
* @time 2013-9-4
* @version V1.0
*/
public class JsonResult extends StrutsResultSupport {
private static final long serialVersionUID = 2232581955223674065L;
private Object result;
private JsonConfig jsonConfig;
public JsonResult() {}
public JsonResult(JsonConfig jsonConfig) {
super();
this.jsonConfig = jsonConfig;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
@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, jsonConfig).toString());
} else {
printWriter.write(JSONSerializer.toJSON(result).toString());
}
} catch (Exception e) {
throw new Exception("json parse error!");
} finally {
response.getWriter().close();
}
}
}
这里使用json-lib来将对象转换成JSON串
Action中是这样的:
注意:这里的返回值,就是刚才自定义的JsonResult
public JsonResult ha() {
System.out.println("--from ha().");
List<String> msgs = new ArrayList<String>();
msgs.add("one");
msgs.add("two");
msgs.add("three");
JsonResult result = new JsonResult();
result.setResult(msgs);
return result;
}
配置文件是这样的:
<action name="ha" class="org.ygy.demo.result.HelloAction" method="ha">
<result name="success" type="json" >/hello.jsp</result>
</action>
PS:这里有点儿乱,明天在研究一下吧

方法二:
package org.ygy.demo.result;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
private static final long serialVersionUID = 6422231025855671997L;
private Person person;
public String say() {
System.out.println("--from say().");
return SUCCESS;
}
public String go() {
System.out.println("--from go().");
return SUCCESS;
}
public JsonResult ha() {
System.out.println("--from ha().");
List<String> msgs = new ArrayList<String>();
msgs.add("one");
msgs.add("two");
msgs.add("three");
JsonResult result = new JsonResult();
result.setResult(msgs);
return result;
}
public String haha() {
person = new Person(100 , "拉速度");
return SUCCESS;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
自定义Result:
package org.ygy.demo.result;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONSerializer;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.util.ValueStack;
public class JsonResult2 extends StrutsResultSupport {
private static final long serialVersionUID = -6528161012160891182L;
@Override
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
ActionContext context = invocation.getInvocationContext();
//通过invocation获取Action的变量
Map<String , Object> temp = context.getContextMap();
Object action = invocation.getAction();
ValueStack stack = invocation.getStack();
Object rootObject = stack.findValue("person");
if(rootObject instanceof Person) {
System.out.println("I get it.");
}
//转换为JSON字符串
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter out = response.getWriter();
out.println(JSONSerializer.toJSON(rootObject).toString());
out.println(JSONSerializer.toJSON((Person)rootObject).toString());
out.flush();
out.close();
}
}
配置文件:
<action name="haha" class="org.ygy.demo.result.HelloAction" method="haha">
<result name="success" type="json2">/do you know me</result>
</action>
这一次,在自定义Result时,在在值栈中找到变量Person,Struts有自己实现的JSONResult,大家可以看一下源码。
这里越写越乱,以后再梳理一下。