Thursday 9 July 2009

How to create Struts2 project in 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!

37 comments:

Unknown said...

It sucks to put code in images. Great for copy-pasting.

Darko Kalinic said...

I agree with you, it is easier to copy/paste code, but that way many will never learn to write that code code by themselves ... :-)

OK , It is not problem for me to put text instead of images, for now on - text only...

Anonymous said...

Thank you! I just started to learn Struts 2 and tried several examples that did not work. Your example works fine. Thank you!

Darko Kalinic said...

I'm glad you like it... ;-)

Anonymous said...

The Worst code i ever seen in my life....
Stop making fool to peoples..U r code didnt work at all....
WTF

Darko Kalinic said...

And what code exactly would that be? :-)

Class "Hello" ?
It's a class with just 2 pairs of getters and setters... :-)
It's something that NetBeans generated :-)

Nevertheless, this post is all about creating an empty Struts2 project, not writing code...
Class "Hello" is here just to make an example of how Struts2 work...

Example, not something advanced...

And of course it works... You maybe didn't do everything as you were told to...

If you tell us what problem/exception you have with it, maybe we would be able to help you do it properly...

Anonymous said...

I,m new to Struts 2.But I did that example with GlassFish Server..
But I got the service error like that
HTTP Status 503 -

--------------------------------------------------------------------------------

type Status report

message

descriptionThe requested service () is not currently available.


--------------------------------------------------------------------------------

Sun GlassFish Enterprise Server v2.1

please help me to correct that..
thanks

Valon said...

Darko,

I got this working with NetBeans 6.8 and Tomcat 6.

Thanks for taking the time to post this example.

It is true you have to write your own code to really learn. But when trying to get something basic working, it is nice to have a complete cookbook of code that can be copyied and pasted.

In addition to providing all code as text that can be copied, I suggest you also note:

1) Which project folder the individual files go it. I looked at your image, at the end of the tutorial, of the entire project so then saw where to put, for example, Hello-validation.xml.

2) You need to right click on the project to deploy to the target server.

3) Make sure the application server is not running as you get an obscure error. Let NetBeans start the server. At least this is what I had to do for Tomcat, for the initial deployment.

Thanks again!

Valon said...

I'm using NetBeans 6.8 and it appears to have all the current updates. It does not have a built in framework for Struts 2. Any suggestions re. how to get this added in via NetBeans or Maven? I'm new to NetBeans.

It seems the best way to automate project setup is with Maven: http://wiki.netbeans.org/MavenBestPractices#Best_Practices_for_Apache_Maven_in_NetBeans_6.x

Thanks

Small Flower said...

please i need your help , i try the tutorial you explain and i download the jars versions as minsion but i got the following error

SEVERE: Exception starting filter struts2
Unable to load configuration. - action - file:/C:/Documents%20and%20Settings/cute/My%20Documents/NetBeansProjects/Struts2Example/build/web/WEB-INF/classes/struts.xml:25:62
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:431)
at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:190)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)


is there any configuration i should modify?

please reply on me on this email
miramld@yahoo.com
thank you

Small Flower said...

please i need your help , i try the tutorial you explain and i download the jars versions as minsion but i got the following error

SEVERE: Exception starting filter struts2
Unable to load configuration. - action - file:/C:/Documents%20and%20Settings/cute/My%20Documents/NetBeansProjects/Struts2Example/build/web/WEB-INF/classes/struts.xml:25:62
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:431)
at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:190)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)


is there any configuration i should modify?

please reply on me on this email
miramld@yahoo.com
thank you

Reshma said...

hi, i agree with you, it is easier way to learn struts2 i follow your application but i have an error to running it like this....The requested resource (/Struts2Example/entername.action) is not available.

please tell me welcome page in web.xml(index.jsp)

Reshma said...

hi, i agree with you, it is easier way to learn struts2 i follow your application but i have an error to running it like this....The requested resource (/Struts2Example/entername.action) is not available.

please tell me welcome page in web.xml(index.jsp)

Reshma said...

hi, i agree with you, it is easier way to learn struts2 i follow your application but i have an error to running it like this....The requested resource (/Struts2Example/entername.action) is not available.

please tell me welcome page in web.xml(index.jsp)

Unknown said...

Good job Darko, i thank you very much for your time spent in this tutorial

Unknown said...

I got this working with NetBeans 6.9 and Glassfish Server 3. Darko, thank you for taking the time to write up this tutorial.

Unknown said...

I got this working with NetBeans 6.9 and Glassfish Server 3. Thanks, Darko, for taking the time to write up this tutorial.

Darko Kalinic said...

Hello to everyone!

Please accept my apologizes, since I didn't reply to your posts.
I was very very busy...

@Anonymous: I cannot help without furthermore info about what happened.
Generally, you obviously missed something while reading text...

