struts2学习笔记之七:Result类型

一:关于Struts2的type类型,也就是Result类型,他们都实现了共同的接口Result,都实现了execute方法
他们体现了策略模式,具体Result类型参见: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>  
我们完全可以自己根据需求扩展Result类型
 
二:在Struts2中默认为转发,也就是<result>标签中的type="dispatcher",type的属性可以修改为重定向
Struts的重定向有两种:
type="redirect",可以重定向到任何一个web资源,如:jsp或Action,如果要重定向到Action,需要写上后缀:xxxx.action
type="redirectAction",可以重定向到Action,不需要写后缀,此种方式更通用些,不会因为后缀的改变影响配置
 
三:<result>标签的name属性,如果不配置,那么缺省值为success
 
四:全局Result和局部Result
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
    
    <!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置  -->
    <constant name="struts.configuration.xml.reload" value="true"></constant>
    <!-- 提供更加友好的提示信息 -->
    <constant name="struts.devMode" value="true"></constant>
    <!-- 对字符集的设置 -->
    <constant name="struts.i18n.encoding" value="GB18030"/>
    
    <package name="struts2" extends="struts-default">
<!--  全局Result,如果Action没有配置Result,使用全局Result,如果有局部Result,使用局部Result  -->
        <global-results>
            <result>/success.jsp</result>
            <result name="error">/error.jsp</result>
        </global-results>
        <action name="login" class="com.djoker.struts2.LoginAction">
            <result>/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
    
    <include file="struts-user.xml"></include>
</struts>

 

posted @ 2016-12-24 22:25  喝杯茶  阅读(253)  评论(0编辑  收藏  举报