View RSS Feed

My Java Tips

Setup a Struts 2 application

Rate this Entry
by , 11-15-2011 at 06:08 PM (1015 Views)
in this post, I will write about how to setup a Struts 2 application from scratch. This will be interesting for the developer who are using Struts for the very first time.


Download the latest struts framework from the http://struts.apache.org. To get started with a new application you can setup a web infrastructure from scratch.

Setup the Web Application File Structure as follows within webapps folder. (This tutorial assumes that you are using Apache Tomcat as a web server, and webapps folder is the folder within Tomcat folder).
Java Code:
MyStrutsFolder
MyStrutsFolderMETA-INF
MyStrutsFolderWEB-INF
MyStrutsFolderWEB-INFclasses
MyStrutsFolderWEB-INFlib
MyStrutsFolderWEB-INFlibminimum JARs + any plugin JARs + plugin dependencies
MyStrutsFolderWEB-INFweb.xml
The following files are a minimum requirement for your application.

Copy to your webapps/lib directory the required JARs from struts framework folder (say \struts-2.0.11-all\struts-2.0.11\lib):

struts2-core.jar
xwork.jar
ognl.jar
freemarker.jar
commons-logging.jar

and any Struts plugin JARs or any plugin dependencies.

Create an web.xml file in <webapp>\WEB-INF as:</webapp>
XML Code:
<!--?xml version="1.0"?-->
"http://java.sun.com/dtd/web-app_2_3.dtd"&gt;

<web-app>
  <display-name>My Application</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>
Create a skeleton struts.xml file in \WEB-INF\classes as .
XML Code:
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;

<struts><!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
         ...
	</package>
</struts>
This is sample setup for your struts application.

Submit "Setup a Struts 2 application" to Facebook Submit "Setup a Struts 2 application" to Digg Submit "Setup a Struts 2 application" to del.icio.us Submit "Setup a Struts 2 application" to StumbleUpon Submit "Setup a Struts 2 application" to Google

Comments