Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

Tags: maven struts
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
JDK 7
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

StrutsMavenProject\pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.beingjavaguys.strutssample</groupId>
<artifactId>StrutsMavenProject</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>StrutsMavenProject</name>
<url>http://maven.apache.org</url>
<properties>
<struts.version>2.3.16.3</struts.version>
<jdk.version>1.7</jdk.version>
<context.path>StrutsMavenProject</context.path>
</properties>
<developers>

<developer>
<id>Nagesh Chauhan</id>
<email>[email protected]</email>
<organization>beingjavaguys.com</organization>
<organizationUrl>http://www.beingjavaguys.com</organizationUrl>
<roles>
<role>Java Developer</role>
</roles>
<timezone>+5:30</timezone>
</developer>
</developers>

<build>
<finalName>${pom.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
</dependencies>
</project>


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:

\StrutsMavenProject\src\main\webapp\WEB-INF\web.xml

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="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>Struts Maven Project</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>

</web-app>

\src\main\resources\struts.xml

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

<struts>
<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>
<action name="register" class="com.beingjavaguys.actions.HomeAction"
method="getDetail">
<result name="success">WEB-INF/pages/details.jsp</result>
</action>
</package>

</struts>


\src\main\java\com\beingjavaguys\actions\HomeAction.java

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) {
this.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) {
this.email = email;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String execute() {

return "SUCCESS";

}
public String getDetail(){
return SUCCESS;
}

}


\src\main\webapp\WEB-INF\pages\form.jsp


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

<html>
<head>
<title>Being Java Guys | Struts Form</title>
</head>
<body>
<center>

<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 />
</form:form>
</center>
</body>
</html>



\src\main\webapp\WEB-INF\pages\details.jsp


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

<html>
<head>
<title>Being Java Guys | Struts Details</title>
</head>
<body>
<center>

<h2>Being Java Guys | User Details</h2>
<br /> <br />
<table>
<tr>
<td>First Name:</td>
<td><s:property value="firstName" />
</td>
</tr>
<tr>
<td>Last Name:</td>
<td><s:property value="lastName" />
</td>
</tr>
<tr>
<td>Email:</td>
<td><s:property value="email" />
</td>
</tr>
<tr>
<td>Phone:</td>
<td><s:property value="phone" />
</td>
</tr>
</table>
</center>
</body>
</html>



\src\main\webapp\index.jsp



<%
response.sendRedirect("form");
%>
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>



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"




This post first appeared on Java, Struts 2, Spring, Hibernate, Solr, Mahout An, please read the originial post: here

Share the post

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

×

Subscribe to Java, Struts 2, Spring, Hibernate, Solr, Mahout An

Get updates delivered right to your inbox!

Thank you for your subscription

×