Struts2 Tutorial for Beginners (XML Config)

View more Tutorials:

1- Introduction

This document was written based on:
  • Eclipse 4.6
  • Struts 2 (2.3.20)
You are viewing "Hello World Struts2 - XML Example". You can view the same example using the Annotation at:

2- Create Maven Project

  • File/New/Other..
  • Group ID: org.o7planning
  • Artifact ID: Struts2XML
  • Package: org.o7planning.tutorial.struts2xml
Do not worry about the error message when the Project has been created. The reason is that you do not declare the Servlet library.
Eclipse create Maven project structure may be wrong. You need to fix
This is the image after Project Completion:

3- Configuring Maven

Configuring Maven to declare the library used. Includes library Servlet, Struts2. Also configure Tomcat Maven Plugin for running web applications directly on Eclipse.
<project xmlns=""

    <name>Struts2XML Maven Webapp</name>


        <!-- Servlet Library -->
        <!-- -->

        <!-- Jstl for jsp page -->
        <!-- -->

        <!-- JSP API -->
        <!-- -->

        <!-- -->



            <!-- Config: Maven Tomcat Plugin -->
            <!-- -->
                <!-- Config: contextPath and Port (Default: /Struts2XML : 8080) -->
Now your project has no error message.

4- Configuring Struts & web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
 id="WebApp_ID" version="3.0">





struts.xml is a resource file, it should be placed in src/main/resources, this file is used to configure the struts.
<?xml version="1.0" encoding="UTF-8" ?>
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   <constant name="struts.enable.DynamicMethodInvocation"
       value="false" />
   <constant name="struts.devMode" value="true" />
   <constant name="struts.custom.i18n.resources"
       value="ApplicationResources" />
   <package name="default" namespace="/" extends="struts-default">
       <action name="login"
           <result name="success" type="redirect">/userInfo</result>
           <result name="error">/WEB-INF/pages/Login.jsp</result>
       <action name="userInfo"
           <result name="userInfoPage">/WEB-INF/pages/UserInfo.jsp</result>

5- Code Project

Create file in src:
label.username= Username
label.password= Password
label.login= Login
error.login= Invalid Username/Password. Please try again.
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<title>Struts 2 - Login Application </title>

<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<s:form action="/login" method="post">
  <s:textfield name="username" key="label.username" size="20" />
  <s:password name="password" key="label.password" size="20" />
  <s:submit method="execute" key="label.login" align="center" />

Username: admin, password: admin123

<%@ page contentType="text/html; charset=UTF-8"%>

<title>User Info</title>
    <h2>Hello, ${loginedUsername}...!</h2>
package org.o7planning.tutorial.struts2xml.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 7299264265184515893L;

	private String username;
	private String password;

	public String execute() {

		HttpServletRequest request = ServletActionContext.getRequest();

		if (this.username != null && this.password != null && this.username.equals("admin")
				&& this.password.equals("admin123")) {

			HttpSession session = request.getSession();
			// Store userName in session
			session.setAttribute("loginedUsername", this.username);

			return "success";
		} else {
			return "error";

	public String getUsername() {
		return username;

	public void setUsername(String username) {
		this.username = username;

	public String getPassword() {
		return password;

	public void setPassword(String password) {
		this.password = password;
package org.o7planning.tutorial.struts2xml.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserInfoAction extends ActionSupport {

    private static final long serialVersionUID = 7299264265184515893L;
    public String execute() {
        return "userInfoPage";


6- The flow of the program

The illustration below depicts the flow of the user program from accessing the website page to receive response

7- Running application

To run the application directly on Eclipse, you need to configure to run  Tomcat Maven Plugin.
  • Name: Run Struts2XML
  • Base Directory: ${workspace_loc:/Struts2XML}
  • Goals: tomcat7:run
Tomcat Maven Plugin is running
In the browser, enter the path:
If you entered an incorrect username/password, page will be displayed as shown below:

View more Tutorials: