Struts 2 Maven Hello World

Here you will see how to create Struts 2 Maven Hello World first application using eclipse tomcat and maven.

Note: To create Struts 2 Hello World eclipse project please use this tutorial

Tools needed:

  • Eclipse Kepler (Download eclipse from here) : We are using eclipse Kepler because it comes with in-built maven plug-in so create maven project would be easy
  • JDK 1.6 or above (Download from here)
  • Tomcat 6 or above (Please follow link to install and configure tomcat in eclipse)
  • Maven 3.0.4

Please follow below steps:

  • Open your eclipse
  • Create Maven project name: StrutsMavenHelloWorld (If you are not sure how to create maven project in eclipse please use this tutorial)
  • Final project structure:

Struts 2 Maven Hello World

  • We need only one artifactId in pom.xml file that includes all dependent jars for us:
<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.javahonk</groupId>
    <artifactId>StrutsMavenHelloWorld</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>StrutsMavenHelloWorld Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>      
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.16.3</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>StrutsMavenHelloWorld</finalName>
    </build>
</project>

  • Just for information maven included below jars in class-path:

Struts 2 Maven Hello World

  • web.xml file:
<!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>struts2Maven</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2Maven</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
</web-app>

  • Please create jsp folder inside \webapp\WEB-INF\
  • Create index.jsp inside jsp folder
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Struts 2 Maven Hello world!!!</title>
        <s:head/>
    </head>
 
    <body>
        <h1 style="color: red"><s:text name="label.welcome" /></h1>
        <s:if test="hasActionErrors()">
            <div id="fieldErrors">
                <s:actionerror/>
            </div>
        </s:if>
        
        <s:form action="strutsAction" namespace="/" method="post" name="strutsForm">
            <s:textfield name="firstName" size="30" maxlength="50" key="label.firstName"/>
            <s:textfield name="lastName" size="30" maxlength="50" key="label.lastName"/>
            <s:textfield name="location" size="30" maxlength="50" key="label.location"/>            
            <s:submit key="submit" align="right"/>
        </s:form>
    </body> 
</html>  
  • Create success.jsp inside jsp folder:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Struts 2 Maven Welcome page!</title>
    </head>
    <body>
        <h2 style="color: red"><s:property value="message"/></h2>
        <p>
            First Name: <s:property value="firstName" />
        </p>
        <p>
            Last Name: <s:property value="lastName" />
        </p>
        <p>
            Location: <s:property value="location" />
        </p>
    </body>
</html>
  • Create ApplicationResources.properties file inside resource folder and copy paste below key value:
label.welcome = Struts 2 Maven Hello World!!!
label.firstName =  First Name
label.lastName =  Last Name
label.location =  Location
error.firstName.required = First Name is required!
error.lastName.required = Last Name is required!
error.location.required = Location is required!
  • Now create struts.xml inside resource folder and copy below code:
<?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>
    <include file="struts-default.xml"/>
    
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />
    
    <package name="default" extends="struts-default">
        <action name="">
            <result>/WEB-INF/jsp/index.jsp</result>
        </action>
       <action name="strutsAction" class="com.javahonk.action.StrutsAction">
          <result name="error">/WEB-INF/jsp/index.jsp</result>
          <result name="input">/WEB-INF/jsp/index.jsp</result>
          <result name="success">/WEB-INF/jsp/success.jsp</result>          
       </action>
    </package>
</struts>
  • Finally create action class StrutsAction.java inside com.javahonk.action package:
package com.javahonk.action;

import com.opensymphony.xwork2.ActionSupport;

public class StrutsAction extends ActionSupport {
    
    private static final long serialVersionUID = 1L;
    private String firstName;
    private String lastName;
    private String location;
    private String message;
    

    @Override
    public String execute() throws Exception {
        message = "Your entered input values are:";
        return ActionSupport.SUCCESS;
    }
    
    @Override
    public void validate() {
        if (null == firstName || firstName.length() == 0)
            addActionError(getText("error.firstName.required"));
        if (null == lastName || lastName.length() == 0)
            addActionError(getText("error.lastName.required"));
        if (null == location || location.length() == 0)
            addActionError(getText("error.location.required"));
    }

    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 getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getMessage() {
        return message;
    }

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

  • Now we are ready to run this project.To run this project right click project –> Click Run As –> Run on Server as below: (Note: Before run this application you will have to configure tomcat server in eclipse. Please follow link to install and configure tomcat in eclipse)

Struts 2 Hello World

 

 

  • You will see below welcome page:

Struts 2 Maven Hello World

  • To check if validation is working properly click Submit button without entering any data. You will see below validation message:

Struts 2 Maven Hello World

  • Now enter some data and hit Submit button success page will be come up:

Struts 2 Maven Hello World

download2 Download Project: StrutsMavenHelloWorld

That’s it Struts 2 Maven Hello World

One thought on “Struts 2 Maven Hello World”
  1. After visiting a number of websites, where the projects given were not working as promised.

    Finally at your page, i found this project and it was working….

    I dont know what was wrong with the other websites, but I was always getting 404, though their authors also said, that it will work…:(

    Thanks for creating this page

Leave a Reply

Your email address will not be published. Required fields are marked *