Вы находитесь на странице: 1из 5

Developing Struts 2 applications using Eclipse and Tomcat in Windows http://www.struts2.org/tutorial/developing-struts-2-applications-using-ecl...

Home
Tutorials
About
Subscribe

Struts2 is a powerful Web application framework for building reliable and maintainable enterprise applications. It provides an
MVC pattern of building applications which promotes reuse and separation of concerns. Some of the key features of Struts 2
include convention over configuration, plugin support and dependency injection. This article provides step by step
instructions for starting Struts2 Web application development using Eclipse and Tomcat server in a Windows machine.

For developing Web applications using Struts2 framework you need the following software packages. All these projects are
open source projects and can be downloaded free of cost including source code. Please note that the version numbers
mentioned below are my recommendations and are NOT the minimum requirements for Struts2 applications,

Java Development Kit 1.6 or above [Download Link | Project Home] – 75MB+
Eclipse 3.6 (Helios – Eclipse IDE for Java EE Developers) or above [Download Link | Project Home] – 200MB+
Tomcat 6.0 or above [Download Link | Project Home] – 6MB+
Struts2 full distribution 2.2.1 or above [Download Link | Project Home] – 110MB+
Javassist library 3.14 [Download Link | Project Home] – 2MB+

Installing Java compiler and runtime is as simple as double clicking on the JDK executable you have downloaded. Just accept
the default installation options and Java suite will be installed under the Program Files\Java folder. You can verify the
installation by running the following command on the Windows command line,

java -version

Eclipse doesn’t come with a Windows installer. To use Eclipse, you just need to extract the contents of the downloaded zip
file somewhere. To run Eclipse IDE, double click on the eclipse.exe located inside the eclipse folder. When you run Eclipse,
it will ask for a Workspace folder. This is the folder where all your projects will be created.

Eclipse is a powerful IDE for Java development and if properly used, it can substantially reduce coding effort. Please see
Eclipse tips and tricks page for various productivity features available. Eclipse IDE for Java EE developers contains built in
support for various J2EE technologies and also provides application server integration.

Eclipse IDE unlike Netbeans doesn’t come with an application server. Hence to develop and test Web applications, you need

1 of 5 3/28/2011 6:25 AM
Developing Struts 2 applications using Eclipse and Tomcat in Windows http://www.struts2.org/tutorial/developing-struts-2-applications-using-ecl...

to get a Web application server such as Tomcat or JBoss. If you don’t need EJBs, tomcat is the best choice.

To install Tomcat, download tomcat zip file and extract to a folder in your machine. Eclipse provides excellent tomcat
integration.

Start Eclipse IDE and select the menu Window => Show View => Servers. This will open the servers tab which lists all the
servers integrated in Eclipse. Right click on the tab to add a new server configuration. From the Define New Server screen,
select Apache => Tomcat v6.0 Server,

Click on Next. In the Tomcat Server configuration screen, click on browse to select the Tomcat folder you have installed in
Step 3. In the JRE section, select the JDK 1.6 you have installed in Step 1. Click on Finish to complete the Tomcat
configuration.

Now we have the Eclipse development environment ready for building Struts 2 web applications.

2 of 5 3/28/2011 6:25 AM
Developing Struts 2 applications using Eclipse and Tomcat in Windows http://www.struts2.org/tutorial/developing-struts-2-applications-using-ecl...

A Struts 2 application is just like any other Web application in Eclipse. The only difference is that it requires a set of jar files
and configuration files as part of the Web application.

From the Eclipse menu select File => New => Project => Web => Dynamic Web Project
and click on Next. Enter the project name as HelloWorld and then click on Finish. We now
have an empty Web application. The screenshot on the right shows the structure of this Web
application in Eclipse. Right click on HelloWorld project to run it on the Tomcat server. You
will see a 404 error page since we have an empty Web application.

Now we need to add the Struts2 jar files required for this application. Since this is a very
simple application, we will only add the minimum set of jars required for a Struts2
application.

Download Struts2 distribution zip (struts-2.2.1-all.zip) and then extract the contents to a
temporary folder. From the lib subfolder of the extracted Struts2 folder copy the following
jars to the WEB-INF/lib folder of HelloWorld project.

commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
freemarker-2.3.16.jar
ognl-3.0.jar
struts2-core-2.2.1.jar
xwork-core-2.2.1.jar

This is the bare minimum set of jars required for building a Struts2 application.

In addition to the above jars, Struts 2.2.1 onwards Javassist jar file is also required.
Download and extract the javassist zip file and then copy the javassist jar file to
WEB-INF/lib. The javassist-3.14.0-GA.zip file contains javassist.jar file which I
renamed to javassist-3.14.0.jar before adding to the project. Please note that in the
case of JBoss server, Javassist is not required since it is available on the server.

Now we need to modify the web.xml file so that Struts2 framework is plugged into the
application. Add the following lines to web.xml,

1 <filter>
2 <filter-name>struts2</filter-name>
3 <filter-
class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-
class>
4 </filter>
5
6 <filter-mapping>
7 <filter-name>struts2</filter-name>
8 <url-pattern>/*</url-pattern>
9 </filter-mapping>

The above configuration enables routing of all URL requests via the Struts2
framework.

Now we will write a simple Hello World application which will output
"Hello World" on the browser window. First let us create an action class
which will handle user request and will forward to the appropriate view.
From Eclipse, create a new class file. Give a package name
(org.struts2.samples) and select the superclass as
"com.opensymphony.xwork2.ActionSupport". Click on finish to create the
controller.

3 of 5 3/28/2011 6:25 AM
Developing Struts 2 applications using Eclipse and Tomcat in Windows http://www.struts2.org/tutorial/developing-struts-2-applications-using-ecl...

Type in the following code to the HelloWorld.java,

01 package org.struts2.samples;
02
03 import com.opensymphony.xwork2.ActionSupport;
04
05 public class HelloWorld extends ActionSupport {
06
07 @Override
08 public String execute() throws Exception {
09 return SUCCESS;
10 }
11
12 }

The return value (SUCCESS) indicates the name of the view to which the request should be forwarded.

Now we need to create a Struts2 meta data file named struts.xml inside the src folder. This file specifies how user requests
are mapped to action classes and also how action return values are mapped to view implementations. Another approach
available in Struts 2 is the use of annotations.

Create struts.xml in "src" folder and add the following lines,

01 <?xml version="1.0" encoding="UTF-8"?>


02 <!DOCTYPE struts PUBLIC
03 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04 "http://struts.apache.org/dtds/struts-2.0.dtd">
05
06 <struts>
07 <package name="sample" extends="struts-default">
08 <action name="index" class="org.struts2.samples.HelloWorld">
09 <result name="success">/index.jsp</result>
10 </action>
11 </package>
12 </struts>

Finally let us create a simple view implementation as a jsp. Create index.jsp inside WebContent and add the following lines,

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

We now have all the pieces of the sample Struts 2 MVC application ready as given in the following table,

Controller (action class) org.struts2.samples.HelloWorld.java


View WebContent/index.jsp
MVC configuration src/struts.xml
Enabling Struts2 WEB-INF/web.xml

Right click on the HelloWorld project and select Run As => Run on
Server. To access the application, type in the following URL in a browser

4 of 5 3/28/2011 6:25 AM
Developing Struts 2 applications using Eclipse and Tomcat in Windows http://www.struts2.org/tutorial/developing-struts-2-applications-using-ecl...

window,

http://localhost:8080/HelloWorld/index.action

Sample Application – How it works?

User accesses http://localhost:8080/HelloWorld/index.action


The filter configured in web.xml routes the request to Struts2.
Struts2 looks at struts.xml and finds a match for "index" (it omits the
extension .action) as HelloWorld.
Struts2 calls the execute() method of HelloWorld.
HelloWorld returns the value "success". Struts2 looks for the mapping of "success" in struts.xml and finds index.jsp.
Index.jsp is executed and its result is returned to the user.

You can download the complete source code of the sample application from here. Please note that to keep the download size
small, I have removed all the jar files from WEB-INF/lib. You need to add them before running the application.

Leave a Comment

Name (required)

Mail (will not be published) (required)

Website

Struts2 development using NetBeans


Struts2 development using Eclipse and Tomcat

Copyright © www.struts2.org | Privacy Policy

5 of 5 3/28/2011 6:25 AM

Вам также может понравиться