create a simple "hello World" Eclipse Dynamic Web Project
add a simple JSP
deploy the project on Apache Tomcat
add Apache Struts 2 support to the aplication
create a simple Struts 2 action
add Apache Tiles 2 support to the aplication
Prerequisites
This guide assumes the following:
Eclipse IDE for Java EE Developers is installed. I'm running the Galileo version (it can be downloaded from here)
Apache Tomcat has been downloaded and installed somewhere on your hard drive (it can be downloaded from here and installation instructions can be found here). I have it installed in C:\apache-tomcat-6.0.16
Apache Struts 2 Full Distribution has been downloaded and extracted somewhere on your hard drive (it can be downloaded from here). I have extracted it to C:\struts-2.1.8
You have some knowledge about Java EE web applications (more info can be found here)
Creating a Dynamic Web Project in Eclipse
From Eclipse, let's create our project by performing the following steps:
File > New > Project... > Web > Dynamic Web Project > Next
In the Dynamic Web Project window, set HelloWorld as the project name.
Now click on Target Runtime > New.... Select Apache Tomcat v6.0 and click Next
Make sure the you set the appropriate value for the Tomcat installation directory field:
Click Finish and Finish again.
Now our Eclipse project should be created:
Creating a JSP
In Eclipse's Project Explorer, right-click on the WebContent folder and select New > JSP.
Name the JSP HelloWorld.jsp and click Finish:
Edit the JSP and make sure it looks something like this:
In Eclipse's Project Explorer, right-click on the HelloWorld project node and select Run As > Run on Server:
Select the Tomcat v6.0 Server at localhost and click Finish
If you take a look a the Servers tab you should see Tomcat up and running your application deployed in it. Also, by right-clicking the server node you will be able to restart Tomcat, shut it down, etc:
Plase note in lines 11 to 21 how we instruct Tomcat to pass all requests (denoted /*) to the Struts 2 filter. (More info about filters can be found here)
Create a class called HelloWorldAction:
Right-click on "HelloWorld" project node > New > Class
Click Finish
Edit HelloWordAction.java so it looks like:
public class HelloWorldAction {
public String execute(){ return "success"; }
}
Create a an XML file called struts.xml:
"HelloWorld" project node > Java Resources : scr > Right-click (default package) > New > XML
Click Finish and edit the file so it looks like:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
In line 7 we instruct Struts 2 to instantiate HelloWorldAction every time a request to HelloWorld.action is made. And in line 8 we indicate that every time the action's public String execute() method returns the success string HelloWorld.jsp should be returned.
Now start Tomcat:
"Servers" tab > Right-click "Tomcat v6.0 Server at localhost" > Click "Start"
<html> <head> <title><tiles:getAsString name="title" /></title> </head> <body> <div id="header" style="height: 100px; background-color: yellow;"> This is the header defined in HelloWorldLayout.jsp </div> <div id="content" style="height: 300px; background-color: white;"> <tiles:insertAttribute name="content"></tiles:insertAttribute> </div> <div id="footer" style="height: 100px; background-color: orange;"> This is the footer defined in HelloWorldLayout.jsp </div> </body> <html>
Please note the Tiles-specifics tags in lines 5 and 12.
Then, also under WebContent create another file called HelloWorldTile.jsp and make sure it looks like:
<h1>Hello World! - Tiles version <br/> This is defined in HelloWorldTile.jsp</h1>
Now, under WebContent/WEB-INF create a file called tiles.xml and make sure it looks like:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">