response.sendRedirect not working in Struts2 tiles

2019-03-04 09:50发布

问题:

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

回答1:

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.