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

Sarin Jacob Sunny
  • 2,138
  • 3
  • 29
  • 61

1 Answers1

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.

Community
  • 1
  • 1
Quaternion
  • 10,380
  • 6
  • 51
  • 102
  • search and guestprofile are valid mappings in struts.xml and tiles.xml, Both I am using in the same project with hyperlinks. – Sarin Jacob Sunny Mar 28 '12 at 04:16
  • 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. – Sarin Jacob Sunny Mar 28 '12 at 04:19
  • 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. – Sarin Jacob Sunny Mar 28 '12 at 04:20
  • Unless I'm missing something, this is all covered in the answer including why it is not working as part of a "flow". – Quaternion Mar 28 '12 at 06:26
  • I tried like this response.sendRedirect("viewuniqueRedirect"); in page and viewunique in struts.xml, but not working – Sarin Jacob Sunny Mar 29 '12 at 07:27
  • I have no idea why you insist on using a scriplet... but if you absolutely insist on this... then the result type is dispatcher. – Quaternion Mar 29 '12 at 07:38