Struts2 基本的ResultType 【学习笔记】

时间:2024-05-07 17:36:01

在struts2-core.jar/struts-default.xml中,我们可以找到关于result-type的一些配置信息,从中可以看出struts2组件默认为我们提供了这 
些result-type 
       <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>

其中

dispatcher     返回jsp

struts.xml

<action name="*_*" class="cn.happy.action.{1}" method="{2}">
<result name="{2}">
/{1}/{2}.jsp
</result>
</action>

默认的模式 无需特殊指定result节点中的type属性

chain    实现从一个Action方法A跳转到另一个方法B

struts.xml

<!-- chain 转发到另一个Action-->
<action name="userAction" class="sword.action.UserAction" method="list">
<result name="list" type="chain">
<param name="actionName">userActionDel</param>
<param name="namespace">/</param>
</result>
</action> <action name="userActionDel" class="sword.action.UserAction" method="del">
<result name="del">
/del.jsp
</result>
</action>

Action

public class LogAction extends ActionSupport {
private String uname;
private String pwd; public String log() throws Exception { return "log";
}

jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
这是del
</body>
</html>

最后地址栏的地址没有变化 请求到了list方法后 转发到了del方法 最后回到了jsp页面