This tutorial guides you through the process of creating and running a basic “Hello, World” Struts 2 web application using the NetBeans IDE 6.1.

NOTE: I recommend trying to follow the NetBeans tutorial on the Struts 2 website first. If you find any problems following it, then come back to my tutorial. I have tried to make this more beginner friendly by including a lot of screenshots.

Download/Install Java SE, NetBeans and Struts 2

  1. Download and Install the latest version of Sun JDK: Sun Java SE 6 Update 7.
  2. Download and Install the latest version of NetBeans IDE: NetBeans IDE 6.1.
  3. Download the Struts 2 “Full Distribution” package(~90MB) and extract it in a directory. You can also opt for the smaller “Essential Dependencies Only” package if you want to later incrementally add additional packages as and when the need arises.

    Download Struts 2 Full Distribution or Dependecy Only package.

  4. Run the NetBeans IDE and create a new Web Application project in it. Name it “HelloStruts2World” or something like that.

    Create a new web application project in NetBeans.

    Don’t select any frameworks, especially Struts 1.2.9, to be added to the project.

Add Struts 2 Libraries to Web Application project in NetBeans and configure web.xml file

  1. Add the Struts 2 library files to the web application project: Expand the web application project structure in the Project pane if not already expanded; right-click on the Libraries folder and click the Add JAR/Folder… menu item.

    Add Struts 2 JAR library files to NetBeans Web application project.

    In the Add JAR/Folder file chooser dialog box, browse to the extracted Struts 2 directory, go to lib directory in it and select all the JAR files that you want to add to the project. Hold down the Ctrl button to select multiple files.

    Add basic Struts 2 JAR library files to NetBeans Web Application project.

    The following JAR library files are essential for most Struts 2 projects(and for this tutorial) but you can select more if you know that you need them(you can add additional libraries later also):

    • commons-logging
    • ognl
    • struts2-core
    • xwork
    • freemarker

    Libraries folder in your project will now look like this:

    Struts 2 JAR files added to Libraries folder of web project in NetBeans.

  2. Add a filter element to web.xml deployment descriptor to let Struts 2 handle all the requests for your web application. Expand the Configuration Files folder and double-click on web.xml file, select the XML tab and copy paste the following code in it.

    [xml]



    Struts 2 filter
    org.apache.struts2.dispatcher.FilterDispatcher


    Struts 2 filter
    /*



    30



    [/xml]

    You can enter the same data in Filters tab also:

Create Struts 2 action Java class and JSP result page and configure struts.xml file

  1. We need one or more Java packages to store all the Java classes(Struts 2 actions) in it. Let’s call the first package “hello.” Right click Sources Package directory, select New and then select Java Class… menu item. Enter a name for the Java class(say HelloStruts2World”) and enter a package name(say hello) and click Finish.
  2. Edit the HelloStruts2World.java file so that it looks like this:
    [java]
    package hello;
    import com.opensymphony.xwork2.ActionSupport;

    public class HelloStruts2World extends ActionSupport {
    private String userName;
    public String getUserName() {
    return userName;
    }
    public void setUserName(String userName) {
    this.userName = userName;
    }

    private String message;
    public String getMessage() {
    return message;
    }

    @Override
    public String execute() {
    message = “Hello, ” + userName + “.”;
    return SUCCESS;
    }
    }
    [/java]

  3. We now need to configure the Java class created in the above step as a Struts 2 Action and map it to a result page. To do so, we need to create two Struts 2 configuration files – struts.xml and struts.properties – in the source package.
    1. Right-click on hello package in Source Packages folder and select New and then select XML document… and enter struts as the file name. This will create struts.xml file.
    2. Right-click on hello package in Source Packages folder and select New and then select Properties File… and enter struts as the file name. This will create struts.properties file.

    Edit struts.xml file such that it has the following content:

    [xml]
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">


    /index.jsp

    [/xml]

    This maps the requests sent to the “/HelloStruts2World.action” url to HelloStruts2World.java Struts 2 action and after the successful execution of the request, it gets directed to the index.jsp result page. Leave the struts.properties file empty for now.

  4. Let us access the message property set by HelloStruts2World action and display it in the index.jsp page. Edit index.jsp so that it looks like this:

    [html]
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="s" uri="/struts-tags" %>
    "http://www.w3.org/TR/html4/loose.dtd">




    Hello World!

    Struts 2 Message:


    Enter your name:




    [/html]

Run the Struts 2 Web Application Project in NetBeans 6.1 IDE

  1. The “HelloStruts2World” web application project structure, when relevant folders expanded, should look like this:

    Hello World Struts 2 web application project structure in NetBeans IDE.

  2. Run the web application by going to Run -> Run Main Project and then access the following URL in a web browser: http://localhost:8080/HelloStruts2World/. You may have to modify this URL depending on what port the web server is running(“8080″) and what is the name of your web application(“HelloStruts2World”).

    Struts 2 Action redirects to JSP "SUCCESS" result page

Other Options:

This is the manual way to develop Struts 2 web applications in the NetBeans IDE but it is at least more beginner friendly than working from the command line using just the Maven build tool. There is a Struts 2 plugin called nbstruts available for NetBeans; its functionality is currently limited but it is under active development. Other popular alternative IDEs for developing web applications using Struts 2 are Eclipse and IntelliJ IDEA.