本章节将继续学习struts2的返回类型的使用方法。

学习文档下载struts2 full包解压后会在doc下包含离线html文档。

点击运行后页面:

点击Guides向导终将会有向导列表

再点开后,就可以看到struts2支持的所有resultType:

默认的resultType是dispatcher.

常用的dispatcher,redirect,redirectAction,chain.

 package name="default" namespace="/" extends="struts-default">
<action name="testServletObject"
class="com.dx.actions.TestActionContextFetchServletObjectAction">
<result>/WEB-INF/test-context.jsp</result>
</action>
<!-- default class="com.opensymphony.xwork2.ActionSupport" method="execute" -->
<action name="gotoLoginPage">
<!-- name default value:"success" -->
<result>/login.jsp</result>
</action> <action name="logout" class="com.dx.actions.TestWithXXXAware"
method="logout">
<result name="logout-success">/login.jsp</result>
</action> <action name="login" class="com.dx.actions.TestWithXXXAware" method="login">
<result name="login-success">/WEB-INF/login-success.jsp</result>
</action> <action name="testResult" class="com.dx.actions.TestResultAction" method="test">
<!-- redirect到一个Action -->
<result name="redirectAction-success" type="redirectAction">
<param name="actionName">testRdirect</param>
<param name="namespace">/testNamespace</param>
</result>
<!-- redirect -->
<result name="redirect-success" type="redirect">/login.jsp</result>
<!-- dispatcher -->
<result name="dispatcher-success" type="dispatcher">/login.jsp</result>
</action> <action name="createAccount" class="com.dx.actions.AccountAction" method="execute">
<!-- chain -->
<result type="chain">login</result>
</action>
</package> <package name="testRedirectPackage" namespace="/testNamespace" extends="struts-default">
<action name="testRdirect" class="com.dx.actions.TestRedirectAction" method="test">
<result name="test-success">/WEB-INF/test-success.jsp</result>
</action>
</package>

TestRedirectAction.java

 package com.dx.actions;

 import org.apache.struts2.ServletActionContext;

 public class TestRedirectAction {
public String test() {
String resultType = ServletActionContext.getRequest().getParameter("resultType");
System.out.println(resultType);
ServletActionContext.getRequest().setAttribute("resultType", resultType);
System.out.println("TestRedirectAction.test");
return "test-success";
}
}

TestResultAction.java

 package com.dx.actions;

 import org.apache.struts2.ServletActionContext;

 import com.opensymphony.xwork2.ActionContext;

 import sun.nio.cs.ext.TIS_620;

 public class TestResultAction {
private String resultType; public void setResultType(String resultType) {
this.resultType = resultType;
} public String test() {
System.out.println(resultType); if (resultType.equalsIgnoreCase("dispatcher")) {
System.out.println("result dispatcher-success");
return "dispatcher-success";
} else if (resultType.equalsIgnoreCase("redirect")) {
System.out.println("result redirect-success");
return "redirect-success";
} else if (resultType.equalsIgnoreCase("redirectAction")) {
System.out.println("result redirectAction-success");
return "redirectAction-success";
} else if (resultType.equalsIgnoreCase("chain")) {
System.out.println("result chain-success");
return "chain-success";
} else {
System.out.println("no match");
} ServletActionContext.getRequest().setAttribute("resultType", this.resultType); return "";
}
}

AccountAction.java

 package com.dx.actions;

 public class AccountAction {
public String execute(){
System.out.println("acction action chain");
return "success";
}
}

index.jsp

<a href="testResult?resultType=redirectAction">redirectAction</a>
<br />
<a href="testResult?resultType=redirect">redirect</a>
<br />
<a href="testResult?resultType=dispatcher">dispatcher</a>
<br />
<a href="createAccount">test chain</a>

result配置还有很多需要注意的地方,可以参考:

file:///D:/Java_Study/struts-2.3.28-all/struts-2.3.28/docs/docs/result-configuration.html

这里就简单说明,介绍,一遍自己可以学习使用。

