struts2之Action配置的各项默认值、Action跳转、result配置的各种试图类型及多个struts配置文件

1、Action配置的各项默认值
  (1)、如果没有为action指定class,默认是ActionSupport。
  (2)、如果没有为action指定method,默认执行action中的execute()方法。
  (3)、如果没有为action指定result,默认值为success。

2、result配置的各种试图转发类型
<result type="">...</result>
  其中type的常用类型有: dispatcher(默认值) ->转发  redirect ->重定向  

如:<result name="success" type="redirectAction">
          <param name="actionName">helloworld</param>
          <param name="nameSpace">/test</param>
      </result>
  plainText ->显示原始文件内容
如:<result type="">
          <param name="location">/xxx.jsp</param>
          <!--指定读取文件的编码-->
          <param name="charSet">UTF-8</param>
      </result>
注意:在result中还可以使用${属性名}表达式访问action中的属性,表达式里的属性名对应action中的属性名
  如:<result name="success" type="redirect">/index.jsp?username=${username}</result>

   <action name="redirect2" class="cn.myself.struts.action.RedirectAction2">
     <result name="success">/forward/result.jsp</result>
  </action>
  <action name="redirect1" class="cn.myself.struts.action.RedirectAction1">
     <result name="success" type="redirectAction">
        <param name="actionName">redirect2</param>
        <param name="namespace">/test</param>
        <param name="username">${username}</param>
        <param name="password">${password}</param>
        <param name="usernameAndPassword">${usernameAndPassword}</param>
     </result>
  </action>
  <action name="forward" class="cn.myself.struts.action.RedirectAction1">
     <result name="success" type="chain">
        <param name="actionName">redirect2</param>
        <param name="namespace">/test</param>
        <param name="usernameAndPassword">${usernameAndPassword}</param>
     </result>
  </action>
<!-- result类型使用值列表 -->
<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" />
        <!-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 -->
        <result-type name="redirect-action" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
        <result-type name="plaintext" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
<!--
        chain    用来处理Action链    
        dispatcher    用来转向页面,通常处理JSP    
        freemaker       处理FreeMarker模板    
        httpheader    控制特殊HTTP行为的结果类型    
        redirect      重定向到一个URL    
        redirectAction    重定向到一个Action    
        stream    向浏览器发送InputSream对象,通常用来处理文件下载,还可用于返回AJAX数据    
        velocity    处理Velocity模板    
        xslt    处理XML/XLST模板    
        plainText    显示原始文件内容,例如文件源代码    
        redirect-action    重定向到一个Action    
        plaintext    显示原始文件内容,例如文件源代码      
-->

3、为应用指定多个struts配置文件

在大部分应用里,随着应用规模的增加,系统中Action数量也大量增加,导致struts.xml配置文件变得非常臃肿。为了避免struts.xml文件过于庞大、臃肿,提高struts.xml文件的可读性,我们可以将一个struts.xml配置文件分解成多个配置文件,然后在struts.xml文件中包含其他配置文件。下面的struts.xml通过<include>元素指定多个配置文件:

<?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>
    <include file="/struts/struts-user.xml"/>
    <include file="struts-order.xml"/>
</struts>
原文地址:https://www.cnblogs.com/lbangel/p/3083462.html