名字 | 说明 |
---|---|
dispater | 默认结果类型,用来呈现JSP页面,请求转发,底层调用RequestDispatcher的forward()或include()方法,(其中请求不变)dispatcher是 type属性的默认值,通常用于转向一个JSP。该类中有两个属性:location和parse。 |
redirect | 当使用该结果类型的时候,框架后台会使用response对象的sendRedirect方法进行重定向 (请求改变)(重定向到一个Url,也可以是Action或一个页面) |
redirectAction | (重定向到一个Action)redirect一样,底层都是调用Response对象的sendRedirect方法进行重定向的,这两者的区别是redirect结果类型一般是针对视图的,而redirectAction则是重定向到某个action,所以如果在action处理之后还要交给另一个action继续处理,那么使用redirecAction结果类型。 |
chain | 用来处理Action链,不会丢失原action的信息,redirect,则会将原action的信息丢失。将action的带着原来的状态请求转发到新的action,两个action共享一个ActionContext |
freemarker | 处理 FreeMarker 模板 |
velocity | 处理 Velocity 模板 |
xslt | 处理 XML/XLST 模板 |
httpheader | 控制特殊 http 行为的结果类型 |
stream | 向浏览器发送 InputSream 对象,通常用来处理文件下载,还可用于返回 AJAX 数据。 |
plainText | 显示源文件内容,如文件源码 |
dispater
type="dispatcher" 为默认,用于jsp页面跳转
<result name="success">/index.jsp</result>
完整的写法为:
<result name="success" type="dispatcher">
<param name="location">/index.jsp</param>
</result>
location参数用于指定action执行完毕后要转向的目标资源,parse属性是一个布尔类型的值,如果为true,则解析location参数中的OGNL表达式;如果为false,则不解析。parse属性的默认值就是true.
location参数是默认的参数,在所有的Result实现类中,都定义了一个字符串类型的DEFAULT_PARAM静态常量,专门用于指定默认的参数名。 DEFAULT_PARAM常量的定义:public static final String DEFAULT_PARAM=“location”;
在设置location参数时,可以在参数值中使用OGNL表达式。
<action name=“viewNews” class=“com.ibm.ViewNewsAction”
<result name=“success” type=“dispatcher”>
<param name=“location” >/viewNews.jsp?id=${id}</param>
<param name=“parse” >true</param>
</result>
</action>
上述可以简化为:
<action name=“viewNews” class=“com.ibm.ViewNewsAction”>
<result name=“success” >viewNews.jsp?id=${id}</result>
</action>
redirect
type="redirect" 重定向到jsp、action、外部网址
<result name="success" type="redirect">/index.jsp</result>
<result name="success" type="redirect">/login.do</result>
<result name="success" type="redirect">/login.do?userId=${userId }</result>
<result name="success" type="redirect">http://www.baidu.com</result>
该类同样有二个属性(property):location和parse,在使用redirect结果类型的场景中,用户要完成一次与服务器之间的交互,浏览器需要完成两次请求,因此第一次请求中的数据在第二次请求中是不可用的,这意味在目标资源中是不能访问action实例、action错误以及错误等。
如果有某些数据需要在目标资源中访问,
i、一种方式是将数据保存到Session中,
ii、另一种方式是通过请求参数来传递数据。
redirectAction
<result name="success" type="redirect-action">
<param name="actionName">login.do</param> 重定向action名
<param name="userId">userId</param>带的参数
</result>
redirectAction结果类型(重定向到一个Action)
他经常用于防止表单重复提交,比方说在增加完用户之后要显示列表
redirectAction结果类型的实现类是org.apache.struts2.dispatcher.ServletActionRedirectResult,该类是ServletRedirectResult的子类,因此我们也就可以判断出redirectAction结果类型和redirect结果类型的后台工作原理是一样的,即都是利用HttpServletResponse的sendRedirect方法将请求重定向到指定的URL。
<result type="redirectAction">
<param name="actionName">dashboard</param>
<param name="namespace">/secure</param>
</result>
重定向到不同命名空间下的 action
<result>aaa.jsp</result>
<result name="error" type="redirectAction">error</result>
重定向到同一命名空间下的action
重定向到Action并且传参
<result name="showReportResult" type="redirectAction">
<param name="actionName">generateReport</param>
<param name="namespace">/genReport</param>
<param name="width">100</param>
<param name="height">100</param>
</result>
做一个测试redirect和redirectAction
package action;
import com.opensymphony.xwork2.ActionSupport;
/**
* @ Author : Rain
* @ Classname : OneAction
* @ Date : 2019/3/19 19:48
* @ Description: one
*/
public class OneAction extends ActionSupport {
@Override
public String execute()
{
System.out.println("我是one,我处理完了,交给two");
return "success";
}
}
package action;
import com.opensymphony.xwork2.ActionSupport;
/**
* @ Author : Rain
* @ Classname : TwoAction
* @ Date : 2019/3/19 19:48
* @ Description: two
*/
public class TwoAction extends ActionSupport {
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
private int number;
@Override
public String execute() {
System.out.println("我是two,我来接着处理number,number="+number);
return "success";
}
}
struts.xml
<package name="user" extends="struts-default" namespace="/">
<action name="one" class="action.OneAction">
<result name="success" type="redirectAction">
<param name="actionName">two</param> 重定向action名
<param name="number">100</param>带的参数
</result>
</action>
<action name="two" class="action.TwoAction">
<result name="success" type="redirect">/index.jsp?number=${number}</result>
<!--<result name="success" type="send">/index.jsp?number=${number}</result>-->
</action>
</package>
index.jsp接受页面
<body>
<center>
property获取action值栈中的值number number=<s:property value="number" default="0"/><br>
property获取request中的值number number=<s:property value="#parameters.number" default="0"/><br>
重定向,number的值为${param.number}<br>
request,<%=request.getParameter("number")%>
</center>
</body>
猜结果?? 第二个action用redirect
https://blog.csdn.net/comeandgo201205/article/details/8044875
Struts2中的property标签<s:property value="…"/>
是用于取得OgnlContext上下文中的属性值,也称为数据标签
特点是取request/session/application/attr/parameters这五个范围时,需要明确指定范围如:
<s:property value="#request.属性名" />,记住,范围前面带#号,如果取ValueStack中的值时,是不需要带#的,可以直接取,因为Action中存储在ValueStack中的,所以取Action中的属性是不需要带#号的,可以直接取.
注意,在action中设置了该属性的setter/getter方法时,该属性才会取到,否则它不会被拦截器处理,value参数的类型是object,可以理解为这个默认是会解析成ognl表达式的。如果需要输入一个字符串<s:property value="‘ddd’">注意,在value里面的ddd字符串外面加了单引号,这样不会将ddd解析成ognl表达式了。
<action name="two" class="action.TwoAction">
<result name="success" type="redirect">/index.jsp?number=${number}</result>
</action>
我是one,我处理完了,交给two
我是two,我来接着处理number,number=100
我是one,我处理完了,交给two
我是two,我来接着处理number,number=100
我是one,我处理完了,交给two
我是two,我来接着处理number,number=100
再来 猜 第二个action用dispatcher
<action name="two" class="action.TwoAction">
<result name="success" type="dispatcher">/index.jsp?number=${number}</result>
</action>
两个redirect
<action name="one" class="action.OneAction">
<result name="success" type="redirect">
<param name="location">/two?number=100</param>
</result>
</action>
<action name="two" class="action.TwoAction">
<result name="success" type="redirect">/index.jsp?number=${number}</result>
</action>
redirect和dispatcher
<action name="one" class="action.OneAction">
<result name="success" type="redirect">
<param name="location">/two?number=100</param>
</result>
</action>
<action name="two" class="action.TwoAction">
<result name="success" type="dispatcher">/index.jsp?number=${number}</result>
</action>
chain
参考:https://www.cnblogs.com/ningvsban/p/3735195.html
chain是一种特殊的视图结果,用来将Action执行完之后链接到另一个Action中继续执行,新的Action使用上一个Action的上下文(ActionContext),数据也会被传递。
这在实际开发中,也是经常用到的一种ResultType。比如我们在Servlet开发中,一个请求,被一个Servlet处理过后,不是直接产生相应,而是把这个请求传递到下一个Servlet继续处理,直到需要的多个Servlet处理完成后,才生成响应返回。同样的,在Struts2开发中,也会产生这样的需要,一个请求被一个Action处理过后,不是立即产生响应,而是传递到下一个Action中继续处理。那么这个时候,就需要使用chain这个ResultType了。
先看看第一个Action
public class HelloWorldAction extends ActionSupport {
private String account;
private String password;
private String submitFlag;
public String execute() throws Exception {
this.businessExecute();
return "toSecond";
}
public void businessExecute(){
System.out.println("用户输入的参数为==="+"account="+account+",password="+password+",submitFlag="+submitFlag);
}
//属性对应的getter/setter方法,省略了
}
第二个Action
public class SecondAction extends ActionSupport {
public String execute() throws Exception {
System.out.println("现在SecondAction进行处理");
return "toWelcome";
}
}
然后到struts.xml中,配置这两个Action,要注意第一个Action的配置,在配置“toSecond”这个result的时候,用的就是chain这个ResultType
<package name="helloworld" extends="struts-default">
<action name="helloworldAction" class="cn.javass.action.action.HelloWorldAction">
<result name="toSecond" type="chain">
<param name="actionName">secondAction</param>
</result>
<!--下面这样配置也可以
<result name="toSecond" type="chain">secondAction</result>
-->
</action>
<action name="secondAction" class="cn.javass.action.action.SecondAction">
<result name="toWelcome">/s2impl/welcome.jsp</result>
</action>
</package>
chain不能在result配置的时候传递参数,也就是说,不能类似于如下的配置
<param name="actionName">secondAction?account=5</param>
因为这里要求配置的是要链接的Action的name,不能传递参数
传参数就需要在Action里面使用ActionContext或者ServletActionContext了。
使用chian的方式,后面的Action会和前面的Action共用同一个ActionContext
名称为“chain”的ResultType在配置的时候,除了前面示例中的actionName外,还有一个参数,名称为“namespace”,表示被链接的Action所在包的命名空间,如果不设置,默认的即是当前的命名空间。配置示例如下:
<result name="toSecond" type="chain">
<param name="actionName">secondAction</param>
<param name="namespace">其他Package的namespace</param>
</result>
(从一个Action转发到另一个Action)
链接到同一命名空间下的Action
<result type="chain">login</result>
其他namespace
<result type="chain">
<param name="actionName">dashboard</param>
<param name="namespace">/secure</param>
</result>
---------------------------------------------------------------------------------------
<actionname="dashboard" class="...">
<result>dashboard.jsp</result>
</action>
参考:http://www.cnblogs.com/LuckyBao/p/6010832.html
Chain:基本用途是构造成一条动作链。前一个动作将控制权转交给后一个动作,而前一个动作的状态在后一个动作里仍然保持着。动作链由Chaining拦截器负责处理,因为这个拦截器是defaultStack拦截器栈的一份子,多以你随时都可以使用动作链。
有人说:chain是共享valuestack;也有人说chain是共享表单参数.就我个人而言,以上两种说法都不见完全正确.
推荐一个博客:浅谈struts2之chain https://blog.csdn.net/randomnet/article/details/8656759
这个是我测试的截图。
其他简单介绍
HttpHeader (用来控制特殊的Http行为)
httpheader结果类型很少使用到,它实际上是返回一个HTTP响应的头信息
<result name="success"type="httpheader">
<paramname="status">204</param>
<paramname="headers.a">a custom header value</param> <paramname="headers.b">another custom header value</param>
</result>
<result name="proxyRequired"type="httpheader">
<paramname="error">305</param>
<paramname="errorMessage">this action must be accessed through aprozy</param>
</result>
Stream (向浏览器发送InputSream对象,通常用来处理文件下载)
<result name="success"type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">imageStream</param>
<param name="contentDisposition">attachment;filename="document.pdf"</param>
<param name="bufferSize">1024</param>
</result>
PlainText (显示原始文件内容,例如文件源代码)
<action name="displayJspRawContent">
<result type="plaintext">/myJspFile.jsp</result>
</action>
<action name="displayJspRawContent">
<result type="plaintext">
<param name="location">/myJspFile.jsp</param>
<param name="charSet">UTF-8</param>
</result>
</action>
若仅设置type="plainText"的话,页面中显示中文时会乱码,这时就可以借助它的charSet属性以解决中文显示时的乱码问题,如果不设置charSet属性,反而去配置struts.i18n.encoding全局属性,是不能解决问题的
设置charSet属性的目的就是让JSP页面的编码与明文显示时的编码一致
不要在浏览器的地址栏中传递中文。在传递中文前先进行编码
<result type="redirect" name="redirect">
/redirect.jsp?username=${username}//如果要传递两个参数,中间用&代替&</result>
redirect.jsp中
<%String s=request.getParameter("username");
s=new String(s.getBytes("iso8859-1"),"utf-8");
s=URLDecoder.decode(s,"utf-8");
out.println(s);
%>
重定向中传递中文先进行编码,在jsp页面中先接受参数,然后对其进行字节分解,然后进行解码。