Struts 2 Annotation Message Resource configuration

Struts 2 Annotation Message Resource configuration

If you are developing application using struts 2 framework with annotation and want to configure message resource properties file. Please follow steps below:

There are two ways you could add message resource configuration to your struts application:

  • Global property file
  • Action class specific property file

To support internationalization(i18n) append property file name with related country locale id. We will append locale id en with our property file to support English language.

Struts framework first search action specific property file in class path if found then reads value from it otherwise its uses global property file. We have created below property file to support global and action specific properties for this demo with below name:

  • Global property file —
  • Action class specific property file —


  • Please create maven project name: Struts2FormValidation. Below is final project structure:

Struts 2 Annotation Message Resource configuration

  • pom.xml file:
<project xmlns="" xmlns:xsi=""
  <name>Struts2FormValidation Maven Webapp</name>

error.firstName.required = First Name is required!
error.lastName.required = Last Name is required! = Email Id is required! = Please enter valid email!
label.welcome = Struts 2 Annotation Message Resource configuration
label.firstName =  First Name
label.lastName =  Last Name =  Email Id

error.firstName.required = First Name is required (!
error.lastName.required = Last Name is required (! = Email Id is required (! = Please enter valid email (!

package com.javahonk.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;

        @Result(name = "error", location = "/index.jsp"),
        @Result(name = "input", location = "/index.jsp")})
public class FormValidationAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private String firstName;
    private String lastName;
    private String email;

    @Action(value = "/formValidationAction")
    public String execute() throws Exception {
        return ActionSupport.INPUT;

    public String getFirstName() {
        return firstName;

    @RequiredStringValidator(key = "error.firstName.required")
    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    @RequiredStringValidator(key = "error.lastName.required")
    public void setLastName(String lastName) {
        this.lastName = lastName;

    public String getEmail() {
        return email;

    @RequiredStringValidator(key = "")
    @EmailValidator(key = "")
    public void setEmail(String email) { = email;

  • web.xml:
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

    <display-name>Archetype Created Web Application</display-name>



  • index.jsp:
<%@ 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" "">
        <title>Struts 2 Annotation Message Resource configuration</title>
        <h1 style="color: green"><s:text name="label.welcome" /></h1>
        <s:if test="hasActionErrors()">
            <div id="fieldErrors">
        <s:form action="formValidationAction" 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="email" size="30" maxlength="50" key=""/>            
            <s:submit type="button" key="submit" label="Submit" align="right"/>            
  • Configure tomcat server to your eclipse using this tutorial. To this project : Right click project –> Run As –> Run on Server. You will below page:

Struts 2 Annotation Message Resource configuration

  • Submit this form without entering any value you will see below validation message whose mapping done in property file:

Struts 2 Annotation Message Resource configuration

For more information please struts 2 tutorial

download2 Download Project: Struts2FormValidation

Leave a Reply

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

I am not Robot *