tags:

views:

2284

answers:

7

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!

A: 

It appears that a popular solution will not work in all containers... http://www.theserverside.com/discussions/thread.tss?thread_id=30190

bpapa
A: 

I would create a filter and bounce all requests to root back with forward responce. Hacks with creating home.do page looks ugly to me (One more thing to remember for you and investigate for someone who will support your code).

Georgy Bolyuba
+3  A: 

"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.

Damien B
+4  A: 

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.

Martin McNulty
This also will work if your app deployed on not-ROOT path. Redirect above will work if you strip the first "/".
Georgy Bolyuba
+1  A: 

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>
Craig Wohlfeil
A: 

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.

Nischal
A: 

create an empty file with name as your struts action, then set the name into "welcome-file-list" in your web.xml.

For eg: your struts action : "welcome.do" 1. create an empty file and named it: "welcome.do" then put into your web dir 2. put welcome.do in your "welcome-file-list" in web.xml file

Dickson