Can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

How To Use Resource Bundle In Struts2

Struts » On Oct 23, 2011 | { 3 Comments }

In 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

 

 

 

What you are thinkig....

3 Responses to “How To Use Resource Bundle In Struts2”
  1. Mohammed Vaseem says:

    Hello Java4s,
    How to use an application with multiple .properties file?
    When I tried to have it, Struts is considering only the first file which i specified in struts.properties file.
    Please help
    Thanks in Advance
    Mohammed Vaseem

  2. Java4s says:

    @Vaseem

    We can write multiple resource bundles specified by providing a comma, some thing like..

    struts.custom.i18n.resources=java4s,java4s2,java4s3…..bla bla

    Hope you will clear.

  3. Kranthi says:

    Hi Java4s,

    I have a requirement to display country related states in the drop-down list of Struts2 tags with out using AJAX/JQuery client side code.
    By using struts 2 tags with out refreshing the whole page we need to display cities drop-down list.

    Please help me in solving the above requirement.

    Thanks in Advance
    Kranthi Kumar

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Jsps,Web Services
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.