Pages

Wednesday, 11 September 2013

How To Use Resource Bundle In Struts2

struts2, if we want to get labels and error messages from an external file then we need to use resource bundle.  The resource bundle in struts 2 is similar to struts 1 but the difference is,  in struts 1 we need to configure the resource bundle name in struts-config.xml, but in 2.x we need to configure our bundle name in struts.properties file. Remember:
Both our Bundle, and struts.properties files need to be store in classes folder only

Syntax of struts.properties

struts.custom.i18n.resources= your bundle name
Example:
struts.custom.i18n.resources=java4s
(Let my bundle name being java4s)

Syntax of java4s.properties

enter.user   =  User name
enter.pass   =  Password
user.wrong =  You must enter the user
pass.wrong =  Password should be entered
u.p.wrong   =  User or Password is wrong
something like key = value
In action class if any logic is failed then also we get error messages from a bundel.  At this time we need to make it as an actionError, not fieldError.  If we want to display an action error on a browser then in jsp we need to use the tag called <s:actionerror>
We will see this in the next example with programmatic validations

No comments:

Post a Comment