Struts2(六):ResultType的更多相关文章

  1. struts2(六) 文件上传和下载

    前面对文件下载提过一点点,这里正好要讲文件上传,就放在一起在说一遍. --WH 一.单文件上传 在没学struts2之前,我们要写文件上传,非常麻烦,需要手动一步步去获取表单中的各种属性,然后在进行相 ...

  2. Struts2(六)result

    一.result简述 result:输出结果:第个Action返回一个字符串,Struts2根据这个值来决定响应结果 name属性:result的逻辑名.和Actin里的返回值匹配,默认"s ...

  3. Struts2六、为应用指定多个配置文件

    为了使用Struts.xml更简洁,更利于维护,我们可以把Struts.xml要配置的Action信息分类别放在其他的XML文件中,使用include在struts.xml中加载这些文件: 将Web. ...

  4. Struts2(六) 用Struts完成客户列表显示

    Struts完成客户列表显示 所用的基础知识 在之前的随笔中已经讲过.这篇是介绍如何使用Struts 完成客户列表显示  . 下面是  完成的代码执行逻辑图: 抽取项目部分代码 相信大家认真看一遍就明 ...

  5. Struts2(六.用标签显示用户列表及Value Stack和Stack Context)

    一.用Struts2标签显示用户列表 原理: 在struts中可以通过在action中将所有用户的信息存入到某个范围中,然后转向userlist.jsp,进行访问 原则: 在jsp网页上,尽量不要出现 ...

  6. Struts2常量_Action配置路径_通配符

    Struts2中常用的常量 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法 和freemarker .velocity的输出 <cons ...

  7. Struts2学习(四)

    struts-defualt.xml指定的result的类型 1.struts-defualt.xml 文件的 181 行 开始定义了: <result-types> <result ...

  8. (五)Struts2处理结果管理

    当Action处理完用户请求时,处理结果应该通过视图资源实现,但将哪个视图呈现给浏览者呢.由<result.../>来决定 Action处理完用户请求后,返回一个普通字符串.整个普通字符串 ...

  9. 【struts2】Result和ResultType

    简单的说,Result是Action执行完后返回的一个字符串,它指示了Action执行完成后,下一个页面在哪里.Result仅仅是个字符串,仅仅是用来指示下一个页面的,那么如何才能够到达下一个页面呢? ...

随机推荐

  1. JS:操作样式表1:行内样式

    //访问元素样式1, stye属性只对行内样式有用 var box = document.getElementById("box"); // alert(box.style.col ...

  2. 【BZOJ2818】Gcd 欧拉筛

    Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...

  3. lsof用法简介

    lsof:一个功能强大的命令 lsof命令的原始功能是列出打开的文件的进程,但LINUX下,所有的设备都是以文件的行式存在的,所以,lsof的功能很强大!  [root@limt01 ~]# lsof ...

  4. 分模块创建maven项目(一)

    maven是一个项目构建和管理的工具. 我们可以通过maven仓库可以实现管理构建(主要是JAR还包括:WAR,ZIP,POM等等). 我们可以通过maven插件可以实现编译源代.产生Javadoc文 ...

  5. [LintCode] Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors.You have to paint a ...

  6. 一次有趣的XSS漏洞挖掘分析(1)

    最近认识了个新朋友,天天找我搞XSS.搞了三天,感觉这一套程序还是很有意思的.因为是过去式的文章,所以没有图.但是希望把经验分享出来,可以帮到和我一样爱好XSS的朋友.我个人偏爱富文本XSS,因为很有 ...

  7. php for循环嵌套

    <?php     //2.打印一个50*50的 'o' 的正方形方整, 使用for的嵌套     // oooooo     // oooooo     // oooooo //for循环嵌套 ...

  8. Excel报表开发

    读取Excel数据 /// <summary> /// 封装方法 /// </summary> /// <param name="path">& ...

  9. css3 animation 实现环形路径平移动画

    注意 @keyframes to/from 的学习 <!DOCTYPE html> <html lang="en"> <head> <me ...

  10. php对二维数组进行相关操作(排序、转换、去空白等)

    php对二维数组进行相关操作(排序.转换.去空白等) 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-11-04   这篇文章主要介绍了php对二维数组进行相关操作,包括php对 ...