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 — ApplicationResources_en.properties
  • Action class specific property file — FormValidationAction_en.properties

Steps:

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

Struts 2 Annotation Message Resource configuration

  • pom.xml file:
<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>Struts2FormValidation</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Struts2FormValidation 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>
        
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.16.3</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>Struts2FormValidation</finalName>
  </build>
</project>

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

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

  • FormValidationAction.java
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;

@Results({
        @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;

    @Override
    @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 = "error.email.required")
    @EmailValidator(key = "error.valid.email.required")
    public void setEmail(String email) {
        this.email = email;
    }   
    
}

  • 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>
        <init-param>
            <param-name>struts.devMode</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>struts.custom.i18n.resources</param-name>
            <param-value>ApplicationResources</param-value>
        </init-param>               
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
</web-app>

  • 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Struts 2 Annotation Message Resource configuration</title>
        <s:head/>
    </head>
 
    <body>
        <h1 style="color: green"><s:text name="label.welcome" /></h1>
        <s:if test="hasActionErrors()">
            <div id="fieldErrors">
                <s:actionerror/>
            </div>
        </s:if>               
        <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="label.email"/>            
            <s:submit type="button" key="submit" label="Submit" align="right"/>            
        </s:form>
    </body> 
</html>  
  • 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 *