Home  >  Struts 2

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 UserAction_en.properties 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.
UserAction_en.properties
user.name = Name.
user.email = Email
user.age = Age
user.name.req = Enter your Name.
user.email.req = Enter your Email
user.age.req = Enter your Age
user.email.valid = 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.
ResultAction.java
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;
@Namespace("/user")
@ResultPath(value="/")
@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; 
	}
        @RequiredStringValidator(key="user.name.req")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@RequiredStringValidator(key="user.email.req")
        @EmailValidator(key ="user.email.valid")
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@RequiredStringValidator(key="user.age.req")
	@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.
user.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h1>Struts 2 i18 Validation Annotation Example</h1>
<s:form action="result" validate="true" >
	<s:textfield name="name" key="user.name"/>
	<s:textfield name="email" key="user.email"/>
	<s:textfield name="age" key="user.age"/>
	<s:submit/>
</s:form>
</body>
</html> 

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
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us