Building your first Struts 2 application using Eclipse
by , 12-09-2011 at 04:30 PM (10106 Views)
Struts is one of the most popular web framework for Java. It comes with 2 major branches, Struts 1 and Struts 2. Both implement MVC architecture but Struts 2 is much simpler, easier to build, deploy and maintain than Struts 1.
This article will guide you through the steps to build a simple Struts 2 application using Eclipse IDE.
Download Struts 2 and required libraries
Download latest version of Struts 2 from Download a Release
Select the full distribution, struts-2.2.3.1-all.zip
Extract the zip file into a directory on your hard drive, says struts-2.2.3. Look at the folder “lib”, you will see there are many JAR files there, but for a simple hello world Struts 2 application, we need only a smaller number of them.
Create a Java web project
Go to File -> New -> Project… In the dialog New Project, select Web -> Dynamic Web Project.
Figure: The New Project dialog
Click Next, in the New Dynamic Web Project dialog, type project name, “HelloStruts2”, leave other fields as default, then click Next:
Figure: The New Dynamic Web Project screen
In the next screen, “Project Facets”, select version “2.4” for “Dynamic Web Module” and version “5.0” for “Java”, as shown in the screen below:
Figure: Select versions
Click Finish, Eclipse will create a skeleton for the project HelloStruts2. Create a package com.mycompany under “Java Resources: src”:
Figure: Structure of the newly created project
Copy the following JAR files from directory struts-2.2.3\lib that you have extracted before, to the directory WebContent\WEB-INF\lib under the project’s root:
- commons-fileupload-1.2.2.jar
- commons-io-2.0.1.jar
- commons-lang-2.5.jar
- freemarker-2.3.16.jar
- javassist-3.11.0.GA.jar
- ognl-3.0.1.jar
- struts2-core-2.2.3.jar
- xwork-core-2.2.3.jar
Configure Struts 2’s action Servlet
In order to have Struts intercepts all requests coming to the application, we need to configure a filter in the deployment descriptor. Open web.xml file under WebContent\WEB-INF, add the following XML lines:
Configure Struts 2 configurationXML Code: Configuration for Struts in web.xml<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
By default, upon initialization, Struts will look for a configuration file called struts.xml in the application’s classpath. Through the configuration file, you tell how Struts maps between the controller (Action class) and the view (JSP page). Create the struts.xml file under “Java Resources: src” directory, and paste the following code:
In Struts 2, we can group related actions into a package. In the above configuration we define a package “main” and have it extends the Struts’ base package, “struts-default”. In this package, we declare an action “hello” with some interesting stuffs:XML Code: The Struts configuration file<?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> <package name="main" extends="struts-default"> <action name="hello" class="com.mycompany.HelloAction" method="sayHello"> <result name="success">/hello.jsp</result> </action> </package> </struts>
- The name attribute specifies the action name, which is also the URL pattern that Struts will intercept.
- The class attribute points to a Java class which we will create in the next step. This class is the action class, or the controller. When a request having the URL ends with “hello”, this action class will be picked up by the framework and invoke the method defined in the attribute “method”.
- The method attribute specifies the method of the action class that will be invoked.
The sub element “result” inside the action specifies a logical mapping to a JSP page (view):
The name attribute specifies the name of the view. The action class method should return a String matching a view name.
The /hello.jsp is the actual JSP page that will render the view for action “hello”.
So far we can understand the configuration in a natural way: When a request having the pattern “hello” comes in, an instance of class com.mycompany.HelloAction is created, and its method “sayHello” will be invoked. The action can pick up a view named “success” to render response, the view actually maps to the hello.jsp page.
Implement the controller
Next, we should implement an action class that matches the configuration above. The source code looks like the following:
The HelloAction class extends ActionSupport class and implements a method “sayHello” and a property “message”. The sayHello method simply assigns a String “Hello World, Struts 2!” to the variable “message”, then returns a String constant, SUCCESS. This constant matches the view’s name defined in the struts.xml file. The member variable message has appropriate getter and setter methods which can be used by the view to access its content.Java Code: Code of the controller classpackage com.mycompany; import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport { private String message; public String sayHello() { message = "Hello World, Struts 2!"; return SUCCESS; } public void setMessage(String message) { this.message = message; } public String getMessage() { return message; } }
Write the view
Next, we create a hello.jsp file under WebContent directory. The code of hello.jsp file looks like this:
It is a pretty simple JSP file. There are two lines you should be noticed:XML Code: Code of the hello.jsp page<%@ 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" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Struts 2</title> </head> <body> <h2><s:property value="message" /></h2> </body> </html>
- <%@ taglib prefix="s" uri="/struts-tags" %>: instructs the container that the page is using Struts 2’s custom tags.
- <h2><s:property value="message" /></h2>: use the <s:property /> tag to print out the value of the variable “message” which is defined by the action class.
So the page simply displays a text message that is set by the action class, “Hello World, Struts 2!”;
Setup server runtime
So far we have built all the basics: web.xml configuration file, struts.xml configuration file, HelloAction class and hello.jsp page. It’s time to set up a server to run the application. Suppose you have Tomcat 6.0 installed on your computer.
Right click in Eclipse’s Servers tab, select New -> Server:
Figure: Create a new server in Servers view
In the New Server dialog, open the branch “Apache” and select “Tomcat v6.0 Server”, click Next.
In the next screen, click Browse to select Tomcat’s installation directory on your computer:
Figure: Selection Tomcat's installation directory
Click Finish. In the screen “Add and Remove Projects”, select the project HelloStruts2 on the column “Available projects”, then click Add to add the project to the column “Configured projects”, then click Finish.
Figure: Add the project HelloStruts2 to Tomcat
Now the HelloStruts2 application is deployed on Tomcat server.
Figure: New server added with the configured project
Running the application
Now we are ready to start the server and run the hello world Struts 2 application. Right click on the server node in tab Servers, click Start:
Figure: Start the server
Look at the Console tab, you will see some logging information outputted when the server starts and Struts initializes.
Type the following URL into your browser’s address bar (your server’s port may be different):
http://localhost:9898/HelloStruts2/hello
If everything is working fine, the server should return this page:
Figure: Running the Struts 2 application
So far we have built, deployed and run a simple Struts 2 web application. I hope this article will be helpful for those who are starting to learn Struts 2.




















Email Blog Entry
License4J 4.0
Today, 12:23 AM in Java Software