Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects

Wednesday, 4 June 2014

Create Struts2 dynamic web project with Maven and make it support Eclipse IDE

In this particular blog we will see how to setup a sample struts2 project with maven. Struts2 is a widely used web application framework for java enterprise applications and maven is an effective build tool to manage dependencies and build the applications in java.

Before we start lets make sure we have all required tools available :
Maven 3.0.5
Eclipse 4.2
Struts 2.X Release
Tomcat 7

Make sure you have a working installation of Maven in your machine, to check this supply following command to your command line:

C:\Users\nagesh.chauhan>mvn -version

Create Dynamic Web Project in Maven

To create dynamic web project with maven, navigate to the folder where you want to store the project and supply following command:

C:\Users\nagesh.chauhan\maven-project>mvn archetype:generate -DgroupId=com.beingjavaguys.sample -DartifactId=StrutsMavenProject -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

What is groupId in maven ?

groupId identifies a particular project uniquely across all projects, so we should follow an naming convention. A very simple and commonly used way of doing this is to use reverse of your domain, i.e. com.beingjavaguys.maven.

A good way of maintaining the integrity of groupId is to use the project structure. In case the project is consists of multiple modules than every module should append an identifier to the parent groupId. i.e. com.beingjavaguys.maven, com.beingjavaguys.spring, com.beingjavaguys.struts .. etc.

What is artifactId in maven ?

artifactId is the name of war file without version, if you are creating it by yourself you are free to took any name of your choice in lower case and without any strange symbol. But if this is a third party jar than we have to take the name of jar as suggested by it’s distribution.

What is archetype in maven ?

Archetype is a Maven project templating toolkit which tells the maven the type of project we are going to create. Archetype enables the maven to create a template project of user’s choice so that the user can get the project up and running instantly.

“archetype:generate”  generates a new project from provided archetype or update the actual project if using a partial archetype. Maven provides a number of predefined archtypes, see more details from Maven Documentation.

What is archetypeArtifactId in maven ?

While creating a new project we provide the archetypeArtifactId that informs maven about what archetype to use to create the initial structure of the project. Maven looks it up from the archetypeCatalog and works accordingly. eg. if we want to create a simple web-app project we specify -DarchetypeArtifactId=maven-archetype-webapp.

Convert Maven project to support Eclipse IDE

Here we are done with creating a dynamic web project in maven, now lets make this project compatible to Eclipse IDE. To make maven project support eclipse ide navigate to project folder and supply following command :

C:\Users\nagesh.chauhan\maven-project\StrutsMavenProject>mvn eclipse:eclipse -Dwtpversion=2.0

Now Import the project into eclipse, you will see a folder structure like this :

Create Struts2 project in Eclipse using Maven

Now we are done with creating a simple dynamic web project in maven and imported it in eclipse. Lets add some required files to it to make it a Struts2 MVC project.

Update pom.xml

<project xmlns="" xmlns:xsi=""

   <id>Nagesh Chauhan</id>
    <role>Java Developer</role>


Now to make the project download all required jars and include them in eclipse, run following command in project directory again :

C:\Users\nagesh.chauhan\maven-project\SampleSpringMaven>mvn eclipse:eclipse -Dwtpversion=2.0

this will add all required jars to eclipse's deployment assembly, as shown in the figure below:

Here we are all done with importing a working struts2 mvc maven project in eclipse. Now lets add some source code to our project to make it useful:


This is simple web.xml file, we have added struts specific filter entry here followed by a url mapping entry. This tells the container that all upcoming requests are going to be handles by struts itself.
<web-app xmlns="" xmlns:xsi=""

 <display-name>Struts Maven Project</display-name>




This is where all struts2 based configurations goes, we have added two actions here for different url patterns and attached respective view pages to them.
<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

 <constant name="struts.devMode" value="true" />
 <package name="user" namespace="/" extends="struts-default">
  <action name="form" class="com.beingjavaguys.actions.HomeAction">
   <result name="SUCCESS">WEB-INF/pages/form.jsp</result>
  <action name="register" class="com.beingjavaguys.actions.HomeAction"
   <result name="success">WEB-INF/pages/details.jsp</result>



This is simple pojo class to represent an action in struts 2, in struts 2 we don't need to extend or implement anything to make a class to be served as action class.
package com.beingjavaguys.actions;

import com.opensymphony.xwork2.Action;

public class HomeAction implements Action {
 private int id;
 private String firstName;
 private String lastName;
 private String email;
 private String phone;

 public int getId() {
  return id;

 public void setId(int id) { = id;

 public String getFirstName() {
  return firstName;

 public void setFirstName(String firstName) {
  this.firstName = firstName;

 public String getLastName() {
  return lastName;

 public void setLastName(String lastName) {
  this.lastName = lastName;

 public String getEmail() {
  return email;

 public void setEmail(String email) { = email;

 public String getPhone() {
  return phone;

 public void setPhone(String phone) { = phone;

 public String execute() {

  return "SUCCESS";

 public String getDetail(){
  return SUCCESS;



<%@ page language="java" contentType="text/html; charset=UTF-8"
<%@ taglib uri="/struts-tags" prefix="form"%>

<title>Being Java Guys | Struts Form</title>

  <h2>Being Java Guys | Registration Form</h2>
  <br /> <br />
  <form:form action="register">
   <form:textfield name="firstName" label="First Name: " />
   <form:textfield name="lastName" label="Last Name: " />
   <form:textfield name="email" label="Email: " />
   <form:textfield name="phone" label="Phone: " />
   <form:submit />


<%@ page language="java" contentType="text/html; charset=UTF-8"
<%@ taglib prefix="s" uri="/struts-tags"%>

<title>Being Java Guys | Struts Details</title>

  <h2>Being Java Guys | User Details</h2>
  <br /> <br />
    <td>First Name:</td>
    <td><s:property value="firstName" />
    <td>Last Name:</td>
    <td><s:property value="lastName" />
    <td><s:property value="email" />
    <td><s:property value="phone" />


 <h2>Hello World!</h2>

Here we are all done with adding all required files to our struts2 maven project, now the complete project structure will look something like this:

Run the project on server (Right Click > Run As > Run on Server), and you will get screens like :

In this particular blog we came across 'how to create a struts2 web project in maven' and 'how to deploy spring maven project war to tomcat' in upcoming blogs we will see more about 'Maven', 'Spring', 'Java' and other opensource technologies.

Thanks for reading !
Being Java Guys Team

Download "Struts2 Maven Hello World Project in Eclipse" from "SkyDrive"


Post a Comment

Like Us on Facebook

Like Us On Google+


Skype: neel4soft