You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tiles.apache.org by Ryan Henson <nw...@yahoo.com> on 2008/07/09 15:07:45 UTC

Struts-Tiles - Missing Content

I think I have found a strange behaviour in the Struts-Tiles framework that I am attempting to pin down and understand.

Here is my scenario, I'm developing a struts/tiles application, part of
which contains a reporting mechanism for users to query against. I have
the ability to print a report, but I also want the ability to PDF that
exact same report and save it for the user. To accomplish that task I
am using the iText API and a HttpURLConnection to retrieve the report
content.

I've been experimenting with multiple variations on how to accomplish
this so my below code is a little messy, but it does create and return
a PDF, but it is missing the content it should have. As far as I can
tell from the order of events the following occurs. I open my browser
to the makePDF.do action which sets up an HttpURLConnection to the
getReport action. The getReport action then forwards to the tiles name
which retrieves the layout jsp and is where it should insert the tiles
attributes and get the report content. This is where it breaks down
however and the only HTML that is returned is from the layout page.

My goal is to simply convert the resulting HTML code into a PDF
document, so if there is another approach I could take I am open to it.
This is the best method I could figure that would allow me to maintain
just one copy of the report rather than having to build both a
printable and PDF version of the report and ensuring they look the same.

I've also noticed something interesting, and
that is in the PDF there is text rendered for the tiles tag
<tiles:getAsString name="title" /> which tells me that the
HttpURLConnection is getting the tiles attributes, but where it is
failing is on the <tiles:insert attribute="body" /> tag. That at
least narrows the problem down to the tiles:insert line specifically
and not tiles overall. 

How can I make this work???

THANKS!
-Ryan Henson

PDFAction.java

/*
 * @author: Ryan Henson Created on July 7, 2008
 * 
 * Purpose: This file takes an input source, converts it into a PDF, and returns it
 */
package org.airybsa.struts.control;
 
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
 
import com.lowagie.text.*;
import com.lowagie.text.html.HtmlParser;
import com.lowagie.text.pdf.PdfWriter;
 
public class PDFAction extends DispatchAction {
public ActionForward makePDF(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
// prepare a string of key value pairs.
			String paramStr = "func=getReport&print=true&reportID=1&selectedIDs=315,158";
 
// POST requests are required to have Content-Length
			String lengthString = String.valueOf(paramStr.length());
 
/*
			 * Establish a connection to the application.
			 */
			URL url = new URL("http://localhost:8080/AirySchedule/getReport.do");
			URLConnection con = url.openConnection();
 
/*
			 * configure the connection to allow for the operations necessary.
			 * 1. Turn off all caching, so that each new request/response is
			 * made from a fresh connection with the servlet. 2. Indicate that
			 * this client will attempt to SEND and READ data to/from the
			 * servlet.
			 */
			con.setUseCaches(false);
			con.setDefaultUseCaches(false);
			con.setDoInput(true);
			con.setDoOutput(true);
// Set the content type we are POSTing and length of the data.
			con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
			con.setRequestProperty("Content-Length", lengthString);
 
			OutputStream os = con.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(os);
// send data to servlet
			osw.write(paramStr);
			osw.flush();
			osw.close();
 
			Document document = new Document();
			document.setPageSize(PageSize.A4.rotate());
			OutputStream out = response.getOutputStream();
 
			response.addHeader("Content-Type", "application/pdf");
//response.addHeader("Content-Disposition", "attachment; filename=\"unnamed.pdf\"");
//response.addHeader("Content-Length", "" + cachedURL.length());
 
try {
				PdfWriter.getInstance(document, out);
				HtmlParser.parse(document, con.getInputStream());
} catch (Exception e) {
				log.error(e);
}
 
} catch (Throwable t) {
			log.error(t);
}
return null;
}
}


      

Re: Struts2 tiles

Posted by Ivan Zucenko <zu...@gmail.com>.
Hi  Antonio,
OK, I just may recommend that being able to include none template on 
specific position would add some more comfort to Tiles.. since I like to 
use tiles.xml for exactly this purpose - to decide where to show or not 
show some element.. on certain position..

But thanks anyway..
Ivan

