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

mrowe picture mrowe · Sep 2, 2008 · Viewed 52.1k times · Source

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!

Answer

Martin McNulty picture Martin McNulty · Sep 4, 2008

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.