• Struts2(六):ResultType


    本章节将继续学习struts2的返回类型的使用方法。

    学习文档下载struts2 full包解压后会在doc下包含离线html文档。

    点击运行后页面:

    点击Guides向导终将会有向导列表

    再点开后,就可以看到struts2支持的所有resultType:

    默认的resultType是dispatcher.

    常用的dispatcher,redirect,redirectAction,chain.

     1 package name="default" namespace="/" extends="struts-default">
     2         <action name="testServletObject"
     3             class="com.dx.actions.TestActionContextFetchServletObjectAction">
     4             <result>/WEB-INF/test-context.jsp</result>
     5         </action>
     6         <!-- default class="com.opensymphony.xwork2.ActionSupport" method="execute" -->
     7         <action name="gotoLoginPage">
     8         <!-- name default value:"success" -->
     9             <result>/login.jsp</result>
    10         </action>
    11                 
    12         <action name="logout" class="com.dx.actions.TestWithXXXAware"
    13             method="logout">
    14             <result name="logout-success">/login.jsp</result>
    15         </action>
    16         
    17         <action name="login" class="com.dx.actions.TestWithXXXAware" method="login">
    18             <result name="login-success">/WEB-INF/login-success.jsp</result>
    19         </action>
    20         
    21         <action name="testResult" class="com.dx.actions.TestResultAction" method="test">
    22             <!-- redirect到一个Action -->
    23             <result name="redirectAction-success" type="redirectAction">
    24                 <param name="actionName">testRdirect</param>
    25                 <param name="namespace">/testNamespace</param>
    26             </result>
    27             <!-- redirect -->
    28             <result name="redirect-success" type="redirect">/login.jsp</result>
    29             <!-- dispatcher -->
    30             <result name="dispatcher-success" type="dispatcher">/login.jsp</result>
    31         </action>
    32         
    33         <action name="createAccount" class="com.dx.actions.AccountAction" method="execute">
    34             <!-- chain -->
    35             <result type="chain">login</result>
    36         </action>
    37     </package>
    38     
    39     <package name="testRedirectPackage" namespace="/testNamespace" extends="struts-default">
    40         <action name="testRdirect" class="com.dx.actions.TestRedirectAction" method="test">
    41             <result name="test-success">/WEB-INF/test-success.jsp</result>
    42         </action>
    43     </package>

    TestRedirectAction.java

     1 package com.dx.actions;
     2 
     3 import org.apache.struts2.ServletActionContext;
     4 
     5 public class TestRedirectAction {
     6     public String test() {
     7         String resultType = ServletActionContext.getRequest().getParameter("resultType");
     8         System.out.println(resultType);
     9         ServletActionContext.getRequest().setAttribute("resultType", resultType);
    10         System.out.println("TestRedirectAction.test");
    11         return "test-success";
    12     }
    13 }

    TestResultAction.java

     1 package com.dx.actions;
     2 
     3 import org.apache.struts2.ServletActionContext;
     4 
     5 import com.opensymphony.xwork2.ActionContext;
     6 
     7 import sun.nio.cs.ext.TIS_620;
     8 
     9 public class TestResultAction {
    10     private String resultType;
    11 
    12     public void setResultType(String resultType) {
    13         this.resultType = resultType;
    14     }
    15 
    16     public String test() {
    17         System.out.println(resultType);
    18 
    19         if (resultType.equalsIgnoreCase("dispatcher")) {
    20             System.out.println("result dispatcher-success");
    21             return "dispatcher-success";
    22         } else if (resultType.equalsIgnoreCase("redirect")) {
    23             System.out.println("result redirect-success");
    24             return "redirect-success";
    25         } else if (resultType.equalsIgnoreCase("redirectAction")) {
    26             System.out.println("result redirectAction-success");
    27             return "redirectAction-success";
    28         } else if (resultType.equalsIgnoreCase("chain")) {
    29             System.out.println("result chain-success");
    30             return "chain-success";
    31         } else {
    32             System.out.println("no match");
    33         }
    34         
    35         ServletActionContext.getRequest().setAttribute("resultType", this.resultType);
    36         
    37         return "";
    38     }
    39 }

    AccountAction.java

    1 package com.dx.actions;
    2 
    3 public class AccountAction {
    4     public String execute(){
    5         System.out.println("acction action chain");
    6         return "success";
    7     }
    8 }

    index.jsp

    <a href="testResult?resultType=redirectAction">redirectAction</a>
        <br />
        <a href="testResult?resultType=redirect">redirect</a>
        <br />
        <a href="testResult?resultType=dispatcher">dispatcher</a>    
        <br />
        <a href="createAccount">test chain</a>
        

    result配置还有很多需要注意的地方,可以参考:

    file:///D:/Java_Study/struts-2.3.28-all/struts-2.3.28/docs/docs/result-configuration.html

    这里就简单说明,介绍,一遍自己可以学习使用。

  • 相关阅读:
    linux下mysql数据导入到redis
    redis常用api
    springboot2.0+mybatis多数据源集成
    斐波那契数列(递归、非递归算法)
    从尾到头打印链表
    docker学习笔记:简单构建Dockerfile【Docker for Windows】
    python3+OpenGL环境配置
    【python库安装问题解决】UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 121: invalid start byte
    DA-GAN技术【简介】【机器通过文字描述创造图像】
    洛谷 P2045 方格取数加强版【费用流】
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/5602256.html
Copyright © 2020-2023  润新知