工作中看到有自定义的result标签,使用起来比较方便,特把代码贴出来:
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.util.ValueStack;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
public class AJAXResult
implements Result
{
private String ajax;
private HttpServletResponse rep;
private String chartSet = "GBK";
private static final long serialVersionUID = 3841999252996023829L;
public void execute(ActionInvocation invocation)
throws Exception
{
this.ajax = ((String)invocation.getStack().findValue("ajax"));
if (this.ajax == null) {
throw new NullPointerException("没有定义ajax字符串结果集合,或者没有提供get方法");
}
this.rep = ServletActionContext.getResponse();
this.rep.reset();
this.rep.setContentType("text/html; charset=" + this.chartSet);
ServletOutputStream os = this.rep.getOutputStream();
os.write(this.ajax.getBytes(this.chartSet));
os.flush();
os.close();
}
public String getChartSet()
{
return this.chartSet;
}
public void setChartSet(String chartSet)
{
this.chartSet = chartSet;
}
}