Basic Struts2 'Hello World' Application

A basic struts2 application can be made in 7 simple steps. There are umpteen struts2 tutorials on the web so I've refrained from going in a lot of detail about the theoretical information, this article will help you implement the most basic struts2 application in eclipse. I've tried to explain all the steps adequately, providing screenshots and code hints wherever required. It is possible that you may be familiar with a few of the initial steps, but for the benefit of those that maybe the slightest bit confused I've not edited them out. I hope that you find this article to be of use. Please do let me know if you face any issues while implementing this, I’d love to help you out, additionally, if you believe that some part of it could be implemented in a better manner I’d love to hear of it. Hope this helps.

Step 1 - Creating a web Project
Right click on the project explorer and click on New. Select 'Dynamic Web Project' from the side-slide menu that appears.

Enter the project name 'Struts2Project' and click on finish.

Step 2 - Add the required libraries
The following jars should be added to the lib folder in WEB-INF.

1. commons-io-2.0.1.jar --------->

2. commons-lang3-3.1.jar -------->

3. commons-logging-1.1.1.jar ----->

4. -2.3.19.jar ----------> 5. ognl-3.0.5.jar -----------------> 6. struts2-core-2.3.4.jar


7. xwork-core-2.3.4.jar ---------->

While the exact use of some of these jars may not be evident at first sight, there are many interlinked dependencies which makes it important to include each of the 7 jars, explaining the dependencies would have been out of scope of the post so I skipped the part.

Step 3 - Edit Web.xml
You would find the web.xml in WEB-INF folder in Web Content folder. Filter definition and filter mapping needs to be given in the file. Paste the following code in web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5"> <display-name>Struts2Project</display-name> <filter> <filter-name>struts2</filter-name> <filterclass></ filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>Index.jsp</welcome-file> </welcome-file-list> </web-app>

Step 4 - Create Index.jsp
Copy and paste the following code into the jsp. Note the second line of the code; you would need to include the tag library which you are going to use in the code. Struts2 tags are slightly different as opposed to your tradational HTML codes, hence the use of <s:submit> instead
of the good old <input type="submit">. You can research this further on the web. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Home</title> </head> <body> <s:form action="Login.action"> <h2>Username : <s:textfield id="username" name = "username"></s:textfield></h2> <s:submit value="Submit"></s:submit> </s:form> </body> </html>

The action attribute mentioned with the form is the keyword which is going to help the application redirect to your action class.

Step 5 - Configuring struts.xml
Create a new xml file in the source folder of the project.

Paste the following code into the new file. Once you click on submit on the Index.jsp, struts.xml sends you over to the mentioned class to the given method. If you do not give a method name in the action tag in struts.xml, the execute ( ) method is called automatically.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" ""> <struts> <package name="default" extends="struts-default" namespace="/"> <action name="Login" class="com.struts2.action.Login" method="validation"> <result name="success">Success.jsp</result> </action> </package> </struts>

Step 6 - Creating the Action class
Create a new class in the package com.struts2.action. This is where all the action is (pun intended). As mentioned earlier, the control would be passed over to the mentioned method validation() in .
package com.struts2.action; import com.opensymphony.xwork2.ActionSupport; public class Login extends ActionSupport{ /** * */ private static final long serialVersionUID = -6105380673356535743L; String username; public String validation(){ setUsername( username); return SUCCESS; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }

Step 7 - The result page
We are almost done now. Create the result page Success.jsp in webcontent. This is the page mentioned in the result tag in struts.xml
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Success</title> </head> <body> <h2>welcome <s:property value="#attr.username"/></h2> </body> </html>

And we are done!!!!!
Your struts2 application is now ready for deployment. There are a couple of questions I've come across while creating this sample project. One of them being why do we add the exact jars that we add, as in, each and every jar file that we add to our project is essential, why though? I'd try to answer that in a following post. Additionally, I came across some changes I had to make while implementing the project in struts2.3.4 as opposed to struts2.1; I wanted to list them out in the beginning of this post but in an attempt to keep it short and trying not to venture too far off the mark I edited them out. That and the rest deserve a small pot of their own and soon one shall follow. Do let me know in case something does not work out for you over here, I'd love to help you out. You can get in touch with me over here, or on facebook (the name shouldn't be hard to find.) Let me take the opportunity to thank you for reading this, please feel free to suggest improvements or innovations.

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.