Struts2学习——1000Result Type类型入门

背景

不仅action是有相关的类型,result也有相关的类型。 type属性可以取的值有:chain、dispatcher、freemarker、httpheader、redirect、redirectAction、stream、velocity、xslt、plainText。但是常用的主要有四种:dispatcher、redirect、chain、redirectActoin。本小节主要介绍的就是这四种类型。四种中最最常用的又是前两种。

Result Type

本小节用一个小实例,解释四种类型的result各有什么不同

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="resultTypes" namespace="/r" extends="struts-default">
        <action name="r1">
            <result type="dispatcher">/r1.jsp</result>
        </action>

        <action name="r2">
            <result type="redirect">/r2.jsp</result>
        </action>

        <action name="r3">
            <result type="chain">r1</result>
        </action>

        <action name="r4">
            <result type="redirectAction">r2</result>
        </action>

    </package>
</struts>

在上述struts.xml中定义了四个action,每个action中有一个result,这四个result的type各不相同。分别是dispatchre、redirect、chain和redirectAction。前两个result对应的jsp文件代码和效果如下:

r1.jsp

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>r1</title>
</head>
<body>
r1
</body>
</html>

效果图:
这里写图片描述

r2.jsp

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>r2</title>
</head>
<body>
r2
</body>
</html>

效果图:
这里写图片描述

index.jsp主页代码及页面如下,页面内容为result的几种类型,并对四种主要讨论类型做了超链接,点击超链接即跳转到对应action

index.jsp

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>

<% String context = request.getContextPath(); %>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
Result类型
<ol>
    <li><a href="r/r1">dispatcher</a></li>
    <li><a href="r/r2">redirect</a></li>
    <li><a href="r/r3">chain</a></li>
    <li><a href="r/r4">redirectAction</a></li>
    <li>freemarker</li>
    <li>httpheader</li>
    <li>stream</li>
    <li>velocity</li>
    <li>xslt</li>
    <li>plaintext</li>
    <li>tiles</li>
</ol>

</body>
</html>

效果图
这里写图片描述

由代码分析可得:

dispatcher的超链接对应到第一个action r1,应用的是服务器跳转。

-所以显示的内容是r1.jsp的内容,而地址栏显示的地址是第一个action r1的地址。

这里写图片描述

redirect的超链接对应的是action r2,应用的是客户端跳转。

-所以页面显示的内容是r2.jsp的内容,而地址栏也是r2.jsp的地址。

这里写图片描述

chain 的超链接内容是action r3,result中的内容是action r1,说明过程如下图

这里写图片描述

-所以页面显示的结果如下图所示,地址栏显示是action r3,而实际内容是r1.jsp。

这里写图片描述

redirectAction超链接对应的内容是action3,result中的内容是action r1,说明过程如下图

这里写图片描述

-所以页面显示的结果如下图所示,地址栏显示是action r2,而实际内容也是r2.jsp。

这里写图片描述

本小节具体分析四种主要result类型的运行方式。由于前两种比较直接,并且涉及的多是servlet的相关知识点,便没有再画图,后两种类型的图,已附于文中。

若有不足之处,请不吝赐教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值