Struts2_Result

【1】Result Type
1.Dispatcher(调度):服务器端跳转到视图,浏览器发起请求给服务器,服务器forward到另一个地址,浏览器都不知道发生了跳转。
<result type="dispatcher">/r1.jsp</result>

2.redirect:客户端跳转跳转到视图,浏览器发起请求给服务器,服务器找到那个url,那个url说跳转到另一个url,并将它交给浏览器,浏览器重新发起请求再去继续访问那个网页。.jsp
<result type="redirect">/r2.jsp</result>

3.chain:服务器端跳转到action
<result type="chain">r1</result>//当前包里就直接写r1:
别的包里就写:
<result type="chain">           
  <param name="actionName">dashboard</param>          
  <param name="namespace">/secure</param>       
</result>

4.redirectAction:客户机端跳转到action
<result type="redirectAction">r2</result>

5.freemarker
6.httpheader
7.stream
8.velocity
9.xslt
10.plaintext(显示源码)
11.tiles

【2】GlobalResult:多个Action需要共享一个result,要是别的包里的result需要使用extends
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
     
     
     【<global-results>
      <result name="mainpage">/main.jsp</result>
     </global-results>】
     
     <action name="index">
      <result>/index.jsp</result>
     </action>
     
     <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
      <result>/user_success.jsp</result>
      <result name="error">/user_error.jsp</result>
     </action>    
    </package>
   
    <package name="admin" namespace="/admin" extends="user">
     <action name="admin" class="com.bjsxt.struts2.user.action.AdminAction">
      <result>/admin.jsp</result>
     </action>
    </package>
</struts>

package com.bjsxt.struts2.user.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
 private int type;

 public int getType() {
  return type;
 }

 public void setType(int type) {
  this.type = type;
 }

 @Override
 public String execute() throws Exception {
  if(type == 1) return "success";
  else if (type == 2) return "error";
  else return "mainpage";
 }
}

【3】DynamicResult
OGNL表达式
OGNL,全称为Object-Graph Navigation Language,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性,它旨在提供一个更高的更抽象的层次来对Java对象图进行导航。
在action中保存一个成员变量(属性),存储具体的结果location
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
     
     <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
      <result>${r}</result>//读值栈里的内容(对于任何一个action,它的任何属性都会放在值栈里面)
     </action>    
    </package>
</struts>

package com.bjsxt.struts2.user.action;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
 private int type;
 
 private String r;
 public String getR() {
  return r;
 }
 public void setR(String r) {
  this.r = r;
 }

 public int getType() {
  return type;
 }
 public void setType(int type) {
  this.type = type;
 }

 @Override
 public String execute() throws Exception {
  if(type == 1) r="/user_success.jsp";
  else if (type == 2) r="/user_error.jsp";
  return "success";
 }
}
【4】ResultWithParams
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
     
     <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
      <result type="redirect">/user_success.jsp?t=${type}</result>
     </action>    
    </package> 
</struts>
将一个action中的参数forward到另一个action时,是不用传递值的,因为对于服务器来说,这都是一次request,所以这些action都是共享一个值栈的。
需要继续传递参数的情况是:服务器告诉浏览器,它需要再进行一次request请求。这其实就是发起了2次request,所以会建立两次值栈。
给了另一个jsp文件,相当于转到了另一个action。但是直接转到另一个jsp文件,是不会向值栈里传递参数的,不能使用值栈取值。而是需要使用#parameters.t
值栈取值:
from valuestack: <s:property value="t"/><br/>
从actioncontext中取值:
from actioncontext: <s:property value="#parameters.t"/>

原文地址:https://www.cnblogs.com/boyangx/p/4069416.html