You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@turbine.apache.org by Jeff Jones <jj...@trap9.com> on 2002/08/06 20:59:25 UTC

problem redirecting to a RawScreen

Hi All.

I have a page with an 'export' button on it, and when they click it, I want to
return raw text (CSV, not html).

So after data collection and processing, I do this in the page's VelocityScreen:

        if(!StringUtils.isEmpty(data.getParameters().get("export"))) {
            doRedirect(data,"online,main,LeadExport.vm");
        }

And in the LeadExport screen (extends RawScreen), I do:

    public String getContentType(RunData data) {
        return "text/plain";
    }
    public void doOutput(RunData data) throws Exception {
        PrintWriter out = new PrintWriter(data.getResponse().getOutputStream());
        out.println("Hello!\n");
        out.flush();
    }

It seems to work (the out.flush() is required here for some reason), but then in
the turbine.log file I get:

[Tue Aug 06 11:50:59 PDT 2002] -- INFO -- Adding
jsessionid=9392068264B0C258A13595D174D58171
[Tue Aug 06 11:51:06 PDT 2002] -- ERROR -- Turbine.handleException: Screen
template 'online/main/LeadExport.vm' not found
[Tue Aug 06 11:51:06 PDT 2002] -- ERROR --
 Exception:  java.lang.Exception: Screen template 'online/main/LeadExport.vm'
not found
 Stack Trace follows:
 java.lang.Exception: Screen template 'online/main/LeadExport.vm' not found

<snip>

[Tue Aug 06 11:51:07 PDT 2002] -- ERROR -- getOutputStream() has already been
called for this response
 Exception:  java.lang.IllegalStateException: getOutputStream() has already been
called for this response
 Stack Trace follows:
 java.lang.IllegalStateException: getOutputStream() has already been called for
this response
 at org.apache.catalina.connector.ResponseBase.getWriter(ResponseBase.java:683)
 at
org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:127)
 at
org.apache.turbine.services.rundata.DefaultTurbineRunData.getOut(DefaultTurbineR
unData.java:1019)
<snip>


Any idea how to redirect to a screen that has no .vm, the "right way" (tm)?
I figure it's trying to finish processing the original template/layout, but I
want to halt all that and just return plaintext (CSV).

Using data.getOut() doesn't work - it renders all the layout with an error
message BELOW the "Hello!" text.

Thanks for any help!

Jeff Jones
Franchise.com



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: problem redirecting to a RawScreen

Posted by Steve Yokanovich <sy...@smythco.com>.
I am not sure if this will help you, but I had a similar situation and 
solved it this way:

I have a screen that lists content stored in an Oracle database as 
clobs. This screen template has a reference to a Turbine Action (no screen).

<a 
href="$link.setAction("GetContent").addPathInfo("id",$id).addPathInfo("version",$version)">$id
</a></td>


In the Turbine Action class (in this example called GetContent) I set 
the appropriate screen template and layout based on the type of content 
the user selected. Eg:

     (c is the content object retrieved from the database)
     String type = c.getType();
     if ( type.equalsIgnoreCase("application/pdf")) {
         // The content is raw and we need a Rawscreen to display it
         data.setLayout(null);
         data.getSession().setAttribute("CONTENT", c);
         data.setScreenTemplate("RawContent.vm");
     }
     if ( type.equalsIgnoreCase("text/html")) {
         // The content is text -- need a VelocityScreen to display it
         data.setScreenTemplate("Content.vm");
     }
     if (type.equalsIgnoreCase("text/xml")) {
        // The content is raw -- need a Rawscreen to display it
        data.setLayout(null);
        data.getSession().setAttribute("CONTENT", c);
        data.setScreenTemplate("PdfContent.vm");
     }
     if (type.equalsIgnoreCase("application/msword")) {
        // The content is raw and we need a Rawscreen to display it
        data.setLayout(null);
        data.getSession().setAttribute("CONTENT", c);
        data.setScreenTemplate("RawContent.vm");
     }

The result is the user may see the content displayed on the screen with 
a layout and (Content.vm) or download the content to a file (RawContent.vm).

I hope this helps. If not, feel free to email me and I can provide some 
other samples.


-- 
Steve Yokanovich
Smyth Companies, Inc.
Information Systems


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: problem redirecting to a RawScreen

Posted by Kris Woodbeck <kr...@webmotion.com>.
Jeff Jones wrote:

>Yep!  That's correct.
>
>My problem (I think) is that I can't figure out the right way to get Turbine to
>do an internal redirect without getting the writer before I do.  Sort of a
>'real' internal forward instead of a forward to another template.
>  
>
Sorry, I missed that part of your original post; try putting

