The Artima Developer Community
Sponsored Link

Web Buzz Forum
HOW-TO: Eclipse + Tomcat + Struts 2 + Tiles

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Lucas Alberione

Posts: 6
Nickname: alberione
Registered: Oct, 2009

Lucas Alberione is Software Engineer based in Boston, MA
HOW-TO: Eclipse + Tomcat + Struts 2 + Tiles Posted: Nov 14, 2009 6:03 PM
Reply to this message Reply

This post originated from an RSS feed registered with Web Buzz by Lucas Alberione.
Original Post: HOW-TO: Eclipse + Tomcat + Struts 2 + Tiles
Feed Title: Techtulia
Feed URL: http://www.techtulia.com/feeds/posts/default
Feed Description: Shamelessly random thoughts on software happenings
Latest Web Buzz Posts
Latest Web Buzz Posts by Lucas Alberione
Latest Posts From Techtulia

Advertisement

Here's a guide that will show how to:

  • 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:



<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

Save the JSP

Deploying and running the application

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:

To test that your application is in fact up and running point your browser to http://localhost:8080/HelloWorld/HelloWorld.jsp. You should see something like this:

Adding Struts 2 support

First, make sure you shutdown Tomcat:


Click on "Servers" tab > right-click on "Tomcat v6.0 Server" > click "Stop"

Add the Struts 2 jar files to the project's build path:


Project Explorer > right-click on "HelloWorld" project node > Build Path > Configure Build Path

Click on "Libraries" tab > click on "Configure Build Path..."

Browse to your Struts 2 installation folder (in my case C:\struts-2.1.8), then go into the lib folder and select the following jar files:

  • commons-beanutils.jar
  • commons-digester.jar
  • commons-fileupload.jar
  • commons-logging.jar
  • freemarker.jar
  • ognl.jar
  • struts2-core.jar
  • struts2-tiles-plugin.jar
  • tiles-api.jar
  • tiles-core.jar
  • tiles-jsp.jar
  • xwork-core.jar

Instruct Eclipse to deploy these jar files in Tomcat:


Project Explorer > right-click on "HelloWorld" project node > Build Path > Configure Build Path

Click on "Libraries" tab > click on "Order and Export" tab

Open the web.xml file located under HelloWorld/WebContent/WEB-INF and edit it so it looks like:



<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>HelloWorld</display-name>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

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">

<struts>
<package name="main" extends="struts-default" >
<action name="HelloWorld" class="HelloWorldAction">
<result name="success">HelloWorld.jsp</result>
</action>
</package>
</struts>

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"

To test that your application is in fact running Struts 2 point your browser to http://localhost:8080/HelloWorld/HelloWorld.action. You should see something like this:

Adding Tiles support

Under WebContent create a file called HelloWorldLayout.jsp and make sure it looks like:

 
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

<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">

<tiles-definitions>
<definition name="helloWorldTileDefinition" template="HelloWorldLayout.jsp">
<put-attribute name="title" value="Hello World!"></put-attribute>
<put-attribute name="content" value="/HelloWorldTile.jsp"></put-attribute>
</definition>
</tiles-definitions>

Please note how lines 9 and 10 relate to lines 5 and 12 of HelloWorldLayout.jsp.

Edit WebContent/WEB-INF/web.xml (more info on web.xml can be found here) and make sure it looks like:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>HelloWorld</display-name>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>

<context-param>
<param-name>tilesDefinitions</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>

</web-app>

Please note how on lines 23 to 26 we instruct Struts 2 to load Tiles definitions from /WEB-INF/tiles.xml.

Finally, edit struts.xml and make sure it looks like:


<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="main" extends="struts-default" >
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
<action name="HelloWorld" class="HelloWorldAction">
<result name="success">HelloWorld.jsp</result>
</action>
<action name="HelloWorldTile" class="HelloWorldAction">
<result name="success" type="tiles">helloWorldTileDefinition</result>
</action>
</package>
</struts>

On line 14, please note that the result type is now tiles meaning that, as a success result, we want to return a Tiles definition rather than a JSP.

Start Tomcat and point your browser to http://localhost:8080/HelloWorld/HelloWorldTile.action, and if everything went well you should see the Tiles version of the Hello World application.

Final words

This completes this tutorial. I hope you could learn something from it. Please let me know if I need to add/correct anything.

Thanks for your time.

Read: HOW-TO: Eclipse + Tomcat + Struts 2 + Tiles

Topic: Fix Windows 7 Update Stopped Working with Fix WU Previous Topic   Next Topic Topic: Customizing Yahoo! Grids CSS, for beginners

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use