iframe 不显示 struts 操作类中的对象

发布于 2024-08-10 09:32:47 字数 2450 浏览 2 评论 0原文

这是我之前提出的问题的延续,该问题的框架不正确。

我在 jsp 类中有一个 iframe,它在其 src 中调用 struts2 操作类,但不是在框架内打开文件而是下载文件,

内部文件 TempContentPage.jsp:

<s:form>
<iframe id="displayFrame" src="ContentPage.action"  width="1000" height="500" FRAMEBORDER="0" value="1">&nbsp;</iframe>
</s:form>

这是操作类 ContentPage.java 中的执行方法

public Stringexecute() throws IOException {

    Session session = SessionUtil.getSession();
    session.beginTransaction();
    ServletOutputStream out = res.getOutputStream();
    ContentBase cb = new ContentBase();


    String quer = "from ContentBase cb where cb.parentType=? AND cb.parentId=? ";
    Query query = session.createQuery(quer);

    query.setParameter(0, "FILE");
    query.setParameter(1, "1");

    list = (ArrayList) query.list();

     if (null != list && !((java.util.ArrayList) list).isEmpty()) {
         cb = (ContentBase) ((java.util.ArrayList) list).get(0);
     }

     docContent = cb.getFile();
     res.reset();
     res.setContentType("application/msword");
     res.setHeader("Content-disposition", "inline; filename=\"scovr.doc\"");


     try{
     InputStream in = docContent.getBinaryStream();

     //InputStream iStream =  new ByteArrayInputStream (docContent.getBytes(0, (int) docContent.length()));

     int length = (int) docContent.length();
     int bufferSize = 1024;
     byte[] buffer = new byte[bufferSize];
        while ((length = in.read(buffer)) != -1) {
            out.write(buffer, 0, length);

        }

    player.setIsRead(true);

    in.close();

     }catch(Exception e){

         e.printStackTrace();
     }

    out.flush();
    return SUCCESS;
}

这是 struts.xml 映射,

<action name="ContentPage" class="com.zoran.action.ContentPage">
             <result name="success" type="stream">
           <param name="contentType">application/msword</param>
           <param name="inputName">in</param>
           <param name="bufferSize">1024</param>
           <param name="contentDisposition">inline</param>
          </result>

            <result name="error" >/pages/ContentPage.jsp</result>
            <result name="input" >/pages/ContentPage.jsp</result>
</action>

我想在 iframe 范围内打开文件,请帮助我(我从 Balusc 获得了宝贵的输入),因此对此代码进行了更改:)。

谢谢, 阿迪亚

This is in continuation with my previous question which was not framed properly.

I have an iframe in a jsp class which is calling a struts2 action class in its src, but instead of opening inside the frame the file is getting downloaded,

Inside file TempContentPage.jsp:

<s:form>
<iframe id="displayFrame" src="ContentPage.action"  width="1000" height="500" FRAMEBORDER="0" value="1"> </iframe>
</s:form>

Here is the execute method in the action class ContentPage.java

public String execute() throws IOException {

    Session session = SessionUtil.getSession();
    session.beginTransaction();
    ServletOutputStream out = res.getOutputStream();
    ContentBase cb = new ContentBase();


    String quer = "from ContentBase cb where cb.parentType=? AND cb.parentId=? ";
    Query query = session.createQuery(quer);

    query.setParameter(0, "FILE");
    query.setParameter(1, "1");

    list = (ArrayList) query.list();

     if (null != list && !((java.util.ArrayList) list).isEmpty()) {
         cb = (ContentBase) ((java.util.ArrayList) list).get(0);
     }

     docContent = cb.getFile();
     res.reset();
     res.setContentType("application/msword");
     res.setHeader("Content-disposition", "inline; filename=\"scovr.doc\"");


     try{
     InputStream in = docContent.getBinaryStream();

     //InputStream iStream =  new ByteArrayInputStream (docContent.getBytes(0, (int) docContent.length()));

     int length = (int) docContent.length();
     int bufferSize = 1024;
     byte[] buffer = new byte[bufferSize];
        while ((length = in.read(buffer)) != -1) {
            out.write(buffer, 0, length);

        }

    player.setIsRead(true);

    in.close();

     }catch(Exception e){

         e.printStackTrace();
     }

    out.flush();
    return SUCCESS;
}

And here is the struts.xml mapping

<action name="ContentPage" class="com.zoran.action.ContentPage">
             <result name="success" type="stream">
           <param name="contentType">application/msword</param>
           <param name="inputName">in</param>
           <param name="bufferSize">1024</param>
           <param name="contentDisposition">inline</param>
          </result>

            <result name="error" >/pages/ContentPage.jsp</result>
            <result name="input" >/pages/ContentPage.jsp</result>
</action>

I want to open the file inside the iframe scope, Please help me out ( I got valuable inputs from Balusc ) hence changes in this code :).

Thanks,
Aditya

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赤濁 2024-08-17 09:32:47

啊,你正在使用 Struts。然后我建议在此处查找真正的 Struts 文件下载示例。

但是,您还依赖于所使用的客户端(Web 浏览器)是否支持内联打开 MS Word 文档。我从未见过或尝试过它,但认为(如果)支持仅限于 MSIE(并且不会感到惊讶)。最好使用 PDF,因为有非常广泛的网络浏览器支持来内联显示它。您可以使用 iTextOOo 将 DOC 转换为 PDF。

或者,如果客户端需要能够编辑文档并将其保存回服务器,那么内联打开它就没有意义,只会让客户端更加困惑,因为没有办法“ “将同一文档保存在服务器端”,而不是将其保存到本地磁盘文件系统,然后再次手动上传到服务器端。

Ah, you're using Struts. Then I recommend to look for real Struts file download example here.

However, you're also dependent on the client (webbrowser) used if it supports opening a MS Word document inline. I have never seen nor tried it, but think (and won't be surprised if) the support is limited to MSIE. Better use PDF instead, there is very wide webbrowser support to display it inline. You can use iText or OOo to convert DOC to PDF.

Or if it is the intent that the client is needs to be able to edit the document and save back to the server, then opening it inline would make no sense and it would only be more confusing for the client, because there's no way to "save" the very same document on the server side other than saving it to the local disk file sytem and then manually uploading to the server side again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文