Saturday, January 24, 2015

Dealing with welcome files in struts 2

Usually we want our users to direct to index.jsp file, or whatever the name of the homepage file, when they key in just the domain name of ours in their browser. This can be simply done by specifying a welcome file list in the deployment descriptor of our java web application. But things get more complicated when you are using struts tags on one of the welcome files. Assume your struts filter is specified by extension mapping as follows.

Now only URIs that end with “*.html” will go through the struts’ servlet filter. Now assume your index file is a jsp called, well index.jsp. So your welcome file list should be something like this.

This configuration works as long as you don't have any struts tags in the index.jsp file. But whenever you put struts tags, the index.jsp file must be accessed by going through the struts servlet filter. Otherwise, following error will pop out.

org.apache.jasper.JasperException: The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter.
So we need to do following changes to make our welcome files go through struts.
  1. Change the welcome files list to the following <welcome-file>index.html</welcome-file>
  2. Create an empty file called html in the web root
  3. Specify an action for index.jsp in the struts.xml file


No comments:

Post a Comment