response.sendRedirect not working in Struts2 tiles
Asked Answered
C

1

0

I am developing a struts2 project with tiles in which I want to use the keyword for redirecting from one jsp page to other page as,

   <%
    response.sendRedirect("search");
    %>

In normal jsp pages the code is working as.

response.sendRedirect("search.jsp");

but when I use with tiles, its not working.

when I am running the page directly its redirecting, but when I call this some other page its not redirecting. I tried the code with blank html page without any other codings, still its not working.

"search" is the name of the action in struts.xml page. Is there any extra attribute I need to add in response.sendRedirect ?

Currently I am using

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=search">

for doing my job. Whether it will create any problem in any aspect?

I checked it with conditions for redirecting to multiple places, its working.

 <%    
    int k=0;
    if(k==1){
    %>

    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=search">
    <%
    }
    else 
    {
    %>

    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=guestprofile">
    <% 
    } 
    %>

As I understood from answer, tried like this

response.sendRedirect("viewuniqueRedirect"); 

in page and

<action name="viewuniqueRedirect" > <result type="chain">viewunique</result> </action> 

in struts.xml, but not working

Colby answered 26/3, 2012 at 5:20 Comment(7)
I saw a similar question, but solution is not working hereColby
You can use result type redirectAction and call search action and pass required parameters to the action.Surtout
@MohanaRaoSV, Sorry I dont get the methode, please give an exampleColby
@SarinJacobSunny: this problem you are getting only when using tiles or with simple S2 application? Sorry a bit lazy to read details ;)Petcock
It might work but is it correct design?Surtout
@UmeshAwasthi, with tiles only this problem comes. If I use it in normal pages its not creating problems.Colby
@MohanaRaoSV, the last added meta method is an example of code which I tried as another method temporary to meet my requirement. But actually what I need is response.sendRedirect itself. Because I am afraid that my crooked method may lead to big troubles later.Colby
B
2

You are right to suspect your meta tag method as being very ugly.

With struts2 you are returning a tiles result type. This is either defined in your struts.xml or the action is annotated to produce this result.

Your action which you want to redirect should NOT be returning a tiles result type but a redirect/redirectAction type. For one of these results see here: Action redirect in struts.xml

Your action should do all the required processing (if any) and tiles should compose the view. If you really intend to redirect, it is a waste to try to compose any view what so ever.

The question which you pointed to (here) probably still applies in this context. That is you can not redirect if you have written content and you may try to redirect and return before writing any further content but tiles is often composing from several views... and will continue to write to the HTTP response and thus likely null the redirect. If you only have a single jsp composing the view, then I don't know. But once again I would not think to invoke tiles at all.

Boo answered 28/3, 2012 at 3:50 Comment(6)
search and guestprofile are valid mappings in struts.xml and tiles.xml, Both I am using in the same project with hyperlinks.Colby
Now I use meta as a temporary method for making the functionality working, Actually what I need is Response.sendRedirect itself. Since it is not working I had to search for a temporary method.Colby
If I load the page in which I use Response.sendRedirect, its working, but if that page is coming in a flow ( as in sequence from other pages). Its not working.Colby
Unless I'm missing something, this is all covered in the answer including why it is not working as part of a "flow".Boo
I tried like this response.sendRedirect("viewuniqueRedirect"); in page and <action name="viewuniqueRedirect" > <result type="chain">viewunique</result> </action> in struts.xml, but not workingColby
I have no idea why you insist on using a scriplet... but if you absolutely insist on this... then the result type is dispatcher. <action name="my-redirect"><result type="dispatcher>/WEB-INF/myFolder/myJSPWithRedirect.jsp</result></action>, then be sure to only have the redirect scriptlet and not to write anything before or after and to return. But considering what you've been shown this is isn't very good. If you're going to do things like this it begs why you are using a MVC framework at all.Boo

© 2022 - 2024 — McMap. All rights reserved.