data.declareDirectResponse();

before you output to the PrintWriter

>I think the VelocityScreen grabs the writer first, and when I doRedirect() to
>the RawScreen, the writer has already been gotten, thus the exception.
>  
>
Actually, from the stack trace, I'd say that it was grabbing the 
PrintWriter after you got the OutputStream, but, whatever!  That will be 
fixed if you call the above method...

>If I use getOut(), which is the template-friendly way to get a PrintWriter in
>turbine, Turbine finishes rendering the rest of the page after my
>RawScreen.doOutput() is done (wasteful).  I want to output raw, without any
>Layout or Screen.
>
>I would like to do something just like a jsp:forward, I think.
>
>I might have to just get the .vm to call the RawScreen directly from the form,
>and write a new business object to process the data instead of doing the
>processing in the screen and forwarding.
>
>I use a velocity template to generate the CSV, so I thought I could just change
>the content type of the response to "text/plain", and remove all Layouts.  It
>still seems to put an HTML header and footer on the document though.
>
>I love using Velocity to render different things (HTML, TeX, Email, CSV, SQL).
>Of course Torque has fun rendering Java and XML with it.
>
>Jeff
>
>----- Original Message -----
>From: "Kris Woodbeck" <kr...@webmotion.com>
>To: "Turbine Users List" <tu...@jakarta.apache.org>
>Sent: Tuesday, August 06, 2002 12:09 PM
>Subject: Re: problem redirecting to a RawScreen
>
>
>  
>
>>Hi Jeff,
>>
>>Check out the servlet javadoc:
>>
>>
>>    
>>
>http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/ServletResponse.h
>tml#getOutputStream()
>  
>
>>IllegalStateException is thrown for this if the getWriter() method for
>>that ServletResponse has been called already, which you can sort-of tell
>>from the exception (I'm guessing that any calls to data.getOut() also
>>just does a data.getResponse().getWriter() ).  I'm not sure why the
>>getWriter() is being called elsewhere, but that seems to be what's
>>happening, so try calling the getWriter() instead for your output (I
>>mean, you're only doing text output, so the PrintWriter would be more
>>appropriate in this case anyways).
>>
>>Kris
>>
>>Jeff Jones wrote:
>>
>>    
>>
>>>Hi All.
>>>
>>>I have a page with an 'export' button on it, and when they click it, I want
>>>      
>>>
>to
>  
>
>>>return raw text (CSV, not html).
>>>
>>>So after data collection and processing, I do this in the page's
>>>      
>>>
>VelocityScreen:
>  
>
>>>       if(!StringUtils.isEmpty(data.getParameters().get("export"))) {
>>>           doRedirect(data,"online,main,LeadExport.vm");
>>>       }
>>>
>>>And in the LeadExport screen (extends RawScreen), I do:
>>>
>>>   public String getContentType(RunData data) {
>>>       return "text/plain";
>>>   }
>>>   public void doOutput(RunData data) throws Exception {
>>>       PrintWriter out = new
>>>      
>>>
>PrintWriter(data.getResponse().getOutputStream());
>  
>
>>>       out.println("Hello!\n");
>>>       out.flush();
>>>   }
>>>
>>>It seems to work (the out.flush() is required here for some reason), but then
>>>      
>>>
>in
>  
>
>>>the turbine.log file I get:
>>>
>>>[Tue Aug 06 11:50:59 PDT 2002] -- INFO -- Adding
>>>jsessionid=9392068264B0C258A13595D174D58171
>>>[Tue Aug 06 11:51:06 PDT 2002] -- ERROR -- Turbine.handleException: Screen
>>>template 'online/main/LeadExport.vm' not found
>>>[Tue Aug 06 11:51:06 PDT 2002] -- ERROR --
>>>Exception:  java.lang.Exception: Screen template 'online/main/LeadExport.vm'
>>>not found
>>>Stack Trace follows:
>>>java.lang.Exception: Screen template 'online/main/LeadExport.vm' not found
>>>
>>><snip>
>>>
>>>[Tue Aug 06 11:51:07 PDT 2002] -- ERROR -- getOutputStream() has already been
>>>called for this response
>>>Exception:  java.lang.IllegalStateException: getOutputStream() has already
>>>      
>>>
>been
>  
>
>>>called for this response
>>>Stack Trace follows:
>>>java.lang.IllegalStateException: getOutputStream() has already been called
>>>      
>>>
>for
>  
>
>>>this response
>>>at
>>>      
>>>
>org.apache.catalina.connector.ResponseBase.getWriter(ResponseBase.java:683)
>  
>
>>>at
>>>      
>>>
>>org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:127)
>>    
>>
>>>at
>>>      
>>>
>>org.apache.turbine.services.rundata.DefaultTurbineRunData.getOut(DefaultTurbine
>>    
>>
>R
>  
>
>>>unData.java:1019)
>>><snip>
>>>
>>>
>>>Any idea how to redirect to a screen that has no .vm, the "right way" (tm)?
>>>I figure it's trying to finish processing the original template/layout, but I
>>>want to halt all that and just return plaintext (CSV).
>>>
>>>Using data.getOut() doesn't work - it renders all the layout with an error
>>>message BELOW the "Hello!" text.
>>>
>>>Thanks for any help!
>>>
>>>Jeff Jones
>>>Franchise.com
>>>
>>>
>>>
>>>--
>>>To unsubscribe, e-mail:
>>>      
>>>
><ma...@jakarta.apache.org>
>  
>
>>>For additional commands, e-mail:
>>>      
>>>
><ma...@jakarta.apache.org>
>  
>
>>>      
>>>
>>
>>
>>--
>>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>>For additional commands, e-mail: <ma...@jakarta.apache.org>
>>
>>    
>>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>  
>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: problem redirecting to a RawScreen

