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.jarMost 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
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!