重写FileUpload拦截器的问题

palocc 2012-11-26 06:20:18
因为项目用ext作为前端,因此struts的返回都是基于json的,这样产生一个问题,fielUploadInterceptor拦截到后,如果不符合条件,比如文件类型不对,大小不对等,默认返回input。

我们的项目定义,在父类action中有2个特定对象:

private String errorMsg;
private boolean success; //
或“false”

的对象,ext通过读取success的值,如果是"true", 正常显示,如果是“false”,则读取errorMsg对象获取错误提示。

好了,现在问题是这样,为了符合这个需求,我自定义了一个拦截器,继承FileUploadInterceptor

public class JsonFileUploadInterceptor extends FileUploadInterceptor
{
private Log log = LogFactory.getLog(this.getClass());

@Override
public String intercept(ActionInvocation invocation) throws Exception
{
log.debug("json file upload interceptor start ....");
String result = "";
try
{
result = super.intercept(invocation);
if ("input".equalsIgnoreCase(result))
{
BasicAction action = (BasicAction) invocation.getAction();
action.setSuccess(false);
action.setErrorMsg("上传失败");
log.debug("json file upload interceptor end ....");
return result;
}
}
catch (Exception e)
{
BasicAction action = (BasicAction) invocation.getAction();
action.setSuccess(false);
//如果action中具有actionError,表示上传文件超过struts默认的最大大小maxSize,
if(action.hasActionErrors() == true)
{
action.setErrorMsg(action.getText(ErrorMsgConstant.ERROR_UPLOADFILE_FILE_TOO_LARGE));
return "success";
}
else
{
return "input";
}
}
log.debug("json file upload interceptor end ....");
return result;
}



现在问题是这样,如果我在action中没有定义result = "input"的result,那么当执行到
result = super.intercept(invocation);

的时候,会抛出异常(因为找不到input这个result),进入到catch环节,此时,通过invocation对象获取action,然后对action中的success和errorMsg赋值,此时页面ext是能够拿到这两个值,从而正常显示。

但问题是,如果我定义了input,那么就不会抛异常,虽然struts2官方的FileUploadInterceptor返回的是input,随后进入我的if()环节,最终return的时候,页面上却什么值也接受不到,我执行的所有的action.setXXX都无效


这是为什么呢?
...全文
50 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