With a release of new Eclipse, I thought it would be interesting to do the same thing, but now in this IDE.
As you probably already know, Eclipse is one of the famous and certainly one of the best Java IDEs.
Well, about a month ago people from Eclipse foundation released a 3.5 version of Eclipse, called Galileo.
I develop almost all my project using NetBeans, and have never done any Struts2 programming using Eclipse.
That is why I decided to write a post on how to create a Struts2 example in new Eclipse release: Galileo. That's quite fun because that way I (we) get to learn on how to use Struts2 in Eclipse, and get to see Galileo in action a little bit closer…
Note: In this post, I’ll use much of the text , entire source code and application logic from my previous post and concetrate only on the differences between these two IDEs.
So, let’s get started:
First of all we need Eclipse Galileo. Go to the http://www.eclipse.org/downloads/ and download Eclipse IDE for Java EE Developers.
Next thing we need is Struts2 itself. 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 archives, extract it to some location, for example: c:\Struts2
It is now time to start a new project. Start Eclipse Galileo.
Click file --> new --> Dynamic web project
Let’s call this project GalileoStruts2Example
I’ll use Tomcat 6 . You can use some other server that you have available , it is completely up to you.
Click Finish.
Your project tree will look like this:
Your project tree will look like this:
Now, in order to use Struts2 libraries, let's add those essential dependencies to project:
Right-click on the project --> properties --> Java Build Path , select Libraries tab and click on the “Add External JARs” button.
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 look like this:
Right-click on the project --> properties --> Java Build Path , select Libraries tab and click on the “Add External JARs” button.
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 look like this:
Now, copy these six JARs into the “lib” folder located in the root of WEB-INF 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 "WebContent" folder.
All Java code that we write, we will place into some package in the "Java Resources: src" 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...
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 "WebContent" folder.
All Java code that we write, we will place into some package in the "Java Resources: src" 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:
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;
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:
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>
Let’s see how our project tree looks at this stage:
There's only 2 things left for us to do:
Edit struts.xml, and web.xml:
struts.xml must be located in “classes” folder located in the root of WEB-INF folder:
This is where place a file struts.properties if we need to use it, as well…
web.xml:
And if we press “Run” button, and choose a server on which it is supposed to run
we’ll get something looking like this:
But don't worry. Everything is OK, we just didin't handle default page...
If we append entername.action on the URL , our new application will open:
If we append entername.action on the URL , our new application will open:
If we do not enter name and press “submit”:
but if we do enter name and press “Submit”:
So, I hope I helped you to understand how to create an empty struts2 project in Eclipse Galileo.
At the end, our project looked like this:
Unfortunately, for me - many things and options remained a little bit more complicated and unintuitive regarding to some other IDEs. That is such a shame!
Nevertheless , for me , Eclipse continues to be one of the best choices!.
Enjoy Struts2 and Galileo!