Struts 2 Annotation Message Resource Example

By Arvind Rai, May 10, 2014
In this example we will see how to use Message Resource or Resource Bundle in struts 2 using annotation. Message Resource files is a property file in which we put key value pairs. We create more than one Message Resource file for different locale. In struts 2, message resource files can be global or an action class specific. Struts 2 searches first action class specific if not found then goes to global property file. In our example I have used only action class specific message resource file. We also say it i18n or localization support. To support i18n, all we need to do is change

1. Create a property file for any specific action class like where UserAction is an action class and en denotes for default or English specific locale.
2. Use keys to define messages and label name in JSP and action class.

Property File for Message Resource

I have used some keys and values in our sample Message Resource property file. = Name. = Email
user.age = Age = Enter your Name. = Enter your Email
user.age.req = Enter your Age = Enter a valid e-mail address.
user.age.valid = Age must be in between 18 and 30 

Annotation based Action class using Message Resource

In the action class we are using keys to provide the validation message.
package com.concretepage.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
@Action(value="result", results={@Result(name="success",location="result.jsp")})
public class ResultAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private String name;
	private String email;
	private int age;
	public String execute() {
	   return SUCCESS; 
	public String getName() {
		return name;
	public void setName(String name) { = name;
        @EmailValidator(key ="")
	public String getEmail() {
		return email;
	public void setEmail(String email) { = email;
	@IntRangeFieldValidator(key ="user.age.valid",   min = "18", max = "30")
	public int getAge() {
		return age;
	public void setAge(int age) {
		this.age = age;

JSP using Message Resource

This JSP is a simple User form. Every input field contains key attribute that will pick label name from Message Resource file.
<%@ taglib prefix="s" uri="/struts-tags" %>
<h1>Struts 2 i18 Validation Annotation Example</h1>
<s:form action="result" validate="true" >
	<s:textfield name="name" key=""/>
	<s:textfield name="email" key=""/>
	<s:textfield name="age" key="user.age"/>

Download Complete code

Eclipse Structure of Code

Find the eclipse structure for annotation based struts 2 example using message resource.
Struts 2 Annotation   Message Resource Example

Output Screen

Run example by http://localhost:8080/Struts2Demo-1/user/user and test it.
Struts 2 Annotation   Message Resource Example

©2024 | Privacy Policy | Contact Us