You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by a....@verizon.net on 2003/05/26 19:57:45 UTC

Follow-up on Tiles and "Cannot redirect after ..." Exception

Ted:

Thank you for your suggestions.  They are valuable, but require 
changes of the current use of a tag library.  I will certainly 
keep them in mind.

I've found a solution, even though it's not entirely satisfying 
and I thought it would be worth to describe the 3 different 
approaches that I took with Struts.  Only the last of them 
would be avoiding the exception.  Maybe it is helpful for 
someone else that stumbles over the same issue and/or someone 
could get an idea on how to make the other two cases work.

(1) Throws "Cannot redirect after ..." exception

Layout layout.jsp:
<%@ taglib uri='/WEB-INF/struts-tiles.tld' prefix='tiles' %>
<tiles:insert attribute="access"/>
<html>
<body>
<tiles:insert attribute="body"/>
</body>
</html>

Page page.jsp:
<%@ taglib uri='/WEB-INF/struts-tiles.tld' prefix='tiles' %>
<tiles:insert template='/layout.jsp'>
  <tiles:put name='access' content='/check.jsp' />
  <tiles:put name='body' content='/body.html' />
</tiles:insert>

Component check.jsp:
<%@ taglib uri="/WEB-INF/access.tld" prefix="access" %>
<access:check/>


(2) Throws "Cannot redirect after ..." exception

Layout layout.jsp:
<%@ taglib uri="/WEB-INF/access.tld" prefix="access" %>
<access:check/>
<%@ taglib uri='/WEB-INF/struts-tiles.tld' prefix='tiles' %>
<html>
<body>
<tiles:insert attribute="body"/>
</body>
</html>

Page page.jsp:
<%@ taglib uri='/WEB-INF/struts-tiles.tld' prefix='tiles' %>
<tiles:insert template='/layout.jsp'>
  <tiles:put name='body' content='/body.html' />
</tiles:insert>


(3) This one works

Layout layout.jsp:
<%@ taglib uri='/WEB-INF/struts-tiles.tld' prefix='tiles' %>
<html>
<body>
<tiles:insert attribute="body"/>
</body>
</html>

Page page.jsp:
<%@ taglib uri="/WEB-INF/access.tld" prefix="access" %>
<access:check/>
<%@ taglib uri='/WEB-INF/struts-tiles.tld' prefix='tiles' %>
<tiles:insert template='/layout.jsp'>
  <tiles:put name='body' content='/body.html' />
</tiles:insert>

Maybe my confusion just stems from the nomenclature.  Is 
page.jsp a tile?  Or is access.jsp in (1) the first tile?
What a about layout.jsp?

Thanks for the help again.  Oh, yes - this is Struts 1.1.

Andreas


> It has to be at the very beginning of the very first tile. Once the page 
> starts to render, and the response has begun, you can no longer 
> redirect. The tiles are rendered one by one, so by the time you get to a 
> later tile, the response has already begun.
> 
> This is one reason why people try to handle all the control flow from an 
> Action, before a page is ever called.
> 
> If you are using Struts 1.1, consider making use of a RequestProcessor 
> that overrides ProcessRoles.
> 
> -Ted.
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Follow-up on Tiles and "Cannot redirect after ..." Exception

Posted by Cedric Dumoulin <ce...@apache.org>.

a.kemkes@verizon.net wrote:

>Ted:
>
>Thank you for your suggestions.  They are valuable, but require 
>changes of the current use of a tag library.  I will certainly 
>keep them in mind.
>
>I've found a solution, even though it's not entirely satisfying 
>and I thought it would be worth to describe the 3 different 
>approaches that I took with Struts.  Only the last of them 
>would be avoiding the exception.  Maybe it is helpful for 
>someone else that stumbles over the same issue and/or someone 
>could get an idea on how to make the other two cases work.
>
>(1) Throws "Cannot redirect after ..." exception
>
>Layout layout.jsp:
><%@ taglib uri='/WEB-INF/struts-tiles.tld' prefix='tiles' %>
><tiles:insert attribute="access"/>
><html>
><body>
><tiles:insert attribute="body"/>
></body>
></html>
>
>Page page.jsp:
><%@ taglib uri='/WEB-INF/struts-tiles.tld' prefix='tiles' %>
><tiles:insert template='/layout.jsp'>
>  <tiles:put name='access' content='/check.jsp' />
>  <tiles:put name='body' content='/body.html' />
></tiles:insert>
>
>Component check.jsp:
><%@ taglib uri="/WEB-INF/access.tld" prefix="access" %>
><access:check/>
>
>
>(2) Throws "Cannot redirect after ..." exception
>
>Layout layout.jsp:
><%@ taglib uri="/WEB-INF/access.tld" prefix="access" %>
><access:check/>
><%@ taglib uri='/WEB-INF/struts-tiles.tld' prefix='tiles' %>
><html>
><body>
><tiles:insert attribute="body"/>
></body>
></html>
>
>Page page.jsp:
><%@ taglib uri='/WEB-INF/struts-tiles.tld' prefix='tiles' %>
><tiles:insert template='/layout.jsp'>
>  <tiles:put name='body' content='/body.html' />
></tiles:insert>
>
>
>(3) This one works
>
>Layout layout.jsp:
><%@ taglib uri='/WEB-INF/struts-tiles.tld' prefix='tiles' %>
><html>
><body>
><tiles:insert attribute="body"/>
></body>
></html>
>
>Page page.jsp:
><%@ taglib uri="/WEB-INF/access.tld" prefix="access" %>
><access:check/>
><%@ taglib uri='/WEB-INF/struts-tiles.tld' prefix='tiles' %>
><tiles:insert template='/layout.jsp'>
>  <tiles:put name='body' content='/body.html' />
></tiles:insert>
>
>Maybe my confusion just stems from the nomenclature.  Is 
>page.jsp a tile?  
>
  Yes, it is a tile.

>Or is access.jsp in (1) the first tile?
>
  In fact, every jsp can be considered as a tile, even the layout ...
The first jsp encountered is generally the layout.
  The "Cannot redirect after ..." exception is thrown when you try to do
a redirect after something is written in the output. The first write to
the output usually happen in the layout tile, which is generally the
first tile included. A simple <html> tag or a leading space can result
in a write in the output.

  If you want to use Tiles and need access checking, you should put the
access check and redirect at the very beginning of the page to protect.
If you want to show/hide a part of a page according to access right, you
can use the role attribute of a tile to insert it conditionally.

   Cedric

>What a about layout.jsp?
>
>Thanks for the help again.  Oh, yes - this is Struts 1.1.
>
>Andreas
>
>
>  
>
>>It has to be at the very beginning of the very first tile. Once the page 
>>starts to render, and the response has begun, you can no longer 
>>redirect. The tiles are rendered one by one, so by the time you get to a 
>>later tile, the response has already begun.
>>
>>This is one reason why people try to handle all the control flow from an 
>>Action, before a page is ever called.
>>
>>If you are using Struts 1.1, consider making use of a RequestProcessor 
>>that overrides ProcessRoles.
>>
>>-Ted.
>>
>>    
>>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>  
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org