Archive for the ‘Getting Started’ Category

Getting started with Struts2 using NetBeans as IDE

Monday, January 14th, 2008

In this post, I will look at how Web application development can be started with Struts2. I am using Ubuntu. If you are using other operating systems such as Windows follow similar steps for the platform.

1. Install Java - The first step is to install latest production release of Java. Download J2SE 1.6 from Sun’s Java site - http://java.sun.com/. On Ubuntu, you can use apt-get as given below.

sudo apt-get install sun-java6-bin

After installation, verify the Java version by running the following in the command prompt,

javac -version

2. Install IDE for development - For Web application development there are a lot of IDEs out there. My favorites are Eclipse, NetBeans, JBuilder, WebSphere Application Developer and IntelliJ IDEA. For this example I will use NetBeans since it is free and the download size is reasonable. You can download it from http://www.netbeans.org/

3. Download Struts2 libraries - For developing Struts2 applications, the minimum you need are the base libraries. You can download them from http://struts.apache.org/. Download “Essential Dependencies Only” zip file which is about 5MB.

4. Creating Your first Struts2 Web Application - We will go with a very simple example in which we want the Web application to print “Hello World”. Here are steps required.

(a) Start NetBeans and then create a new Web application. From File->New Project choose Web->Web Application. In the project properties, give the application name as HelloWorld and context name as /HelloWorld. You can either choose Tomcat or Glassfish server. I prefer to use Tomcat. You will get a standard blank Web Application created.

(b) Add Struts2 libraries. For this add a folder “lib” under WEB-INF. Then copy only the following “Essential Dependencies” Jar files from the Struts zip you downloaded. Also add all the jars to compilation path under “project properties”->”libraries”.

Minimum set of jars with which you can start a Struts2 Web project (xxx stands for the latest version number),

commons-logging-xxx.jar
fremarker-xxx.jar
ognl-xxx.jar
struts2-core-2.xx.jar
xwork-xxx.jar

(c) Create web.xml under WEB-INF folder and struts.xml under the root folder for “Source Packages”.

web.xml configures Struts2 to handle all URLs as shown below.

struts.xml configures action classes to URL paths as shown below. It also configures the view component to be invoked after executing controller class.

(d) Create HelloWorld.java under “Source Packages” which is an action class in Struts2 speak. This acts as the controller. The SUCCESS return value points to the view element under default result tag in struts.xml, which in this case is HelloWorld.jsp

(e) Create HelloWorld.jsp under root of Web Content (which is “Web Pages” folder in NetBeans).

(f) “Run the Main Project” from NetBeans and access the URL “http://localhost:8084/HelloWorld/HelloWorld.action” from browser. You should see “Hello World!” in the browser. Now you are all set to start Struts2 Web Application Development!