Get Rid of http://x.action URL

Change Struts URL extension suffix .do .action

If you notice, many web applications deployed over the web have .do or .action at the end of URL’s. One can deliberately use these kinds of url’s.

But usually it is the default behavior of the framework/tool being used then a deliberate attempt by the developer.

Struts based web applications are the widest of them to have .do or .action suffixes with URL’s.

That is because of the numerous tutorials and books have adopted .do as a convention for Struts 1 based applications. The same applied to Struts 2. The good news is that one can easily change or get rid of extension suffixes in Struts 1 and Struts2. A question then arises as to how come the suffixed come into the URL’s.

Struts 1

If we look at a form being written using Struts 1 tag libraries, it doesn’t have any .do or .action. Here is a sample form in a JSP page of a Struts based application.


        
        
    

As we can see that we are using the form tag of the struts-html tag library. So let us dig more into that tag by having a look at the struts-taglib.jar. Inside this library, look for org.apache.struts.taglib.html.FormTag.java

In the FormTag.java we can see the code for the attribute action:
After analyzing the code for FormTag.java, the URL pattern being used for mapping ActionServlet, is being used as the URL suffix by the custom tags.
In the web.xml we generally have something like this:


        actionServlet
        *.do
    

TO make all the URL to have suffix of .who instead of .do, change the mapping to:


        actionServlet
        *.who
    

Struts 2

But if we look at the web.xml for struts 2 based application, we have


        struts2D
        org.apache.struts2.dispatcher.FilterDispatcher
    
    
        struts2D
        /*
    

So from where .action does come from?
In case of Struts 2, the extension suffix comes from the property struts.action.extension.

The value for this property can be changed to any suitable value by setting this property in the struts.xml
An example for changing the URL's in case of Struts 2 to .do is given below:


 
      
 
     
       
         login.jsp
       
     
 
   


With the above structure in the struts.xml, all the URL's will have .do as the extension suffix. Similarly, to get rid of the extension suffix at all, use the following xml code in struts.xml


 
      
 
     
       
          login.jsp
       
     
 
   

Hope this tutorial will help people in customizing their applications while using the Struts framework.

No comments: