在struts 2中的文件下载中文件不存在时无法映射 [英] Unable to map when file is not existed in file downloading in struts 2

查看:34
本文介绍了在struts 2中的文件下载中文件不存在时无法映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 struts2 开发 Web 应用程序 .. 因为当用户单击链接时我正在下载文件 ..当用户单击链接时,我正在检查我的操作类是否存在请求的文件,如果存在则它工作正常......当它不存在时,我给出了一个操作错误消息..但是那个页面正在重定向到我全局映射的错误页面..在我的控制台中,除了这些错误行之外没有异常消息..在调用堆栈中找不到名称为 [fileInputStream] 的 java.io.InputStream.检查<param name="inputName">为这个动作指定的标签.而且我可以在我的错误页面中看到该操作消息......我也调试了这个问题......映射也很完美......

当 SYSTEM 中不存在该文件时,是否可以映射回相同的 JSP ..谢谢

解决方案

在action中检查文件是否存在,返回success &error 分别.你可以这样做:

<块引用>

error.jsp

<s:actionmessage/>或者你的硬编码信息

但是,因为您需要返回到同一页面,所以我建议使用 ajax 调用下载操作,并在成功时返回 200,在错误时返回 204,如下所示:

ajax 调用可能类似于 - docs

$.ajax({状态代码:{404:函数(){alert("找不到页面");},204:函数(){alert("找不到文件");}}});

I am developing a web application using struts2 .. In that I am downloding a file when user clicks a link .. When user clicks the link I am checking in my action class whether requested file is existed or not if it is existed then it is working fine ... WHen it is not existed I am giving a action error message .. But That page is redirecting to error page that i have mapped globally .. IN my console ther is no exception message except these error lines .. Can not find a java.io.InputStream with the name [fileInputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action. And I can see that action message in my error page ... I have debugged the problem also .. Mapping also perfect ..

<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
      <result name="success" type="stream">              
          <param name="contentType">application/octet-stream</param>
          <param name="inputName">fileInputStream</param>
          <param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
          <param name="bufferSize">1024</param>
       </result>

Is It possible to map back to the same JSP when The file is not existed in SYSTEM .. Thanks

解决方案

Check whether the file exists or not in the action, and return success & error respectively. You can do something like this :

<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
      <result name="success" type="stream">              
          <param name="contentType">application/octet-stream</param>
          <param name="inputName">fileInputStream</param>
          <param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
          <param name="bufferSize">1024</param>
       </result>
<result name="error">/error.jsp</result>

error.jsp

<s:actionerror/>
<s:actionmessage/>
or your hard-coded message

But since, you need to go back to the same page, then I suggest calling the download action using ajax and return 200 on success and 204 on error something like this :

<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
          <result name="success" type="stream">              
              <param name="contentType">application/octet-stream</param>
              <param name="inputName">fileInputStream</param>
              <param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
              <param name="bufferSize">1024</param>
           </result>
    <result name="error" type="httpheader">
         <param name="status">204</param>
    </result>

The ajax call can be something like - docs

$.ajax({
  statusCode: {
    404: function() {
      alert( "page not found" );
    },
    204: function() {
      alert( "file not found" );
    }
  }
});

这篇关于在struts 2中的文件下载中文件不存在时无法映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