struts2 Result Type四个常用转跳类型

时间:2023-03-09 08:58:32
struts2 Result Type四个常用转跳类型

Result的四个常用转跳类型分别为

Dispatcher    用来转向页面,是Struts的默认形式

Redirect       重定向到一个URL

Chain       用来处理Action链

RedirectAction     重定向到一个Action

还有以下几种不太常用

freemaker:  处理FreeMarker模板

httpheader:  控制特殊HTTP行为的结果类型

stream:      向浏览器发送InputSream对象,用来处理文件下载,还可用于返回AJAX数据

velocity :    处理Velocity模板

xsl:             处理XML/XLST模板

plaintext:   显示原始文件内容,例如文件源代码

这里只说最常用的4种

看一下struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<!-- Add packages here -->
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<action name="add1" >
<result type="dispatcher">
/Oneaddok.jsp
</result>
</action>
<action name="add2" >
<result type="redirect">
/Oneeditok.jsp
</result>
</action>
<action name="add3" >
<result type="chain">
add1
</result>
</action>
<action name="add4">
<result type="redirectAction">
add2
</result>
</action>
</package>
</struts>

再看一个jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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 'Index.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 my JSP page. <br>
<li><a href="add1">Dispatcher</a></li>
<li><a href="add2">Redirect</a></li>
<li><a href="add3">Chain</a></li>
<li><a href="add4">RedirectAction</a></li>
</body>
</html>

就是这么简单,自己动手试一下吧

源代码:Struts15AccessResultType.rar