Skip to main content

Create Struts2 project Using NetBeans

If you decide to start learning and using struts2, probably one of the first things you think about is which IDE to use, and what you need in order to create and run a Struts2 project...

If you search the web , you'll find many tutorials on how to create a struts2 project, and start developing.
This is what I did when I was starting, and soon find out that nothing I found on the web worked for me in proper manner...

So, I decided to create a small tutorial on how to create an empty struts2 project , in order to help others, and save their time.
Not to teach you Struts2, but to help you create a working Struts2 project!

Ok, I will use NetBeans 6.5, and a Tomcat 6 that comes with it...

First of all, go to http://struts.apache.org/2.x/index.html and download the latest version of Struts2.
I downloaded struts-2.1.6-all.zip
You won't be needing all the libraries in it. Just 6 of them. Because of this, you can download essential dependencies only.

When you download one of these files, extract it to some location, for example: c:\Struts2
It is now time to start a new project. Start NetBeans.

Click File -> New project -> Java web -> Web application



Let's name our project "Struts2Example". Click "next".
Choose which server you wish to use ( I selected Tomcat 6 ), set JavaEE to version 5, and leave ContextPath to be /Struts2Example

You can click "finish" now.
If you click "next" DO NOT, I repeat DO NOT choose available ( older version ) of struts under "frameworks".

Your project tree will look this:



Now, in order to use Struts2 libraries, let's add those essential dependencies to project:

Right click on "Libraries" folder and then click "Add JAR/folder". A dialog will open.
Go to folder where you extracted Struts2 libraries and select ( add to project ) following files:

- struts2-core-2.1.6.jar
- freemarker-2.3.13.jar
- ognl-2.6.11.jar
- commons-fileupload-1.2.1.jar
- commons-logging-1.0.4.jar
- xwork-2.1.2.jar


Most of the tutorials on the web are written for older version of struts2, at the time when commons-fileupload-1.2.1.jar was not mandatory, so even when following all the steps in tutorial , you just couldn't get your project to work ( with newer version of Struts2 ) !

Now, your project will loke like this:



Now, in the root of WEB-INF folder create a new folder , and name it "lib".
Copy these six JARs into this new folder.



These files will be uploaded to App Server , and a deployed application will use it.
We could put this files into main lib folder of application server, but it is most probably better this way...

We need to create one more folder in the root of the WEB-INF , and name it "classes".
This is where you will create struts.xml, and if needed - a struts.properties file.




I'll put all my JSP's into new folder, "jsp" , which I will create in the root of "Web Pages" folder.

All Java code that we write, we will place into some package in the "Source Packages" folder.
I'll create one now, called "Struts2Example" and a "Hello" Class in it.
If you use validaton, put all Validation XMLs inside class's package too...





This is how, at minimum, your Struts2 project structure should look like.


I'll create one small Struts2 application now. Nothing complicated.
User will enter his name, and press "submit" and other page will open , saying Hello to this user.

So, let's first create a Java class extending ActionSupport class , named hello, inside Struts2Example package.
Let's make it to look something like this:


package Struts2Example;

    import com.opensymphony.xwork2.ActionSupport;

    /**
    *
    * @author Darko
    */
    public class Hello extends ActionSupport
    {
    private String name, message;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getMessage() {
    return "Hello " + getName();
    }

    public void setMessage(String message) {
    this.message = message;
    }

    }

We now need two JSPs, for example "nameinput.jsp" and a "response.jsp"
Each JSP needs to contain this line, in order to use struts2 custom tags in it:

<%@ taglib prefix="s" uri="/struts-tags" %>

nameinput.jsp:



response.jsp:


Let's create an XML validation file for Hello class, and make a name property mandatory.
Hello-validation.xml:


<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
<field name="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Please enter name</message>
</field-validator>
</field>
</validators>

There's only 2 things left for us to do:

Edit struts.xml, and web.xml:

struts.xml:


web.xml:


and if you deploy this project to server, and open
http://localhost:8084/Struts2Example/entername.action , you'll get something like this:




If we do not enter name:





and if we do:




So, I hope I helped you to understand how to create an empty struts2 project in NetBeans.
At the end, our project looked like this:




There's more to it. For example, if you wish to override some theme's behaviour ( for example "simple" ) you create folders
template/theme_name ( for example template/simple ) and it you copy all the original files from this theme ( from struts2-core-X.X.X.jar ) and edit them in this folder...



But, some other time about that and some other usefull things...

Enjoy Struts2!

Comments

Post a Comment

Popular posts from this blog

Struts2 - Radio Tag (Form Tags)

In this section, we are going to describe the radio tag. The radio tag is a UI tag that renders a radio button input field. Add the following code snippet into the struts.xml file. struts.xml <action name="radioTag" class="net.struts2.checkboxlistTag">   <result>/pages/uiTags/radioTag.jsp</result> </action> Create an action class with two lists as shown below: checkboxlistTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.*; public class checkboxlistTag extends ActionSupport{      private List fruits;   private List animals;     public String execute()throws Exception{   fruits = new ArrayList();   fruits.add("Apple");   fruits.add("Mango");   fruits.add("Orange");   fruits.add("Pine Apple");  ...

Struts2 - Combobox Tag (Form Tags)

The struts2 combobox is basically an HTML INPUT of type text and HTML SELECT grouped together to give you a combo box functionality. You can place text in the INPUT control by using the SELECT control or type it in directly in the text field. Add the following code snippet into the struts.xml file. struts.xml <action name="comboboxTag" class="net.struts2.comboboxTag">    <result>/pages/uiTags/comboboxTag.jsp</result> </action> Create "comboboxTag " action class and a list in the class and populate it with various items as shown in the class. comboboxTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.*; public class comboboxTag extends ActionSupport{      private List fruits;   public String execute()throws Exception{   fruits = new ArrayList();   fruits.add("Appl...

Global Action Forwards and Action Mappings

What is an action forward? A action forward can be used to forward to a jsp or action mapping. There are two different action forwards. The global action forward and the local action forward. You can access a global action forward on each jsp or action class. A local action forward can only be accessed by the assigned action class. What is a action mapping? The action mapping is the heart of struts. It managed all actions between the application and the user. You can define which action will be executed by creating a action mapping. The diagram show you, how the application server manage the request of the index.jsp or a non existing action mapping. In the first step we create a new action mapping. Open the struts-config.xml, you will find it in the folder WebRoot/WEB-INF. Right click in the outline window on action-mapping. Choose Use Case default and Action Type Forward. The Forward Path is the welcome page /jsp/index.jsp In the second step you create a global act...