Antonio Petrelli wrote:
> 2008/7/9 Ivan Zucenko <zu...@gmail.com>:
>   
>> 1) I have one main layout template for many situations. There is a place
>> where I want to put or not to put one special component..
>> ...
>>  in one definition I need to show somethink in "foot" section
>>  in most situation nothing.. but do not like to use empty.jsp for this.. I
>> am sure I misser right solution....
>>     
>
> I think your solution is right, except in the case you have a
> "strange" result as an HTML document, such as an empty <div>. In this
> case I suggest to change the template that you use.
>
>   
>> 2) how to include instead of value="modul/news.jsp" rather struts action
>> tag..
>> not to have modul/news.jsp containing:
>> <s:action name="modul/news" executeResult="true"/>
>>
>> but i can understand you may consider too tight coupling with Struts2
>>     
>
> See this previous e-mail:
> http://www.nabble.com/Tiles2-import-Struts2-action-via-insertTemplate-td13834640.html
> A template and an attribute value work mostly the same way.
>
> Antonio
>
>   


Re: Struts2 tiles

Posted by Antonio Petrelli <an...@gmail.com>.
2008/7/9 Ivan Zucenko <zu...@gmail.com>:
> 1) I have one main layout template for many situations. There is a place
> where I want to put or not to put one special component..
>...
>  in one definition I need to show somethink in "foot" section
>  in most situation nothing.. but do not like to use empty.jsp for this.. I
> am sure I misser right solution....

I think your solution is right, except in the case you have a
"strange" result as an HTML document, such as an empty <div>. In this
case I suggest to change the template that you use.

> 2) how to include instead of value="modul/news.jsp" rather struts action
> tag..
> not to have modul/news.jsp containing:
> <s:action name="modul/news" executeResult="true"/>
>
> but i can understand you may consider too tight coupling with Struts2

See this previous e-mail:
http://www.nabble.com/Tiles2-import-Struts2-action-via-insertTemplate-td13834640.html
A template and an attribute value work mostly the same way.

Antonio

Struts2 tiles

Posted by Ivan Zucenko <zu...@gmail.com>.
Hi I have 2 questions I tried to find solution for two things on the 
edge of Struts2/Tiles

1) I have one main layout template for many situations. There is a place 
where I want to put or not to put one special component..

example:

    <definition name="index.default" extends="default">
        <put-attribute name="body" 
value="/templates/tiles/index/default.jsp"/>
        *<put-attribute name="foot" value="modul/news.jsp"/>*
    </definition>

    <definition name="index.logged" extends="default">
        <put-attribute name="body" 
value="/templates/tiles/index/default.jsp"/>
        *<put-attribute name="foot" value="empty.jsp"/>*
    </definition>

  in one definition I need to show somethink in "foot" section
  in most situation nothing.. but do not like to use empty.jsp for 
this.. I am sure I misser right solution....


2) how to include instead of value="modul/news.jsp" rather struts action 
tag..
not to have modul/news.jsp containing:
<s:action name="modul/news" executeResult="true"/>

but i can understand you may consider too tight coupling with Struts2

Thanks
Ivan




Re: Struts-Tiles - Missing Content

Posted by Antonio Petrelli <an...@gmail.com>.
2008/7/9 Ryan Henson <nw...@yahoo.com>:
> I think I have found a strange behaviour in the Struts-Tiles framework that I am attempting to pin down and understand.

First of all, what version of Struts and Tiles are you using?

> I've been experimenting with multiple variations on how to accomplish
> this so my below code is a little messy, but it does create and return
> a PDF, but it is missing the content it should have. As far as I can
> tell from the order of events the following occurs. I open my browser
> to the makePDF.do action which sets up an HttpURLConnection to the
> getReport action. The getReport action then forwards to the tiles name
> which retrieves the layout jsp and is where it should insert the tiles
> attributes and get the report content. This is where it breaks down
> however and the only HTML that is returned is from the layout page.

You're mixing PDF and HTML. Do you want to return a PDF to the user?
So use directly the HttpServletResponse.

> My goal is to simply convert the resulting HTML code into a PDF
> document, so if there is another approach I could take I am open to it.

Neither Struts nor Tiles can help in this.

Antonio