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

53,063

Solution 1

Personally, I'd keep the same setup you have now, but change the redirect for a forward. That avoids sending a header back to the client and having them make another request.

So, in particular, I'd replace the

<% 
  response.sendRedirect("/myproject/MyAction.action");
%>

in index.jsp with

<jsp:forward page="/MyAction.action" />

The other effect of this change is that the user won't see the URL in the address bar change from "http://server/myproject" to "http://server/myproject/index.jsp", as the forward happens internally on the server.

Solution 2

This is a pretty old thread but the topic discussed, i think, is still relevant. I use a struts tag - s:action to achieve this. I created an index.jsp in which i wrote this...

<s:action name="loadHomePage" namespace="/load" executeResult="true" />

Solution 3

As of the 2.4 version of the Servlet specification you are allowed to have a servlet in the welcome file list. Note that this may not be a URL (such as /myproject/MyAction.action). It must be a named servlet and you cannot pass a query string to the servlet. Your controller servlet would need to have a default action.

<servlet>
  <servlet-name>MyController</servlet-name>
  <servlet-class>com.example.MyControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>MyController</servlet-name>
  <url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
  <welcome-file>MyController</welcome-file>
</welcome-file-list>

Solution 4

"Surely there's a better way!"

There isn't. Servlet specifications (Java Servlet Specification 2.4, "SRV.9.10 Welcome Files" for instance) state:

The purpose of this mechanism is to allow the deployer to specify an ordered list of partial URIs for the container to use for appending to URIs when there is a request for a URI that corresponds to a directory entry in the WAR not mapped to a Web component.

You can't map Struts on '/', because Struts kind of require to work with a file extension. So you're left to use an implicitely mapped component, such as a JSP or a static file. All the other solutions are just hacks. So keep your solution, it's perfectly readable and maintainable, don't bother looking further.

Solution 5

Something that I do is to put an empty file of the same name as your struts action and trick the container to call the struts action.

Ex. If your struts action is welcome.do, create an empty file named welcome.do. That should trick the container to call the Struts action.

Share:
53,063
user391301
Author by

user391301

Updated on May 31, 2020

Comments

  • user391301
    user391301 almost 4 years

    I have a struts-based webapp, and I would like the default "welcome" page to be an action. The only solutions I have found to this seem to be variations on making the welcome page a JSP that contains a redirect to the action. For example, in web.xml:

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    

    and in index.jsp:

    <% 
      response.sendRedirect("/myproject/MyAction.action");
    %> 
    

    Surely there's a better way!

  • jon
    jon over 15 years
    This also will work if your app deployed on not-ROOT path. Redirect above will work if you strip the first "/".
  • Haozhun
    Haozhun over 12 years
    forward doesn't work for me while sendRedirect does work. Got '404 Not Found'.
  • cprcrack
    cprcrack over 12 years
    Apparently this won't work if you don't specify the full URL (including the domain etc), but @mrowe 's approach worked for us!
  • Miles010
    Miles010 about 12 years
    This solution is framework agnostic. You can use it with anything, you just have to figure out how to specify a default action with your chosen framework.
  • guymac
    guymac over 9 years
    This is incorrect. You can map particular extensions to the default (static content) servlet and anything/everything else regardless of extension to the Struts action servlet.
  • guymac
    guymac over 9 years
    Worked for me; thanks for the tip! Previously I had used the following: <%@ taglib prefix="struts" uri="http://struts.apache.org/tags-logic" %><struts:redirect forward="login.action" />
  • Damien B
    Damien B over 9 years
    If I remember correctly (that was 6 years ago after all), in Struts 1 you are required to have your action URLs having a specific extension, because this is how the mapping is performed (URL -> strip extension -> resolve module -> Action). Of course that doesn't apply to WebWork aka "Struts 2".
  • Damien B
    Damien B over 9 years
    Anyway, maybe that was a WebWork question and not a Struts question after all :-)
  • silver
    silver over 9 years
    Same, also got 404 with <jsp:forward>.
  • silver
    silver over 9 years
    This doesn't work. Probably because the <welcome-file-list> looks for a physical file hence "welcome FILE list". That's why putting an empty file with the same name as the action name in the app root (WebContent) folder works.
  • silver
    silver over 9 years
    This doesn't work. Probably because the <welcome-file-list> looks for a physical file hence "welcome FILE list". That's why putting an empty file with the same name as the action name in the app root (WebContent) folder works.
  • Alireza Fattahi
    Alireza Fattahi about 8 years
    The best answer as you don't hard code the struts.action.extension in your jsp. The struts.action.extension may be changed to something other than action and then you need to change the request.redirect url in this jsp too.
  • VeenarM
    VeenarM almost 8 years
    This is a struts 2 only way.