1 Jun 2015

Action Extension in Struts2

In struts2 all action class has .action extension by default. Consider an example as shown below:
<struts>
  <package name="default" namespace="/" extends="struts-default">
     <action name="contact">
        <result>/contact.jsp</result>
     </action>
  </package>
</struts>
For the preceding configuration we can access the contact action class as
/contact.action
Struts2 allows to configure the action extension easily just by declaring a constant struts.action.extension inside struts.xml. You can set different action extensions by providing the value of this constant as a comma separated list e.g: action, do or no extension at all. The blank extension allows you to use pure action names.

Let us see how to set action extension with the following examples.

1. html extension

Set the value of action extension as html as shown.
<struts>
   <constant name="struts.action.extension" value="html"/>
   <package name="default" namespace="/" extends="struts-default">
      <action name="contact">
         <result>/contact.jsp</result>
      </action>
   </package>
</struts>
For the preceding configuration we can access the contact action class as
/contact.html

2. no extension

Set the value of action extension as empty string as shown.
<struts>
   <constant name="struts.action.extension" value=","/>
   <package name="default" namespace="/" extends="struts-default">
      <action name="contact">
         <result>/contact.jsp</result>
      </action>
   </package>
</struts>
For the preceding configuration we can access the contact action class purely with its name as
/contact

3. action or no extension

Set the value of action extension as ,action as shown.
<struts>
   <constant name="struts.action.extension" value=",action"/>
   <package name="default" namespace="/" extends="struts-default">
      <action name="contact">
         <result>/contact.jsp</result>
      </action>
   </package>
</struts>
For the preceding configuration we can access the contact action class with action extension or purely with its name as
/contact.action
/contact





Popular Posts

Write to Us
Name
Email
Message