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