Struts2 actionError and actionMessage | Code Factory

Donate : Link

Medium Blog : Link

Applications : Link

Download Code and Jars : Link

File : index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    <%@ taglib prefix="s" uri="/struts-tags"%>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
            <title>Index page</title>
                <s:if test="hasActionErrors()">
                        <span style="text-align: center; color: red; font-size: 17px;"><s:actionerror /></span>
                <s:if test="hasActionMessages()">
                        <span style="text-align: center; color: green; font-size: 17px;"><s:actionmessage /></span>
                <form action="checkName" method="post">
                    <input type="text" name="firstName">
                    <input type="submit" value="Submit">

File : struts.xml

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        <constant name="struts.devMode" value="true" />
        <include file="struts-default.xml" />
        <package name="actionErrorActionMessage" extends="struts-default">
            <action name="checkName" class="com.codeFactory.checkNameAction">
                <result name="success">index.jsp</result>

File : web.xml

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

File :

package com.codeFactory;

import com.opensymphony.xwork2.ActionSupport;

public class checkNameAction extends ActionSupport {

    private String firstName;

    public String execute()
        if (getFirstName().equals("admin"))
            addActionMessage("Welcome admin.");
        } else
            addActionError("Wrong input.");
        return SUCCESS;

    public String getFirstName() {
        return firstName;

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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s