struts2的结果类型

 

1、从struts-default.xml入手,得到结果类型列表以及对应的处理类:

        <result-types>
            <!-- 转发到action -->
            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
            <!-- 转发到jsp -->
            <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"/>
           <!-- 重定向到jsp -->
            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
           <!-- 重定向到action -->
            <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-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
        </result-types>

然后我们知道一些信息:

  1、常用的跳转类型:

    转发

      chain:转发到action

      dispatcher:转发到jsp

    重定向:

      redirect:重定向到jsp

      redirectAction:重定向到action

    流:

      stream:在文件下载中使用

  2、默认的跳转方式是转发到jsp

 

2、几种跳转方式的使用例子:

   

 <package name="resulttype" namespace="/resulttype" extends="struts-default">
    
        <action name="resultTypeAction" class="cn.itcast.resulttype.ResultTypeAction">
            <!-- 方法一
                 默认为转发
                    type:指定结果类型,默认为转发“dispatcher”
             -->
            
             <!--  <result name="success" type="dispatcher">/resulttype/success.jsp</result>-->
            
            <!-- 方法二:标准写法
            
             -->
            <!--<result name="success" type="dispatcher">
                
                  
                    param:参数
                        name:参数的名称为“location”
                            实际上是struts2框架底层StrutsResultSupport类的setLocation()提供的
                        所谓的参数,应该是set方法或者get方法后面跟的名称才是参数名
                
                <param name="location">/resulttype/success.jsp</param>
            </result>-->
            
            <!-- 重定向到jsp -->
            <!--<result name="success" type="redirect">
                <param name="location">/resulttype/success.jsp</param>
            </result>-->
            
            <!-- 
                重定向到action 
                    param:
                        actionName:指定“目的地”动作的名称。指定action标签name属性的值    
                        namespace:用来指定目的地的命名空间。指定的是struts的配置文件action对应的package的namespace的值
            -->
            <!--<result name="success" type="redirectAction">
                <param name="actionName">userAction</param>
                <param name="namespace">/prima</param>
            </result>
            
            -->
            <!-- 
                跳转到action
                param:
                    和重定向到action的一样    
             -->
            <result name="success" type="chain">
                <param name="actionName">userAction</param>
                <param name="namespace">/prima</param>
            </result>
            
        </action>
    </package>

注意:

  我们以后写结果类型的时候尽量写标准写法。

  重定向与转发的最大的区别在域request域信息的是否重置,所以我们使用request中添加属性,判断属性值是否可以显示验证转发与重定向。

  如何在Action中获取request等web中常用的对象呢?

    使用struts2提供的工具类ServletActionContext的指定方法,原来是struts2将request等web对象封装成了一个map集合,需要我们使用该工具类调用!

public String execute() throws Exception {
        System.out.println("ResultTypeAction ****** execute() ");
        
        //struts2框架将request封装成一个map集合,通过struts2框架提供的工具类ServletActionContext的getRequest()方法来获取request
        HttpServletRequest request=ServletActionContext.getRequest();
        
        
        request.setAttribute("username", "username_request");
        
        return "success";
    }

 

测试使用的代码:

  

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'form.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <form action="${pageContext.request.contextPath }/resulttype/resultTypeAction.do" name="form1" method="post">
        <input type="submit" value="提交">
    </form>
  </body>
</html>
resulttype/form.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    resulttype:    ${requestScope.username }
  </body>
</html>
resulttype/success.jsp

前面的prima/success.jsp部分修改:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    this is success.jsp! <br>
    ${requestScope.username }
    
    
  </body>
</html>
resulttype/success.jsp

struts_resulttype.xml主要代码列出来就不写了,导入xml的也不写了

package cn.itcast.resulttype;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class ResultTypeAction extends ActionSupport {

    @Override
    public String execute() throws Exception {
        System.out.println("ResultTypeAction ****** execute() ");
        
        //struts2框架将request封装成一个map集合,通过struts2框架提供的工具类ServletActionContext的getRequest()方法来获取request
        HttpServletRequest request=ServletActionContext.getRequest();
        
        
        request.setAttribute("username", "username_request");
        
        return "success";
    }
}
resultTypeAction.java

 

  

posted @ 2016-09-19 23:15  guodaxia  阅读(789)  评论(0编辑  收藏  举报