Wednesday, December 1, 2010


Message Resources are used in Struts to provide standard messages to be used in JSPages, and Servlets. These messages are stored in files having the extension properties. Message Resources are also used in Internationalizing of applications.We shall touch upon internationalization next. The present topic is the use of Message Resources.
1)Create the resources as property file(extension .properties).
2)Register them in the struts-config.xml file under the heading
The default resource:-

Extra resources require a key

3) Output the messages using




Here are the two property Files
ApplicationResource.properties
# -- standard errors --
champak.message=You are Champak{0}
urdeshmessage=Urdesh is an Adult Now
errors.header=
    errors.prefix=

  • errors.suffix=
  • errors.footer=
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title=Struts Application
welcome.heading=Struts Applications in Netbeans!
welcome.message=It's easy to create Struts applications with NetBeans.


personalproperties.properties
# To change this template, choose Tools | Templates
# and open the template in the editor.
personal.name=Champak
personal.address=Nai Basti,Pandepur
personal.city=Varanasi

The JSP file
Champak.jsp
<%-- Document : Champak Created on : Aug 30, 2010, 3:24:12 PM Author : Champak --%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

<title>Struts Messages</title>

</head>

<body>

<%String s="Roy ";%>

<h1><bean:message key="champak.message" arg0="<%=s%>"/></h1>

<h1><bean:message key="personal.name" bundle="personal"/></h1>

<h2><bean:message key="personal.address" bundle="personal"/></h2>

<h3><bean:message key="personal.city" bundle="personal"/></h3>

</body>

</html>

No comments:

Post a Comment