Posted by Jeff Jones <jj...@trap9.com>.
Yep!  That's correct.

My problem (I think) is that I can't figure out the right way to get Turbine to
do an internal redirect without getting the writer before I do.  Sort of a
'real' internal forward instead of a forward to another template.

I think the VelocityScreen grabs the writer first, and when I doRedirect() to
the RawScreen, the writer has already been gotten, thus the exception.

If I use getOut(), which is the template-friendly way to get a PrintWriter in
turbine, Turbine finishes rendering the rest of the page after my
RawScreen.doOutput() is done (wasteful).  I want to output raw, without any
Layout or Screen.

I would like to do something just like a jsp:forward, I think.

I might have to just get the .vm to call the RawScreen directly from the form,
and write a new business object to process the data instead of doing the
processing in the screen and forwarding.

I use a velocity template to generate the CSV, so I thought I could just change
the content type of the response to "text/plain", and remove all Layouts.  It
still seems to put an HTML header and footer on the document though.

I love using Velocity to render different things (HTML, TeX, Email, CSV, SQL).
Of course Torque has fun rendering Java and XML with it.

Jeff

----- Original Message -----
From: "Kris Woodbeck" <kr...@webmotion.com>
To: "Turbine Users List" <tu...@jakarta.apache.org>
Sent: Tuesday, August 06, 2002 12:09 PM
Subject: Re: problem redirecting to a RawScreen


> Hi Jeff,
>
> Check out the servlet javadoc:
>
>
http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/ServletResponse.h
tml#getOutputStream()
>
> IllegalStateException is thrown for this if the getWriter() method for
> that ServletResponse has been called already, which you can sort-of tell
> from the exception (I'm guessing that any calls to data.getOut() also
> just does a data.getResponse().getWriter() ).  I'm not sure why the
> getWriter() is being called elsewhere, but that seems to be what's
> happening, so try calling the getWriter() instead for your output (I
> mean, you're only doing text output, so the PrintWriter would be more
> appropriate in this case anyways).
>
> Kris
>
> Jeff Jones wrote:
>
> >Hi All.
> >
> >I have a page with an 'export' button on it, and when they click it, I want
to
> >return raw text (CSV, not html).
> >
> >So after data collection and processing, I do this in the page's
VelocityScreen:
> >
> >        if(!StringUtils.isEmpty(data.getParameters().get("export"))) {
> >            doRedirect(data,"online,main,LeadExport.vm");
> >        }
> >
> >And in the LeadExport screen (extends RawScreen), I do:
> >
> >    public String getContentType(RunData data) {
> >        return "text/plain";
> >    }
> >    public void doOutput(RunData data) throws Exception {
> >        PrintWriter out = new
PrintWriter(data.getResponse().getOutputStream());
> >        out.println("Hello!\n");
> >        out.flush();
> >    }
> >
> >It seems to work (the out.flush() is required here for some reason), but then
in
> >the turbine.log file I get:
> >
> >[Tue Aug 06 11:50:59 PDT 2002] -- INFO -- Adding
> >jsessionid=9392068264B0C258A13595D174D58171
> >[Tue Aug 06 11:51:06 PDT 2002] -- ERROR -- Turbine.handleException: Screen
> >template 'online/main/LeadExport.vm' not found
> >[Tue Aug 06 11:51:06 PDT 2002] -- ERROR --
> > Exception:  java.lang.Exception: Screen template 'online/main/LeadExport.vm'
> >not found
> > Stack Trace follows:
> > java.lang.Exception: Screen template 'online/main/LeadExport.vm' not found
> >
> ><snip>
> >
> >[Tue Aug 06 11:51:07 PDT 2002] -- ERROR -- getOutputStream() has already been
> >called for this response
> > Exception:  java.lang.IllegalStateException: getOutputStream() has already
been
> >called for this response
> > Stack Trace follows:
> > java.lang.IllegalStateException: getOutputStream() has already been called
for
> >this response
> > at
org.apache.catalina.connector.ResponseBase.getWriter(ResponseBase.java:683)
> > at
>
>org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:127)
> > at
>
>org.apache.turbine.services.rundata.DefaultTurbineRunData.getOut(DefaultTurbine
R
> >unData.java:1019)
> ><snip>
> >
> >
> >Any idea how to redirect to a screen that has no .vm, the "right way" (tm)?
> >I figure it's trying to finish processing the original template/layout, but I
> >want to halt all that and just return plaintext (CSV).
> >
> >Using data.getOut() doesn't work - it renders all the layout with an error
> >message BELOW the "Hello!" text.
> >
> >Thanks for any help!
> >
> >Jeff Jones
> >Franchise.com
> >
> >
> >
> >--
> >To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> >For additional commands, e-mail:
<ma...@jakarta.apache.org>
> >
> >
>
>
>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: problem redirecting to a RawScreen

