Announcement Announcement Module
No announcement yet.
resource bundle to read properties from poperty file Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • resource bundle to read properties from poperty file

    when i use properties file to read the content from resource bundle
    it always give me default string rather than existing string in property file
    so how can i get it
    code is here:
    this is web.xml file==>
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' ''>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>





    <servlet-class>org.springframework.web.context.ContextLoade rServlet</servlet-class>



    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName"><value> qlserver.SQLServerDriver</value></property>
    <property name="url">
    <value>jdbc:microsoft:sqlserver://;databaseName=mcpg;SelectMethod=cu rsor</value>
    <property name="username"><value>iusr_mcpg</value></property>
    <property name="password"><value>mcpg</value></property>

    <bean id="messageSource" class=" eBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages"/>

    this is springapp-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">
    <!-- - Application context definition for "springapp" DispatcherServlet. -->

    <bean id="valdiform" class="LoginValid">
    <property name="messages" ref="messageSource"/>

    <bean id="springappController" class="SpringappController">
    <property name="messages" ref="messageSource"/>
    <property name="sessionForm"><value>true</value></property>
    <property name="commandName"><value>loginCommand</value></property>
    <property name="commandClass"><value>LoginCommand</value></property>
    <property name="formView">
    <property name="validator">
    <ref bean="valdiform"/>
    <property name="successView">

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="interceptors">
    <ref local="contentInterceptor"/>

    <property name="mappings">
    <prop key="/">springappController</prop>

    <bean id="contentInterceptor" class="org.springframework.web.servlet.mvc.WebCont entInterceptor">
    <property name="cacheSeconds"><value>30</value></property>
    <property name="supportedMethods"><value>POST</value></property>

    <bean id="viewResolver" class = "org.springframework.web.servlet.view.InternalReso urceViewResolver">
    <property name="viewClass"><value> rvlet.view.JstlView</value></property>
    <property name ="prefix"><value>/jsp/</value></property>
    <property name ="suffix"><value>.jsp</value></property>



    import org.springframework.context.*;
    import org.springframework.validation.*;
    public class LoginValid implements Validator {

    public boolean supports(Class clazz) {
    return LoginCommand.class.isAssignableFrom(clazz);

    private MessageSource messages;

    public void setMessages(MessageSource messages) {
    this.messages = messages;
    System.out.println("hello " + messages);

    public void validate(Object obj, Errors errors) {
    String message = this.messages.getMessage("app.vishal",
    null, "Required", null);
    System.out.println("i m in valid");
    LoginCommand account = (LoginCommand)obj;
    String ord = account.getOrderid();
    System.out.println("i m in display"+ord.length());
    if (ord.length()==0) {
    System.out.println("i m in valid");
    errors.rejectValue("orderid", "orderid.required");

  • #2
    Me too facing the same problem while loading the key -value pairs in errors.reject value. Can any one help us?


    • #3
      Can you post your file?


      • #4
        thanks in advance

        <bean id="messageSource" class=" eBundleMessageSource">
        <property name="basenames">


        error.required=Please enter {0}

        (i have placed this under web-inf/classes)

        Object[] msgFormatArgs = new Object[] { fieldName};
        errors.rejectValue(fieldKey, "error.required", msgFormatArgs,


        • #5
          Apparently I can see nothing wrong in your configuration.

          Did you try inspecting the messages object instance in your validator class to see if there is something wrong with it?

          Also, take a look at this link:

          As quoted from ResourceBundleMessageSource API:
          This MessageSource caches both the accessed ResourceBundle instances and the generated MessageFormats for each message. It also implements rendering of no-arg messages without MessageFormat, as supported by the AbstractMessageSource base class. The caching provided by this MessageSource is significantly faster than the built-in caching of the java.util.ResourceBundle class.

          Unfortunately, java.util.ResourceBundle caches loaded bundles forever: Reloading a bundle during VM execution is not possible. As this MessageSource relies on ResourceBundle, it faces the same limitation. Consider ReloadableResourceBundleMessageSource for an alternative that is capable of refreshing the underlying bundle files.
          Is your VM somehow caching this message source and thus is not able to display the messages you add?


          • #6
            Thanks leonardoborges .
            I debugged it and was able to retrieve msgs. may be some cache problem
            Now "error.required" was retrieved from properties file.but not able to substitute the arguments.

            error.required =Please enter {0}

            Both the methods display only Please enter {0} {argument not subsituted}

            1 Method:
            DefaultMessageSourceResolvable msgSourceRes = new DefaultMessageSourceResolvable("label.firstname");
            Object[] msgFormatArgs = new Object[] { msgSourceRes };
            errors.rejectValue(fieldKey, "error.require", msgFormatArgs, "values required");

            2 Method:
            errors.rejectValue(fieldKey, errorMessageKey, new String[]{"firstname"},
            "values required");


            • #7
              Note that this:

              error.required =Please enter {0}
              should look like this

              error.required =Please enter '{0}'
              Try now and see if this works!


              • #8
                Thanks leo!!!
                I would check this and tell the output


                • #9
                  i tried giving like this \{0}\,it works fine.


                  • #10
                    Simple Resource Bundle Usage

                    <bean id="messageSource" class=" eBundleMessageSource">
                    <property name="basename" value="/WEB-INF/com.test.resource.MyErrorMessages"/>

                    Your resource file placed in src/com/test/resource/ It may contain

                    myerror.key={0} is my name

                    After compilation will go into classes as-

                    This is the default bundle when Locale specific bundle is not found.

                    In your code you should inject "messageSource" bean
                    and use API-

                    messageSource.getMessage("myerror.key",new Object[]{"John"}, Locale.getDefault());

                    This will look for, if it doesnt find , t'will take the key from default

                    Your result will be display as-
                    John is my name

                    Thats all there is to it!! Good Luck !
                    Last edited by L10y; Mar 16th, 2009, 01:47 PM.


                    • #11
                      Hi there.
                      I'd like to access message source from spring javascript.
                      I mean, I'd like to display message from messageResource instead "Username cannot be null!" text.

                      <script type="text/javascript">
                      	Spring.addDecoration(new Spring.ElementDecoration({
                      		elementId : "j_username",
                      		widgetType : "dijit.form.ValidationTextBox",
                      		widgetAttrs : { required : true, invalidMessage : "Username cannot be null!" }}));  
                      How can I do this?