Thursday, August 15, 2013

Configuring Eclipse's Dynamic Web project for Struts 2 with Maven and deploying on Tomcat 7.

This post will introduce you to creating a new Struts 2 dynamic web application to run on Tomcat 7. 

The motivation behind this post was, that I was looking for a archetype to create a web app that could be configured on Eclipse as a Maven project in Facets form and could be run directly on Eclipse using Tomcat 7 configured on eclipse. This might seem very simple to many, but lot of people get confused in setting up project in facets form, which can be bundled using maven in a way that it can be easily deployed on tomcat.

If you intend to create a struts web app that you would want to run on jetty server for development then you can use following archetypes to create sample applications. 


  • struts2-archetype-convention
  • struts2-archetype-blank
Once created you can run these applications using mvn jetty:run command.

Now back to creating a struts 2 based dynamic web application on Eclipse, that will run on Tomcat 7. Before starting with the development, you will need following software configured or downloaded.

  1. Eclipse IDE
  2. Tomcat 7 or 6 configured in Eclipse IDE.
  3. Maven 3 on Eclipse
  4. jdk 6 or 7


We start with creating a new Dynamic Web project in Eclipse

Create a new dynamic web project, New->New Dynamic Web Project in eclipse



Click next and again click next. On Web Module section, click the checkbox for "Generate web.xml dd"



Now click Finish button.

Now the transformation of the dynamic web application to a struts 2 application that could be bundled using maven and run on Tomcat starts.

Right click on the project and scroll to Configure, then click on Convert to Maven Project... link.



Enter the groupid, artifact id and select the packaging as war. Then click Finish button.

Now your project has been converted to a maven project. You could achieve up to this stage by running a struts 2 archetype. But I wanted to explain you this, in case you wanted to setup a web project from scratch.


If you check in your project properties, Project Facets section - you will find at the project is in facets form with Dynamic Web Module 3.0, Java 1.6 & JavaScript 1.0 selected.






 If you would have used an archetype to create the maven project then you would have to convert the project into Facets form from this section. That would have been an additional step.

Now add following dependencies in your pom.xml



<properties>

<struts2.version>2.3.8</struts2.version>

</properties>

<dependencies>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-config-browser-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>${struts2.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
</dependencies>


Now we create required folders, so that everything could be bundled properly.

Right click on Java Resources in your project and create following new Source Folder

  • src/main/java
  • src/main/resources



Delete existing WebContent folder

Scroll to src folder, you will find src/main with java & resources folders. Create new webapp folder.

Now your folder structures in Eclipse should look exactly like this.




Previous step is important, because that is the structure of the project that we want to achieve. So make sure your project structure exactly matches this.

Your Eclipse Deployment Assembly should look like this:-


Now we start configuring the project so that our struts 2 application can run.

Create a WEB-INF folder inside webapp, and create a web.xml file. Content of your web.xml file will be as follows:-

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

<web-app id="struts_blank" version="2.4"

         xmlns="http://java.sun.com/xml/ns/j2ee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>Struts Blank</display-name>

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

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

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>


As you see that I have added struts 2 filter and filter mapping. This is required to run the struts2 application.


Now try running maven build, run mvn build or install from eclipse. You will need internet connection so that maven can download these dependencies from repository.This will package your empty project as war and populate target folder.


If you are getting an error like - Cannot nest 'Project/src/main/java' inside library 'Project/src

Then go to org.eclipse.wst.common.component,xml file in .settings of your project (visible from Navigator view) and make sure the contents of the file look like this. Otherwise update.


<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0"> <wb-module deploy-name="strutsapplication"> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/> <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>/> <property name="context-root" value="strutsapplication" />/> <property name="java-output-path" value="/strutsapplication/target/classes"/> </wb-module> </project-modules>


Now your project is ready for creating a sample web application using struts 2 to run on Tomcat 7. Check my new blog http://javaredhot.blogspot.com/2013/08/creating-sample-struts-2-web-application.html for the same.

Creating sample struts 2 web application

No comments:

Post a Comment