在有特殊情况时;如果没有异常信息,但是有错误并且有错误信息等内容;此时也需要进行友好的错误处理的话,那么可以借助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. 洛谷P3871 [TJOI2010]中位数(splay)

    题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从 ...

  2. django项目创建requirements.txt文件

    workon+虚拟环境名 然后输入 pip freeze > requirements.txt

  3. redis 带入的挖矿病毒 qW3xT.2 wnTKYg 解决方法

    最近我的阿里云ecs 老是收到 云盾态势感知系统检测到异常 top -c 后发现一个 疑似病毒  /tmp/qW3xT.2 看到网友们的解决方案 试过之后效果不错,可以用的 知道wnTKYg是什么鬼之 ...

  4. Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO

    你有碰上过这样的提示吗? Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in t ...

  5. 洛谷 T51922 父子

    题目描述 对于全国各大大学的男生寝室,总是有各种混乱的父子关系. 那么假设现在我们一个男生寝室有不同的 nn 个人,每个人都至多有一个“爸爸”,可以有多个“儿子”,且有且只有一个人没有“爸爸”(毕竟是 ...

  6. STM32(3)——外部中断的使用

    1 .简介 ARM Coetex-M3内核共支持256个中断,其中16个内部中断,240个外部中断和可编程的256级中断优先级的设置.STM32目前支持的中断共84个(16个内部+68个外部),还有1 ...

  7. CentOS6.9重新安装python2.6.6和yum

    CentOS6.9重新安装python2.6.6和yum 本文转载自昔日暖阳,原文地址:http://www.osheep.cn/4801.html 最近为了部署一个Python应用到腾讯云服务器,强 ...

  8. Drupal 网站漏洞修复以及网站安全防护加固方法

    drupal是目前网站系统使用较多一个开源PHP管理系统,架构使用的是php环境+mysql数据库的环境配置,drupal的代码开发较为严谨,安全性较高,但是再安全的网站系统,也会出现网站漏洞,dru ...

  9. vue2.0 Axios 的简单用法

    安装 使用 npm: $ npm install axios 使用 bower: $ bower install axios 使用 cdn: <script src="https:// ...

  10. mybatis-generator自定义注释生成

    最近做的项目发现没有中文注释,故查找资料,特此记录. 本文所用的是基于mybatis-generator 1.3.2版本来完成的. mybatis-generator 自动生成的代码注释是很反人类的, ...