Developing Struts2 Applications Using NetBeans
Struts2 is a highly extensible and modular Java based MVC web application framework. It substantially reduces the effort and time required to build large enterprise web applications. Various Java development environments such as Eclipse, NetBeans etc. can be used to build web applications using Struts2. This article is a step by step guide on building Struts2 web applications using NetBeans. The article assumes that you are using a Windows machine, but the steps are similar in any platform.
Prerequisites
The following tools are required to start Struts2 development using NetBeans. Since new versions are always coming out, the versions mentioned may be different from the latest versions available for download. Usually this is not a problem since new versions are compatible with older code,
- NetBeans 7 or above (NetBeans download page : 150MB+) : Download the Java EE edition of NetBeans which contains Java SDK 6 for compiling Java code and Tomcat server/Glassfish server for running applications. When you download, there is a brief user survey which you may ignore and proceed to download.
- Struts2 binaries 2.2.3.1 or above (Struts2 download page : 75MB+) : Download the latest binary release of Struts2 which is above 2.2.3. Also ensure that you download the full distribution which contains all binaries, source code and documentation for Struts2. The newer Struts2 releases contain Javassist libraries and hence the following download is not required.
- Javassist binaries 3.7 or above (Javassist download page : 2MB+) : This is required if you are using Struts2 2.2.1 or above. If you don’t include this jar you will get runtime exception java.lang.ClassNotFoundException: javassist.ClassPool in Tomcat. However JBoss server is bundled with Javassist and hence it is not required in JBoss.
Step 1 : Install NetBeans
Double click on the installer to install NetBeans IDE. During installation select both Glassfish and Tomcat servers. Glassfish can be used when you want to try out EJB based projects. Also during installation you need to select location for NetBeans and location for Java SDK. I usually modify the location of NetBeans and retain the default for the location of Java SDK. If you have selected Glassfish and Tomcat, you can also configure the locations for them.

The installation takes about 2 minutes to complete. At the end of the installation, you have an option to provide anonymous usage data to NetBeans web site. I usually uncheck this for performance reasons.
Step 2 : Create a Struts2 Project in NetBeans
A Struts2 application is not much different from a J2EE web application. The only difference is that to build Struts2 applications you need to have additional jars in class path and also need to provide a number of configuration file.
Open NetBeans IDE by running netbeans.exe located inside the bin folder of NetBeans installation. Alternatively you can also invoke it from Windows application menu.
From NetBeans main menu, select File => New Project menu item. This opens up the project wizard as shown below. Select Java Web/Web Application from the wizard and then click next.

Give a name for your project and choose a location for storing all your projects. Based on your project name, NetBeans will create a subfolder under the location mentioned above. Click on next.
![]()
Select Tomcat as the server and click on finish.
![]()
Now you will have a sample web application named HelloWorld in NetBeans. We will modify this project to convert this into a Struts2 web application.
Step 3 : Developing and Configuring Struts2 Application
In step 2, we got a standard web application up and running. Now we will convert this into a Struts2 web application. In NetBeans, click on the "Files" view on top left window. Right click WEB-INF folder and then add lib folder under it.
Extract the Struts2 zip file downloaded in prerequisites to a temporary folder. Drag and drop the following files from lib subfolder in temporary folder to Libraries folder in NetBeans project. This is the minimum set of libraries required to create a Struts2 application.
- 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 (from Struts2 libraries or can be downloaded separately)
- struts2-core-2.2.3.1.jar
- xwork-core-2.2.3.1.jar
- ognl-3.0.1.jar
After the copy, "Files" view will look like the above screenshot on the right.
Now we need to configure Struts2 framework in our web application. This is done by adding the Struts2 as a filter in standard web application descriptor (web.xml). To add web.xml in NetBeans project, right click the project and click on "New" and then on "Other". From the wizard, select standard deployment descriptor as shown below and then click on "Next" and "Finish".

