2

Related to: How can I set the welcome page to a struts action?

I'm trying to redirect my welcome page index.jsp to an action (Struts2). None of options in related post worked for me:

type Status report
message /malelo/indexRedir
description The requested resource (/malelo/indexRedir) is not available.

I'm follow this FLOW structure:

http://localhost:8580/malelo/ SEND TO WELCOME PAGE
     --->  (root)index.jsp SEND USER TO (by redirect, I need forward)
          --->  (indexRedir) IndexAction.java SEND USER TO
               ---> (WEB-INF/content) index-login.jsp

This worked, but not too elegant (index.jsp):

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

I'm trying to do this (index.jsp):

<jsp:forward page="indexRedir" />

I'm using only Annotation, no XML files configuration, please.

@Action (value = "indexRedir", results = 
    { @Result (location = "index-login.jsp", name = "ok") } ) 

EDIT

Just to clarify, this is the http header sent to browser by a response.sendRedirect :

HTTP/1.1 302 Found
Date: Tue, 27 May 2014 16:15:12 GMT
Location: http://cmabreu.com.br/basico/
Content-Length: 0
Connection: close
Content-Type: text/plain

This location (http://cmabreu.com.br/basico/) points to a index.jsp with:

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

And show the content of a JSP page pointed by indexRedir action. Google Webmasters Tools and some bots don't follow pages like that.

See more here: http://en.wikipedia.org/wiki/HTTP_302

Community
  • 1
  • 1
Magno C
  • 1,922
  • 4
  • 28
  • 53
  • Do *any* actions work? Also note that a forward isn't the same as a redirect; if you want to *forward* (which I wouldn't, since your URL wouldn't change) then you'd need to configure your server to process forwards through the interceptors, e.g., for Tomcat, http://stackoverflow.com/a/16123779/438992. – Dave Newton May 27 '14 at 13:58
  • Yes. My system is working fine with 50 actions. I know forward is not same redirect. I want to change just because I know that. My picture is confusing people, sorry. It's not folders, is flow. – Magno C May 27 '14 at 14:14
  • Please don't downvote without tell me WHY and without understand the question! Please DON'T delete answers! – Magno C May 27 '14 at 14:23
  • Welcome to the world of one of the S2 tag's users; I apologize for his behavior. Can you access the `indexRedir` action outside of the forward? – Dave Newton May 27 '14 at 14:27
  • Yeap! Using `response.sendRedirect("indexRedir")` in welcome page `index.jsp` works fine. By the way, your solution in other post (the link you give me above) worked. Please put it in a answer. Now I can `` from index.jsp! – Magno C May 27 '14 at 14:30
  • DaveNewton is talking about an answer that was deleted by it's owner. I'm asking to not delete answers right above. – Magno C May 28 '14 at 11:52

1 Answers1

4

In order to forward to an action you must configure filters to run on forwards; normally they don't, although this is container-dependent. For example, under Tomcat:

<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher> 
  <dispatcher>FORWARD</dispatcher>
  <dispatcher>INCLUDE</dispatcher> <!-- If you want includes as well -->
</filter-mapping>

Personally, I don't find the redirect particularly inelegant, and it's pretty standard.

The other benefit is that you shuttle people into the action-based app; with the forward your URL doesn't change. In general you'd want to disallow JSP-based URLs.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I think it's not too good do do just in the welcome page. The Google Webmasters Tools understood a `Moved Temporarily` and I don't like that. Some robots may have troubles too. – Magno C May 27 '14 at 14:46