Search code examples
javaiframestruts2google-document-viewer

Display doc file in iframe struts2


I want to show .doc and .docx files on my website in an iframe.

<iframe style="border-style: none;" src="http://docs.google.com/viewer?embedded=true&url=<s:url action="loadFile"><s:param name="id" value="id"/></s:url>" height="500" width="100%">

The loadFile action sends a result of type stream

<action name="loadFile" class="com.view.file" method="loadFile">
                <result name="success" type="stream">
                    <param name="contentType">${fileType}</param>
                    <param name="inputName">inputStream</param>
                    <param name="contentDisposition">inline;filename="${fileName}"</param>
                    <param name="bufferSize">1024</param>
                </result>
</action>

The iframe works when I set the action url directly in the src attribute. However, with src="http://docs.google.com/viewer?embedded=true&url= the action is never called. Is there any other alternative to show a MS Word file? I'm able to show a pdf by directly putting the action url in src attribute.

Thanks

EDIT

The problem may be because the src of iframe converts to:

src="http://docs.google.com/viewer?embedded=true&url=/App/loadFile?id=25"

The required url should be the absolute url to the action like:

src="http://docs.google.com/viewer?embedded=true&url=http://localhost/App/loadFile?id=25"

How can I achieve that? It might not work on localhost but should work on a hosting


Solution

  • Use the attribute forceAddSchemeHostAndPort of s:url tag to get the complete url

    More Info

    For Example

    <s:url action='myaction' forceAddSchemeHostAndPort='true'>
    <s:param name='id' value='25'/>
    </s:url>