在有特殊情况时;如果没有异常信息,但是有错误并且有错误信息等内容;此时也需要进行友好的错误处理的话,那么可以借助StrutsResultSupport 返回结果类型来实现特定处理。此种方式先需要继承StrutsResultSupport ,然后可以在子类中获取本次请求的相关信息,再根据相关信息进行结果处理:

package commons.struts2;
import <a href="http://lib.csdn.net/base/java" class='replace_word' title="Java 知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import com.opensymphony.xwork2.ActionInvocation;
/**
* result type for output string in action
*
* @author songwei,yaolei <b>Example:</b>
*
* <pre>
* <!-- START SNIPPET: example -->
* <result name="success" type="string">
* <param name="stringName">stringName</param>
* </result>
* <!-- END SNIPPET: example -->
* </pre>
*
*/
public class StringResultType extends StrutsResultSupport {
private static final long serialVersionUID = 1L;
private String contentTypeName;
private String stringName = "";
public StringResultType() {
super();
}
public StringResultType(String location) {
super(location);
}
protected void doExecute(String finalLocation, ActionInvocation invocation)
throws Exception {
HttpServletResponse response = (HttpServletResponse) invocation
.getInvocationContext().get(HTTP_RESPONSE);
// String contentType = (String)
// invocation.getStack().findValue(conditionalParse(contentTypeName,
// invocation));
String contentType = conditionalParse(contentTypeName, invocation);
if (contentType == null) {
contentType = "text/plain; charset=UTF-8";
}
response.setContentType(contentType);
PrintWriter out = response.getWriter();
// String result = conditionalParse(stringName, invocation);
String result = (String) invocation.getStack().findValue(stringName);
out.println(result);
out.flush();
out.close();
}
public String getContentTypeName() {
return contentTypeName;
}
public void setContentTypeName(String contentTypeName) {
this.contentTypeName = contentTypeName;
}
public String getStringName() {
return stringName;
}
public void setStringName(String stringName) {
this.stringName = stringName;
}
}
package test;
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport{
String result="abc";
public String ajax() {
return "ajaxResponse";
}
// getter && setter
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
} }
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="test" extends="struts-default">
<result-types>
<result-type name="string" class="test.StringResultType"></result-type>
</result-types> <action name="myAction" class="test.MyAction" >
<result name="ajaxResponse" type="string">
<param name="stringName">result</param>
</result>
</action>
</package>
</struts>

这里定义返回Action的String  result变量,即“abc”。

StrutsResultSupport的使用的更多相关文章

  1. 扩展struts2的结果集StrutsResultSupport 自定义Result处理JSON

    以前在采用Struts2开发的项目中,对JSON的处理一直都在Action里处理的,在Action中直接Response,最近研读了一下Struts2的源码,发现了一个更加优雅的解决办法,自己定义一个 ...

  2. struts debug 标签

    < s:debug> 引起下面的错误 org.apache.jasper.JasperException: Caught an exception while getting the pr ...

  3. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  4. Struts2 源码分析——Result类实例

    本章简言 上一章笔者讲到关于DefaultActionInvocation类执行action的相关知识.我们清楚的知道在执行action类实例之后会相关处理返回的结果.而这章笔者将对处理结果相关的内容 ...

  5. 【Java EE 学习 34】【struts2学习第一天】

    一.struts2简介 struts2是一个用来开发MVC应用程序的框架.它提供了Web应用程序开发过程中的一些常见问题的解决方案. 1.struts2的作用域范围:三层架构当中的第一层,相当于MVC ...

  6. SSH项目(1)

    1.新建项目,添加jar包 tomcat jsp struts.hibernate.spring 2.配置 web.xml <?xml version="1.0" encod ...

  7. 使用jasperreports-5.6.0.jar导致的问题

    使用jasperreports-5.6.0.jar导致的问题 Struts2+jasperReport5.6如下设置: <!-- 社员档案 --> <package name=&qu ...

  8. javax.el.PropertyNotFoundException: 异常处理

    javax.el.PropertyNotFoundException: Property 'policyId' not found on type com.omhy.common.model.enti ...

  9. Struts2:java.lang.NoSuchFieldException: resourceEntries at java.lang.Class.getDeclaredField(Class.java:1901)

    今天在做Struts2的测试用例时候,程序能正常跳转,但是在Console却报了一个错误,如下: java.lang.NoSuchFieldException: resourceEntries at ...

随机推荐

  1. 两台Linux主机 scp免密传输

    两台服务器IP如下配置 Linux1: 10.0.0.1   Linux2: 10.0.0.2 Linux1服务器执行如下操作: #  ssh-keygen -t rsa 然后一直回车就行 # sud ...

  2. 添加fileinfo扩展

    首先声明:笔者用的是军哥的lnmp一键安装包!链接地址:https://lnmp.org 打开upgrade_php.sh配置文件[文件所在位置:~/lnmp1.4/include/upgrade_p ...

  3. java后台去除JSON数组的重复值

    假设原始Json数组是这样的 原始JSONArry:[{"Value":"15153129877","Key":"09770985 ...

  4. webBrowser 应用编程函数总结

    /*============================说明部分================================= 实现一下函数需包含头文件 #include <Winine ...

  5. JQ 封装全选函数

    单击时触发效果: 如果有一个没有选中把全选的勾去了,如果select所有的都选中了,那就把全选勾上 html里: <div class="row cl"> <la ...

  6. OMAPL138制作SD卡启动介质及重装Linux系统

    OMAPL138制作SD卡启动盘及重装Linux系统 手里的创龙的OMAPL138平台的系统SSH坏掉了,我重新移植了openssh还是不好使,没有办法了只能重装OMAPL138的系统了,按照创龙给的 ...

  7. Leecode刷题之旅-C语言/python-9.回文数

    /* * @lc app=leetcode.cn id=9 lang=c * * [9] 回文数 * * https://leetcode-cn.com/problems/palindrome-num ...

  8. C语言数据结构(二)

    算法和算法的衡量 一.算法 算法是为了解决某类问题而规定的一个有限长的操作序列.一个算法必须满足以下五个重要特性: 1.有穷性   对于任意一组合法输入值,在执行又穷步骤之后一定能结束,即:算法中的每 ...

  9. MOVE-PERCENTAGE(文字列の部分の代入)

    以下の MOVE 命令のバリアントは.c 型項目についてのみ機能します. MOVE c1 TO c2 PERCENTAGE p [RIGHT]. 左寄せした (RIGHT オプションを指定した場合は右 ...

  10. 关于cookie的一些学习笔记

    0x00 发现自己对一些原理性的东西实在是太不了解 最近看了<cookie之困>记一下笔记 0x01 因为http是无状态的 所以需要cookie和session来保持http的会话状态和 ...