StrutsResultSupport的使用
在有特殊情况时;如果没有异常信息,但是有错误并且有错误信息等内容;此时也需要进行友好的错误处理的话,那么可以借助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的使用的更多相关文章
- 扩展struts2的结果集StrutsResultSupport 自定义Result处理JSON
以前在采用Struts2开发的项目中,对JSON的处理一直都在Action里处理的,在Action中直接Response,最近研读了一下Struts2的源码,发现了一个更加优雅的解决办法,自己定义一个 ...
- struts debug 标签
< s:debug> 引起下面的错误 org.apache.jasper.JasperException: Caught an exception while getting the pr ...
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- Struts2 源码分析——Result类实例
本章简言 上一章笔者讲到关于DefaultActionInvocation类执行action的相关知识.我们清楚的知道在执行action类实例之后会相关处理返回的结果.而这章笔者将对处理结果相关的内容 ...
- 【Java EE 学习 34】【struts2学习第一天】
一.struts2简介 struts2是一个用来开发MVC应用程序的框架.它提供了Web应用程序开发过程中的一些常见问题的解决方案. 1.struts2的作用域范围:三层架构当中的第一层,相当于MVC ...
- SSH项目(1)
1.新建项目,添加jar包 tomcat jsp struts.hibernate.spring 2.配置 web.xml <?xml version="1.0" encod ...
- 使用jasperreports-5.6.0.jar导致的问题
使用jasperreports-5.6.0.jar导致的问题 Struts2+jasperReport5.6如下设置: <!-- 社员档案 --> <package name=&qu ...
- javax.el.PropertyNotFoundException: 异常处理
javax.el.PropertyNotFoundException: Property 'policyId' not found on type com.omhy.common.model.enti ...
- Struts2:java.lang.NoSuchFieldException: resourceEntries at java.lang.Class.getDeclaredField(Class.java:1901)
今天在做Struts2的测试用例时候,程序能正常跳转,但是在Console却报了一个错误,如下: java.lang.NoSuchFieldException: resourceEntries at ...
随机推荐
- spring boot 搭建基本套路《1》
1. Spring复习 Spring主要是创建对象和管理对象的框架. Spring通过DI实现了IoC. Spring能很大程度的实现解耦. 需要掌握SET方式注入属性的值. 需要理解自动装配. 需要 ...
- Hello,移动WEB—px,dp,dpr像素基础
问题点1:iphone5分辨率:640 * 1136 dp,为什么chrome浏览器F12中显示的320 *568?? iPhone5 分辨率640 * 1136指的是物理像素,而实际 ...
- admin添加用户时报错:(1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_l
在stackoverflow找到答案: DATABASES = { 'default': { ... 'OPTIONS': { "init_command": "SET ...
- python 装饰器 生成及原里
# 装饰器形成的过程 : 最简单的装饰器 有返回值的 有一个参数 万能参数 # 装饰器的作用 # 原则 :开放封闭原则 # 语法糖 :@ # 装饰器的固定模式 #不懂技术 import time # ...
- go学习笔记-运算符
运算符 运算符 内置运算符 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 其他运算符 算术运算符 假定 A 值为 10,B 值为 20. 运算符 描述 实例 + 相加 A + B 输出结果 ...
- SEARCH(文字の検索)
文字列に関する無効命令 以下の各命令は無効であり.4.6 および 6.10 までのリリースとの互換性を確保するためにのみ利用可能となっています.これらの命令が古いプログラムの中に出現することはあります ...
- shell重温---基础篇(流程控制&if判断&for&while&循环操作)
和Java.PHP等语言不一样,sh的流程控制不可为空,如(以下为PHP流程控制写法): <?php if (isset($_GET["q"])) { search( ...
- Windows2008新建域时Administrator 帐户密码不符合要求
Windows 2008 系统安装完毕后,(环境:在安装的时间,系统没有设置密码.做好系统后,进入制面板添加了密码或按ctrl + alt + del 设置密码后 在服务器管理-角色 ...
- 云计算之路-阿里云上:Web服务器请求到达量突降
今天下午遇到了自使用阿里云以来首次遇到的新情况——http.sys的ArrivalRate突降(说明请求到达IIS的请求数量少了),而且SLB中的3台ECS都出现了这个问题. 1. 10.161.24 ...
- 多个Target的使用
背景介绍 开发过程中,我们会在内网搭建一个测试服务器,开发.测试都是在内网进行的.这样产生脏数据不会影响外网的服务器.外网服务器只有最后发布时才会进行一些必要的测试. 还有就是要对同一份代码生成不同的 ...