Replace the content in WEB-INF/web.xml with the following lines. This ensures that all requests received by the application is routed to a Struts2 controller,
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<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>
</web-app>
Now we will create a very simple HelloWorld program using MVC architecture of Struts2. Our application will print "Hello World" to the browser when user accesses a URL. In an MVC architecture, Model will be responsible for processing data, Controller for invoking appropriate model and view and View for display of the data to the end user.
Let us first create a Model class which will return the message "HelloWorld!". Create a class "HelloModel" in package "com.struts2.samples". This model object has a single business method getMessage() which will return the message to be displayed to the user. The contents of HelloModel.java is given below,
package org.struts2.samples;
/**
*
* @author jayson
*/
public class HelloModel {
public String getMessage() {
return "Hello World!";
}
}
Now let us create the controller class which would process the user request, get the data from the model object and then decide which view needs to be shown to the user. Create a class "HelloWorld" in package "com.struts2.samples". Controller classes in Struts2 is known as action classes and they usually extend from ActionSupport class. The contents of HellWorld.java is given below,
package org.struts2.samples;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
private String message;
@Override
public String execute() throws Exception {
HelloModel model = new HelloModel();
message = model.getMessage();
return SUCCESS;
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message the message to set
*/
public void setMessage(String message) {
this.message = message;
}
}
HelloWorld extends the execute() method in ActionSupport. This method is automatically called when a URL request is configured to be forwarded to an action class (see struts.xml below). First we invoke the model object and then set the returned message in a member in HelloWorld. We then forward the user to a view pointed to by the value SUCCESS (see struts.xml below). The members in the action class can be directly referred in the view class using Struts2 custom tags.
Let us now create a simple view using JSP rendering. Create or modify index.jsp inside the Web Pages folder of the NetBeans project. The contents of index.jsp is given below,
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title><s:property value="message"/></title>
</head>
<body>
<h1><s:property value="message"/></h1>
</body>
</html>
Note the use of Struts2 custom tags. Using the taglib directive we register struts-tags under the namespace prefix s. We can then use various Struts2 custom tags in JSP. The property tag enables easy access to the variables on the top of the OGNL stack made available by Struts2 in JSP. In this case top of the stack corresponds to the action class controller which invoked this JSP. Internally Struts calls getMessage() method of the action class when the property custom tag with value "message" is encountered.
Finally to glue all the components together, create struts.xml under Source Packages in the project. This defined how a URL is mapped to an action class invocation and how the return values in action classes are mapped to view components.
<?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="sample" extends="struts-default">
<action name="HelloWorld" class="org.struts2.samples.HelloWorld">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
Note how the action name "HelloWorld" is mapped to the actual class using the action tag in XML. Also note how the success value is mapped to the view component "index.jsp". Since the action is organized under a package named "sample", the URL for accessing the action is http://[server:port]/[application context]/sample/HelloWorld.action. The actual URL since you are developing locally would be http://localhost:8084/HelloWorld/sample/HelloWorld.action
Step 4 : Running Struts2 Application in NetBeans
To run the sample application, click on the Run menu in NetBeans. This will compile the project and would deploy the application to Tomcat and then will open default application URL in the browser. Change the URL to point to your HelloWorld action class – http://localhost:8084/HelloWorld/sample/HelloWorld.action . You will see "Hello World!" printed in the browser.
Struts2 Application Workflow in a Nutshell
When you access the URL http://localhost:8084/HelloWorld/sample/HelloWorld.action,
- Struts2 filter configured in web.xml gets invoked. This looks at the URL and looks for a matching entry for HelloWorld (.action is automatically stripped off) in "sample" package in struts.xml.
- The execute() method of the HelloWorld action class is invoked which prepares the message and returns the value of "success".
- Struts looks for a view named "success" in struts.xml under the action class HelloWorld and finds index.jsp. The request is forwarded to index.jsp.
- The custom tags in index.jsp is executed and the value for "message" attribute in action class is rendered by making a call to method getMessage().
October 18, 2011 | Posted in Tutorials 1 Comment »

November 26th, 2011 at 5:20 pm
[...] tutorial requires Oracle JDK 1.6 and NetBeans 7 or above installed in your machine. Check out NetBeans installation guide for details on installing [...]