Posted by Kris Woodbeck <kr...@webmotion.com>.
Hi Jeff,

Check out the servlet javadoc:

http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/ServletResponse.html#getOutputStream()

IllegalStateException is thrown for this if the getWriter() method for 
that ServletResponse has been called already, which you can sort-of tell 
from the exception (I'm guessing that any calls to data.getOut() also 
just does a data.getResponse().getWriter() ).  I'm not sure why the 
getWriter() is being called elsewhere, but that seems to be what's 
happening, so try calling the getWriter() instead for your output (I 
mean, you're only doing text output, so the PrintWriter would be more 
appropriate in this case anyways).

Kris

Jeff Jones wrote:

>Hi All.
>
>I have a page with an 'export' button on it, and when they click it, I want to
>return raw text (CSV, not html).
>
>So after data collection and processing, I do this in the page's VelocityScreen:
>
>        if(!StringUtils.isEmpty(data.getParameters().get("export"))) {
>            doRedirect(data,"online,main,LeadExport.vm");
>        }
>
>And in the LeadExport screen (extends RawScreen), I do:
>
>    public String getContentType(RunData data) {
>        return "text/plain";
>    }
>    public void doOutput(RunData data) throws Exception {
>        PrintWriter out = new PrintWriter(data.getResponse().getOutputStream());
>        out.println("Hello!\n");
>        out.flush();
>    }
>
>It seems to work (the out.flush() is required here for some reason), but then in
>the turbine.log file I get:
>
>[Tue Aug 06 11:50:59 PDT 2002] -- INFO -- Adding
>jsessionid=9392068264B0C258A13595D174D58171
>[Tue Aug 06 11:51:06 PDT 2002] -- ERROR -- Turbine.handleException: Screen
>template 'online/main/LeadExport.vm' not found
>[Tue Aug 06 11:51:06 PDT 2002] -- ERROR --
> Exception:  java.lang.Exception: Screen template 'online/main/LeadExport.vm'
>not found
> Stack Trace follows:
> java.lang.Exception: Screen template 'online/main/LeadExport.vm' not found
>
><snip>
>
>[Tue Aug 06 11:51:07 PDT 2002] -- ERROR -- getOutputStream() has already been
>called for this response
> Exception:  java.lang.IllegalStateException: getOutputStream() has already been
>called for this response
> Stack Trace follows:
> java.lang.IllegalStateException: getOutputStream() has already been called for
>this response
> at org.apache.catalina.connector.ResponseBase.getWriter(ResponseBase.java:683)
> at
>org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:127)
> at
>org.apache.turbine.services.rundata.DefaultTurbineRunData.getOut(DefaultTurbineR
>unData.java:1019)
><snip>
>
>
>Any idea how to redirect to a screen that has no .vm, the "right way" (tm)?
>I figure it's trying to finish processing the original template/layout, but I
>want to halt all that and just return plaintext (CSV).
>
>Using data.getOut() doesn't work - it renders all the layout with an error
>message BELOW the "Hello!" text.
>
>Thanks for any help!
>
>Jeff Jones
>Franchise.com
>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>  
>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>