struts2几种result type探究 (一)

2014-11-24 02:38:25 · 作者: · 浏览: 2

可以在struts2-core-{version}.jar中找到struts-default.xml,里面列举了当前可以使用的所有result-type以及对应的class

此处是struts2.2.3的


view plaincopy to clipboardprint
























1.chain

该类型是请求转发给其他的action,如果为jsp则会报错

需要注意的就是与redirect的区别,请求转发是还在当前请求,

而redirect会响应一次浏览器然后浏览器再根据响应请求重定向的资源,注意看url的变化就明白了!

下面是apache给的示例,这个比较简单,没什么说的,

view plaincopy to clipboardprint login dashboard /secure dashboard.jsp


login




dashboard
/secure




dashboard.jsp

2.dispatcher请求转发视图资源,比如jsp,html,如果请求action会找不到资源,请求转发的问题看1中的说明apache给的示例。。也没啥说的view plaincopy to clipboardprint foo.jsp
foo.jsp

3.httpheader这个挺有意思的,可以随便更改响应状态比如下面的这个示例view plaincopy to clipboardprint 400

400

当action返回SUCCESS的时候,会将响应状态修改为400,客户端错误的请求,还有其他的状态可以自行尝试,比如为100时,浏览器会在请求一段时间之后继续当前页面,500则为服务器内部错误等等具体为什么会产生这样的结果,当然得把源码翻出来了下面是HttpHeaderResult的一些成员变量,private的可以作为param的name属性view plaincopy to clipboardprint private static final long serialVersionUID = 195648957144219214L; /** The default parameter */ public static final String DEFAULT_PARAM = "status"; private boolean parse = true; private Map headers; private int status = -1; private int error = -1; private String errorMessage; private static final long serialVersionUID = 195648957144219214L;

/** The default parameter */
public static final String DEFAULT_PARAM = "status";


private boolean parse = true;
private Map headers;
private int status = -1;
private int error = -1;
private String errorMessage;下面是具体执行操作过程,可以看到response.setStatus等操作view plaincopy to clipboardprint public void execute(ActionInvocation invocation) throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); ValueStack stack = ActionContext.getContext().getValueStack(); if (status != -1) { response.setStatus(status); } else if (error != -1) { if (errorMessage != null) { String finalMessage = parse TextParseUtil.translateVariables( errorMessage, stack) : errorMessage; response.sendError(error, finalMessage); } else response.sendError(error); } if (headers != null) { for (Iterator iterator = headers.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); String value = (String) entry.getValue(); String finalValue = parse TextParseUtil.translateVariables(value, stack) : value; response.addHeader((String) entry.getKey(), finalValue); } } } public void execute(ActionInvocation invocation) throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
ValueStack stack = ActionContext.getContext().getVal