在jsp/servlet中,结果集一般是指请求转发和重定向这两种。

Struts2作为框架,提供了基于这两种的很多其它的结果集!


在struts-default.xml中定义了一些默认的结果集:
         <result-types>
            <result-type name= "chain" class="com.opensymphony.xwork2.ActionChainResult" />
            <result-type name= "dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
            <result-type name= "freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult" />
            <result-type name= "httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult" />
            <result-type name= "redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult" />
            <result-type name= "redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult" />
            <result-type name= "stream" class="org.apache.struts2.dispatcher.StreamResult" />
            <result-type name= "velocity" class="org.apache.struts2.dispatcher.VelocityResult" />
            <result-type name= "xslt" class="org.apache.struts2.views.xslt.XSLTResult" />
            <result-type name= "plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
        </result-types>

怎样配置结果集呢?
在struts的配置中,使用result标签来表示结果集:
< result type = "redirect" name = "redirect"> / resulttype/resulttype.jsp </result >


type:表示结果集的类型
name:与Action中方法的返回值相互相应!

常见的type有三种:dispatcher,redirect,redirectAction

以下具体介绍一下这三种结果集:
1.创建一个struts-resulttype.xml文件:
在struts.xml文件里。使用inclue将上述文件引入:
< include file = "struts-resulttype.xml" ></include >

struts-resulttype.xml:
<?xml version= "1.0" encoding ="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache
Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
     <package name="resulttype" namespace="/" extends="struts-default" >
          
           <!--
              全局结果集:当某个方法返回为name属性相应的值时。就调用全局结果集进行处理。

              能够用来进行通用错误页面的处理。
           -->
           <global-results>
               <result name="error" >errot/error.jsp</ result>
           </global-results>
          
           <!-- 測试请求转发 
-->
           <action name= "dispatcherAction" method="testDispatcher" class="cn.itheima03.struts2.resulttype.ResultTypeAction" >
               <!--
              <result type="" name=""></result> :
                   result标签代表一种结果集。在struts-default.xml中定义了一写结果集
                   type
                        dispatcher  转发  默认值
                        redirect    重定向
                        redirectAction  重定向到一个action
                  name
                      success 默认值
                  method="testDispatcher"
               -->
               <result type= "redirect" name="redirect" >/ resulttype/resulttype.jsp</result >
           </action>
          
          
           <!-- 測试重定向
               浏览器中的 url会发生变化:
               http://localhost/itheima03_struts2/resulttype/resulttype.jsp
           -->
           <action name= "redirectAction" method="testRedirect" class="cn.itheima03.struts2.resulttype.ResultTypeAction" >
               <result type= "redirect" name="redirect" >/ resulttype/resulttype.jsp</result >
           </action>
          
           <!--
              測试重定向到一个action
              浏览器的地址栏会变成:
               http://localhost/itheima03_struts2/dispatcherAction!testDispatcher.action
           -->
           <action name= "redirectActionAction" method="testRedirectAction" class="cn.itheima03.struts2.resulttype.ResultTypeAction" >
               <result type= "redirectAction" name="redirectAction" >dispatcherAction!testDispatcher.action </result>
           </action>
          
           <!-- 測试全局结果集的处理
              当自定义的name属性的值和全局结果集name属性值一直时,自定义的优先!
              1.当自己的name='error'时,使用自定义的结果集。
              2.当自己的name!='error'时。假设訪问的方法返回的是"error",那么调用全局结果集进行处理!

              
          -->
           <action name= "globalResultAction_*" method ="{1}" class="cn.itheima03.struts2.resulttype.ResultTypeAction" >
               <result name="error" >resulttype/resulttype.jsp</ result>
           </action>
          
     </package >
</struts>

2.Action的处理:
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class ResultTypeAction extends ActionSupport{
     
     /**
      * 測试请求转发结果集:type为dispatcher
      * <result type="dispatcher" name="dispatcher">/resulttype/resulttype.jsp </result>
      */
     public String
testDispatcher(){
          ServletActionContext. getRequest().setAttribute("aa", "aadda");
//        return "error";//測试全局结果集
           return "dispatcher" ;
     }
     
     /**
      * 測试重定向
      */
     public String
testRedirect(){
          ServletActionContext. getRequest().setAttribute("aa", "aaa");
           return "redirect" ;
     }
     
     /**
      * 測试重定向到一个action
      * <result type="redirectAction" name="redirectAction">dispatcherAction!testDispatcher.action</result>
      */
     public String
testRedirectAction(){
          ServletActionContext. getRequest().setAttribute("aa", "aaa");
           return "redirectAction" ;
     }
     
     /**
      * 測试全局结果集
      * 返回值要和全局结果集的name属性值要相应!
      *        <global-results>
                   <result name="error">errot/error.jsp </result>
             </global-results>
      *
      * @return
      */
     public String
globle(){
           return "error" ;
     }
}


