Platinum Partner
java,eclipse,css,how-to,annotations,struts,web development

Write A Struts 2 Action Class In 5 Minutes Using Annotations

This tutorial is a continuation of Setup web application using spring Struts 2 and Maven with Eclipse 3.3. The project structure is as shown in the below screenshot.

[img_assist|nid=4027|title=|desc=|link=none|align=none|width=252|height=339]

There are two ways to create a Struts 2 Action class. The first is to configure in struts.xml. We will follow the second way to configure through annotation. Create StartAction.java under the package com.thea.airlines [under the folder src/main/java] as shown in the screenshot below.

[img_assist|nid=4028|title=|desc=|link=none|align=none|width=686|height=359]

Here is a brief explanation of what the Struts Action class does.

  • We end the class name with Action to inform struts that this Java file should be considered as a Struts 2 Action.
  • We provide the implementation of the method public String execute() to fulfil the contract for a struts 2 action [which is to implement com.opensymphony.xwork2.Action].
  • Returns "SUCCESS" which dispatches second.jsp [configured using annotation]

Let us create second.jsp under src/main/webapp as in the screenshot below. It is very simple and has one line of text saying "The content of second.jsp".

[img_assist|nid=4029|title=|desc=|link=none|align=none|width=518|height=248]

Next modify the web.xml to include a filter as below(so that our struts requests are processed). We have mentioned the value of actionPackages as com. This instructs struts 2 to scan all packages [and classes in them] under com package and look for struts action classes fulfilling the contract for struts action class.

[img_assist|nid=4030|title=|desc=|link=none|align=none|width=641|height=388]

Now open command prompt and change to the Maven project root as in the screenshot below. Execute the command mvn package

[img_assist|nid=4031|title=|desc=|link=none|align=none|width=466|height=73]

We can use cargo plugin related to Maven to deploy it to the desired location. But for now let us copy the airlines-.1.war created [as a result of mvn package command] under the folder E:\theacodefactory\code\airelinesweb\airlines\target to Tomcat webapps directory. I have my Tomcat 6 installed in E:\theacodefactory\tomcat6. So I have copied airlines-.1.war under E:\theacodefactory\tomcat6\webapps as in the screenshot below.

[img_assist|nid=4032|title=|desc=|link=none|align=none|width=220|height=142]

Then start the Tomcat by executing catalina run command as in the screenshot below.

[img_assist|nid=4033|title=|desc=|link=none|align=none|width=351|height=34]

Once Tomcat has started invoke the Struts Action and second.jsp is displayed as in the screenshot below.
Logic involved in constructing the URL to invoke the action:

  • The URL part after the context root of the web application is as follows. Include the folder names after the actionPackages parameter mentioned in web.xml earlier.
  • The action class name is considered excluding the Action [in the end] and the first letter is converted into a lower case.
  • .action is appended to the URL.
[img_assist|nid=4034|title=|desc=|link=none|align=none|width=640|height=274]

View next section of this tutorial -- How to auto deploy web application in Tomcat using Maven Cargo Plugin
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}