@Valon:
I try to pint out where each file goes, but sometimes I just forget to do so. This is where image takes it's part...
As for adding Struts2 support to NetBeans, I thing there is no way to do it yourself easily, without writing a new module. I think there is some beta version project on this... Google a little...

@avner & @aroc725:
You're welcome! ;-)

அணில் said...

//soon find out that nothing I found on the web worked for me..
Yes sir. Ur tutorial helped me to kick start struts2 successfully in netbeans6.9. Active comments even after one year is testimony to that. Thank you.

Darko Kalinic said...

I am very happy to hear that!

Unknown said...

I am not able to Install Struts2 support on NetBeans 6.9 (http://nbstruts2support.dev.java.net). I get the following error:

" Some plugins require plugin Editor Library to be installed.

The plugin Editor Library is requested in version >= 1.38.2.9.2 (release version 1) but only 2.10.1.10.2 (of release version different from 1) was found.

The following plugin is affected:
Struts2 Support "

Pls urgent help.

Darko Kalinic said...

@Mayayane Baloi:

Unfortunately, I can't help you with this...

I don't use any plugin for struts2...

Unknown said...

Hello Sir,
Really wonderful
You helped so many people over the net
and I am one among them
thank you sir,

hemant said...

Baloi, you can check this link. Here i have posted the link for the updated plug-in for the netbeans 6.9.
http://www.coderanch.com/t/510771/vc/Struts-Netbeans

Mahesh said...

Hi Darko,

I found is a good example. But very weired tho, I was unable to find web.xml When I create an appliction this way. But when I chose a frame work I do get the web.xml.
I can only find sun-web.xml. Hope I get a solution for this. Thank you

Darko Kalinic said...

Mahesh,

You must have a web.xml file when you create web application project in netbeans...
I don't know what you did wrong...?

Anonymous said...

Hi
thank you very much for this brief, focused and simple example of creating struts 2 application.

after following this tutorial, I have some problems to deploy this application, follows the console messages :

In-place deployment at /NetBeansProjects/teststruts2/build/web
deploy?config=file%3A%2Ftmp%2Fcontext2205351369006511581.xml&path=/teststruts2
FAIL - Deployed application at context path /teststruts2 but context failed to start
NetBeansProjects/teststruts2/nbproject/build-impl.xml:694: The module has not been deployed.
BUILD FAILED (total time: 9 seconds)

I'm using tomcat 6 and Netbeans 6.9.

any help please, thanks in advance.

Darko Kalinic said...

The answer is very simple: you probably did not do everything as I wrote... :-)

Start with your version of Struts2, and read everything again, and check if you did everything as it was told...

RedHat Linux said...

@Anonymous (posted on 7 Jan 2011)
Solution to "build-impl.xml:694: The module has not been deployed.
BUILD FAILED (total time: 9 seconds)"

error is stop tomcat in Netbeans, clean, build and run project again.

Utkarsh said...

Best Struts2 example for a novice !!
clear simple and realistic
thnks for usefule post....

Kaleeswaran said...

I am new to this technology.... Ur tutorial is great , has showed me the way... Thanks

Visal Singh said...

Thanks Darko. i am so tankfull to u for this application of struts2. its too simple and also easy to understand than u.

Vishal Singh

Visal Singh said...

Thank u Darko!
your example is too simple and eaisly understands.

Thank u

Anonymous said...

Mine did not work... I have the same problem as "Redhat Linux"... about that build-impl.xml problem...

Though I noticed that this occurs if I add the filter in web.xml...
Without adding that, it creates another problem though :(

I'm stuck.

Anonymous said...

hi...i need develop in netbeans integrating struts2 + spring + hibernate in windows7....help me please!!...grx...

can you send some example??

my mail is

the_new_kornflakes@hotmail.com

Neil said...

I faced a error while running the project "SEVERE: Exception starting filter struts2" which was resolved on including following jar files.

commons-fileupload-X.X.X.jar
commons-io-X.X.X.jar
commons-logging-X.X.X.jar
commons-logging-api.X.X.jar
freemarker-X.X.X.jar
ognl-X.X.X.jar
struts2-core-X.X.X.X.jar
xwork-core-X.X.X.jar
javassist-3.7.ga.jar (new for Struts versions 2.2.1 and higher)

I am using netbeans7 and tomcat7. Just posting so that it helps someone.

Neil said...

commons-logging-api.X.X.jar
freemarker-X.X.X.jar
ognl-X.X.X.jar
struts2-core-X.X.X.X.jar
xwork-core-X.X.X.jar
javassist-3.7.ga.jar (new for Struts versions 2.2.1 and higher)

I am using netbeans7 and tomcat7. Just posting so that it helps someone.