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.

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.

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".

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.

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

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.

Then start the Tomcat by executing catalina run command as in the screenshot below.
![]()
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.

View next section of this tutorial -- How to auto deploy web application in Tomcat using Maven Cargo Plugin
- Login or register to post comments
- 6443 reads
- Flag as offensive
- Email this Story
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)







Comments
George Jempty replied on Mon, 2008/07/14 - 6:19pm
Steven replied on Mon, 2008/07/14 - 7:30pm
Greg replied on Mon, 2008/07/14 - 8:29pm
It's still a forward-based heap of junk, like JSF. Big deal.
Piero Sartini replied on Tue, 2008/07/15 - 7:41am
This demonstration is not very impressive. BUT Struts2 _is_ very powerful. If you bring in the convention plugin and the maven archetypes you are set up in no time and need no configuration or even annotations... They learned a lot of lessons from RoR.
With s2 you get a really impressive framework that allows to develop fast and easy. But it is extensible in many ways, so if you need more complex things you are not lost. IMO, this flexibility is one of the key features of Struts2 which is unmatched in the market. DI Framework, Frontend rendering, Template system, EL, action language (you can write your actions in groovy or JRuby if you want to)... you have the choice. Some people don't need this choice or want something "easy" that says them how to do things. If this is your case, S2 is not for you :)
But if you search for a flexible and future safe framework that can grow with your needs and emerging technologies, then S2 gives you everything you could dream of.