Struts 2 – Hands on Tutorial
Struts 2 Practical Tutorial:
Following tutorial demonstrates you a simple struts2 application and explains you how things work together in struts2. The tutorial demonstrates sruts2 using a calculator application which calculates different operation results and displays them.
The tutorial uses Eclipse3 IDE - or above.
Step 1: Download struts2
Download struts2-blank-xxx.war from : http://struts.apache.org/download.cgi
Step 2: create a project in eclipse
Create a new Web project in eclipse. (To create webProject click File Menu on top–> select New –> Select Web Project ).
Once you create the application will have src folder and a WebRoot folder.
Step 3: copy jars
Copy jars from downloaded struts 2-blank/EB-INF/lib to WebRoot/WEB-INF/lib directory. You can see the copied jars in the references jars once you refresh the project after copying.
Step 4: web.xml setup
Struts2 is based on MVC (Model – View – Controller) architecture. All requests must go through a central controller. This controller decides what action should be taken and then generates the view and sends back to the client. The controller in struts 2 is filter called ‘FilterDispatcher’ . The Controller reads struts.xml file to map a url request to an Action. We will discuss the struts.xml later.
To make all the calls go through he controller, we have to configure it in web application’s deployment descriptor. i.e web.xml file.
We need to configure this in the web.xml as below:
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app id=”WebApp_9″ version=”2.4″ xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd“><display-name>Test</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter><filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping><welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list></web-app>
This FilterDispatcher acts as controller here. All the requests to any url of this application get redirected to this filter.
Now we have setup struts 2 framework. We now start developing calculator components.
Step 5: The Action Class
Action class has execute() method, where the action for the request takes place. This method gets called when there is any request came for this action. All the request data gets copied to the action class using getter methods before calling execute method. In the example, we have param1 and param2 parameters, which will be copied to the action class using the setter methods exposed. In action class’s execute method, one can concentrate on business logic leaving beind codes like retrieving request parameters, validation etc.
Once the business logic is done the Action classes execute method returns a String. This String is used by controller to determine which view to be displayed. The view will be picked up. The data needed by the view (jsp page) will be retrieved from the action class using setter methods
The action class is a simple bean, which calculates the results of the input values. Create a package example and create a class Calculator with following contents:
package example;
public class Calculator {
private int param1;
private int param2;
private int result1;
private int result2;
private int result3;
private int result4;public String execute() {
if(param1>0 && param2 >0){
result1 = param1 + param2;if(param2> param1)
result2 = param2 – param1;
else
result2 = param1 – param2;result3 = param1 * param2;
result4 = param1 / param2;
}
return “result”;
}public int getParam1() {return param1; }
public void setParam1(int param1) { this.param1 = param1;}
public int getParam2() {return param2; }
public void setParam2(int param2) { this.param2 = param2; }
public int getResult1() { return result1; }
public void setResult1(int result1) {this.result1 = result1; }
public int getResult2() { return result2; }
public void setResult2(int result2) {this.result2 = result2; }
public int getResult3() { return result3; }
public void setResult3(int result3) {this.result3 = result3; }
public int getResult4() {return result4; }
public void setResult4(int result4) { this.result4 = result4; }
}
Step 7: The view
The Jsp files:
Create a file called Calculator.jsp and copy following contents:
<%@ page contentType=”text/html; charset=UTF-8″ %>
<%@ taglib prefix=”s” uri=”/struts-tags” %>
<html>
<head>
<title>Name Collector</title>
</head>
<body>
<h4>Enter 2 Numbers</h4>
<s:form action=”Calculate”>
<s:textfield name=”param1″ label=”Number 1″/>
<s:textfield name=”param2″ label=”Number 2″/>
<s:submit/>
</s:form>
<h4>Sum = <s:property value=”result1″/></h4> <br />
<h4>Diffence = <s:property value=”result2″/></h4> <br />
<h4>param 1 X param 2 = <s:property value=”result3″/></h4> <br />
<h4>param 1 / param 2 = <s:property value=”result4″/></h4> <br />
</body>
</html>
The view fetches result1, result2, result3 and result4 properties from the action class targeted using the url. . In View we can access and set Action data using an expression language called OGNL.
When submit button is clicked, the Calculate.action will be called.( <s:form action=”Calculate”> )
Step 6: The struts.xml file
This is the main configuration file where we define which url request should be mapped to what Action. Controller uses this file to determine the Action and the resulting view to be displayed.
Create a file named struts.xml in src folder and copy following contents.
<?xml version=”1.0″ encoding=”UTF-8″ ?>
<!DOCTYPE struts PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”
“http://struts.apache.org/dtds/struts-2.0.dtd“><struts>
<constant name=”struts.enable.DynamicMethodInvocation” value=”false” />
<constant name=”struts.devMode” value=”false” /><package name=”default” namespace=”/” extends=”struts-default”>
<action name=”Calculator”>
<result>/Calculator.jsp</result>
</action>
<action name=”Calculate”><result name=”result”>/Calculator.jsp</result>
</action>
</package>
<!– Add packages here –></struts>
With above setup, any request to http://server-name:port/application_name/Calculator.action
Will load the Calculator.jsp file with empty values.
Any request to Calculate.action will call execute() method onresult”>)
Now access http://localhost/test/Calculator.action, You can enter 2 numbers and get results.


Classic and direct tutorial, i found this after 2 hrs of googling and it made it so simple.
Thanks
Thanks DA. Glad you liked it.