Map struts .action/.do to the URL root

To map the URL root to certain welcome file, we normally just specify the welcome-file in the web.xml like this


index.html
index.jsp



What this means is, if in a directory without specifying any file name, the web application will first look for index.html and then index.jsp.

Now add struts framework into the picture, default struts extension for 1 and 2 is *.do and *.action respectively.

There are 2 ways of making our application pointing to the special struts action.

#1: Meta redirect
You can keep index.html or index.jsp as welcome-file and what you need to do is put a meta refresh in these index files

Struts 1
meta equiv="refresh" content="0;URL=/homepage.do"


Struts 2
meta equiv="refresh" content="0;URL=/homepage.action"


But the downside of this is - whatever action name you use would appear on the browser. (i.e. www.mydomain.com always appear as www.mydomain.com/homepage.action even user types in www.mydomain.com)

#2: Change welcome-file and add dummy place holder file

The second way (or better way) of fixing the struts action mapping to the URL root is to update the welcome-file list.

Struts 1

homepage.do



Struts 2

homepage.action



And then in the WebContent Root, add a dummy file (e.g. in Struts 2 you need an empty file homepage.action)*This step is important, without the empty dummy file, it won't work, at least in Tomcat*

Now when you hit http://www.mydomain.com your application will hit the homepage.action directly without adding the homepage.action after your url.

Comments (0)

Post a Comment