ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Computer Programming Tutorials

How to create dynamic web project using struts2 and Maven in Eclipse IDE or Spring Tool Suite IDE?

Updated on November 24, 2013

In this example, a simple hello program will be implemented using Struts 2. We will use Maven as a build tool in our project.

Full project structure

The full structure of this project looks like the following:

Required tools/ libraries:

For this tutorial, we will use the following tools or libraries:

  1. JDK 1.5 or latest version
  2. Eclipse 4.3.0 or above
  3. Maven 4.0.0 or above
  4. M2Eclipse Plugin

Sometimes, you may need to figure out the version of Eclipse or Maven. You may watch the following videos to confirm that.

How to find Eclipse version

How to find Maven version?

Steps to create dynamic web project using struts2 and Maven in Eclipse IDE or Spring Tool Suite IDE:

Step 1: Creating a new Maven project in Eclipse

Create a new Maven project from File > New > Project.. and select Maven Project into your Eclipse by specifying the following Archetype parameters:

  1. GroupId: com.hubpages.plusminus
  2. AartifactId: MavenDemo
  3. Version: 0.0.1-SNAPSHOT
  4. Package: com.hubpages.plusminus.MavenDemo

Step 2: Add Struts2 dependencies

We will use Maven to download the entire Struts2 dependencies. To do this, just add struts2-core in pom.xml. Add the Struts 2 dependencies by the following information:

  1. Group Id: org.apache.struts
  2. Artificat Id:struts2-core
  3. Version: 2.3.15.3

Full code of pom.xml file [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.hubpages.plusminus</groupId>
	<artifactId>MavenDemo</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>MavenDemo Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.15.3</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>MavenDemo</finalName>
	</build>
</project>

Step 3: Configuring the web.xml

Create a new xml file under src/main/webapp/WEB-INF folder and named it as web.xml. web.xml configuration file is a J2EE configuration file. This file determines how elements of the HTTP request are processed by the servlet container. To make Struts2 working, you must need to configure this file.

Full code of web.xml [web.xml]

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Archetype Created Web Application</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>

Step 4: Create an action class

Actions are the core of the Struts2 framework. Each URL is mapped to a particular action. And in action class, you need to implement those using your own business logic. For this tutorial, we create an action class named as MavenDemo.

Full code of action class [MavenDemo.java]

package com.hubpages.plusminus;

import com.opensymphony.xwork2.ActionSupport;

public class MavenDemo extends ActionSupport{

	private static final long serialVersionUID = 1L;

	public String message = "Hello PlusMinus....";
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String displayMessage(){
		setMessage(message);
		System.out.println(message);
		return SUCCESS;
	}
	
	public String executeAll(){
		displayMessage();
		return SUCCESS;
	}
	
	public static void main (String args[]){
		MavenDemo md = new MavenDemo();
		md.executeAll();
	}
}

Step 5: Creating a view page using JSP

It's time to create a view page. This jsp page will just displays the message of our action class.

Full code of JSP page [index.jsp]

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<body>
<!-- <h2>Hello World!</h2> -->
<h1> Welcome to PlusMinus's home page </h1>
<br />

<s:property value="message"/>

<h1>
----------
<font style="font-family:arial;color:blue;font-size:20px;">Plus</font>
<font style="font-family:arial;color:red;font-size:20px;">Minus</font>
----------
</h1>


<footer>
	<font style="color:green;;">____________________________________________</font> <br />
	<font style="color:green;margin-left:200px;">Copyright © PlusMinus</font>
</footer>

</body>
</html>

Step 6: Configuring the struts.xml [struts.xml]

Each project of Structs2 has a configuration file which is defined in struts.xml file. Create a struts.xml file and configure the file. Configuring the file means, to create all links all action and result of your Java project. Remember, the configuration name must be struts.xml otherwise it will not work.

<?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>
	<package name="basicStruts" extends="struts-default">
		<action name="hello" class="com.hubpages.plusminus.MavenDemo">
			<result name="success">/index.jsp</result>
		</action>
	</package>

</struts>
    

Step 7: Running the struts 2 Maven project

Right-click on the Projct and select Run As option. "Run As" option will be expanded and you will find an option like Run on Server. Choose this option.

struts.xml

Choose the server where you want to run the application. For this tutorial, we choose Tomcat server and we select that. After selecting the server, click on Finish button.

Go to the browser and type http://localhost:8080/MavenDemo/hello into the browser address bar and hit Enter key. You will see the action class output in the browser.

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      mickle 3 years ago

      i like this post i will come back