【Struts2二】结果集(result-type)的更多相关文章

  1. struts2 跳转类型 result type=chain、dispatcher、redirect(redirect-action)

    dispatcher 为默认跳转类型,用于返回一个视图资源(如:jsp) Xml代码 : <result name="success">/main.jsp</re ...

  2. Struts2 语法--result type

    result type: dispatcher,redirect:只能跳转到jsp,html之类的页面,dispatcher属于服务器跳转, redirect属于客户端跳转 chain: 等同于for ...

  3. 基于struts2注解@action的@Result跳转问题——跳转到另一个action

    初学ssh 基于注解的方式简单灵活,但是做一个例子的时候,添加用户AddUser 完成后 想页面跳转到 ListUser 这个action, 然后action 成功后 会跳转到list.jsp 显示 ...

  4. struts2.xml 中result type属性说明

    chain           用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息.           com.opensymphony.xwork2.Acti ...

  5. struts2 Result Type四个常用转跳类型

    Result的四个常用转跳类型分别为 Dispatcher 用来转向页面,是Struts的默认形式 Redirect   重定向到一个URL Chain  用来处理Action链 RedirectAc ...

  6. Struts2 中result type属性说明

    Struts2 中result type属性说明 首先看一下在struts-default.xml中对于result-type的定义: <result-types><result-t ...

  7. struts2 action result type类型

    struts2 action result type类型 1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony. ...

  8. struts2 result type类型

    result标签中type的类型 类型 说明 chain 用于Action链式处理 dispatcher 用于整合JSP,是<result>元素默认的类型 freemarket 用来整合F ...

  9. struts2 中 result type="stream"

    Stream result type是Struts2中比较有用的一个feature.特别是在动态生成图片和文档下载的情况下 1:图片验证码: Action类,action主要要提供一个获取InputS ...

随机推荐

  1. 【转】C语言将字符串转换成对应的数字(十进制、十六进制)

    转自:http://wawlian.iteye.com/blog/1315133 1.一个十进制数字的字符串表示转换成对应的整数.举例:将“1234”转换成整数1234 /*将字符串s转换成相应的整数 ...

  2. 【codeforces 810A】Straight «A»

    [题目链接]:http://codeforces.com/contest/810/problem/A [题意] 有n门课的成绩,和一个整数k代表每门课的满分都是k分; 然后这n门课的成绩是按照平均分算 ...

  3. Java代码实现MySQL数据库的备份与还原

    通常在MySQL数据库的备份和恢复的时候,多是采用在cmd中执行mysql命令来实现. 例如: mysqldump -h127.0.0.1 -uroot -ppass test > d:/tes ...

  4. 聚类算法学习-kmeans,kmedoids,GMM

    GMM参考这篇文章:Link 简单地说,k-means 的结果是每个数据点被 assign 到其中某一个 cluster 了,而 GMM 则给出这些数据点被 assign 到每个 cluster 的概 ...

  5. Cocos2d-x使用Luajit将Lua脚本编译为bytecode,实现加密 更新

    项目要求对lua脚本进行加密,查了一下相关的资料 ,得知lua本身能够使用luac将脚本编译为字节码(bytecode)从而实现加密,试了一下,确实可行. 以下是使用原生的lua解释器编译字节码: 1 ...

  6. &lt;pre&gt;标签

     <pre>标签最主要的认识就是预格式化文本,被包围在 pre 元素中的文本一般会保留空格和换行符.而文本也会呈现为等宽字体.经经常使用于在网页中显示计算机源码. 1.格式化文本举例 &l ...

  7. BZOJ 4568 倍增维护线性基

    在树的路径上选取一些点 使得这些点权xor后的结果最大 思路: 时限60s 59696ms卡过去了哈哈哈 //By SiriusRen #include <cstdio> #include ...

  8. POJ 3693 后缀数组+RMQ

    思路: 论文题 后缀数组&RMQ 有一些题解写得很繁 //By SiriusRen #include <cmath> #include <cstdio> #includ ...

  9. @JsonIgnore忽略属性,返回的json中不包含字段

    @JsonIgnore的使用: 实体类中加@JsonIgnore注解 package com.baidu.entity; import com.fasterxml.jackson.annotation ...

  10. C++之指针与引用,函数和数组

    ]={,,}; //ptr是指针,该指针类型是int[3] ]=&arr; cout << **ptr << endl;//第一次解指针时得到数组地址,第二次解指针取数 ...