Announcement Announcement Module
Collapse
No announcement yet.
Spring and commons validation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring and commons validation

    I set up a form extending a simpleFormController to work with a Validator ( with much help received from this forum).
    Everything worked fine - trapping errors and displaying them on the jsp page. However this was done using springs validation framework.
    I was now trying to get validation with commons-validation working but it fails on few things.
    I'm aware that spring modules is another project which is currently under work and does include integration with commons-validation. Since there is not much documentation , it's a little bit of a struggle. So i turn to the one place i've got results every single time - the forum

    This is what i have in my app-servlet.xml

    Code:
    <bean id="validatorFactory" class="org.springmodules.commons.validator.DefaultValidatorFactory">
            <property name="validationConfigLocations">
                <list>
                    <value>/WEB-INF/validator-rules.xml</value>
                    <value>/WEB-INF/validation.xml</value>
                </list>
            </property>
        </bean>
        <bean id="beanValidator" class="org.springmodules.commons.validator.DefaultBeanValidator">
            <property name="validatorFactory"><ref local="validatorFactory"/></property>
        </bean>
    I stumbled upon a few pages on Matt Raible's blog about integrating commons-validation with spring. But the code in his examples follow a different API. I came to the conclusion that since it was written sometime in 2004, that it has changed and is re-organized and refactored differently under spring modules.
    Here is the xml for my controller -
    Code:
    <bean name="/secured/tradeEntry.frm" class="xxx.xxx.xxx.xxx.TradeEntryController">
    		<property name="mvcMap">
                <map>
                    <entry key="name" value="trade_entry"/>
                    <entry key="permission" value="SCREEN_ACCESS"/>
                    <entry key="page" value="/secured/trade_entry.jsp"/>
                    <entry key="url" value="/secured/tradeEntry.frm"/>
                </map>
            </property>
            <property name="validator" ref="beanValidator"/>
            <property name="commandName" value="smtmTradeMessage"/>
            <property name="commandClass" value="xxx.xxx.xxx.xxx.SmtmTradeMessage"/>
            <property name="formView" value="/secured/trade_entry.jsp"/>
            <property name="successView" value="/secured/trade_entry_review.jsp"/>
    	</bean>

    Initializiation of the "/secured/tradeEntry.frm" bean fails because it says that the validator does not support the command class. What makes a command class "supportable" to a validator? what am i doing wrong?

    The above configuration works fine when i use spring's own validation.
    Any help will be appreciated. Thank you.
    ...D

  • #2
    If you use the library out-of-the-box, in your config for Commons Validator the name of each form has to be the short name of your command class, so in your case that would be smtmTradeMessage.

    E.g.

    Code:
    <form-validation>
       <formset>
         <form name="smtmTradeMessage">
            ...
    Cuong.

    Comment


    • #3
      Works. Thanks

      Cuong, Thank you. That was what it was.

      Comment


      • #4
        ApplicationResources.properties

        Well, i ran into a hurdle trying to get the application read values off of ApplicationResources.properties.
        I tried putting it under /web-inf/ApplicationResource.properties, and also under the root.
        And in my web.xml i tried -

        Code:
        <context-param> 
            <param-name>application</param-name> 
            <param-value>/WEB-INF/ApplicationResources</param-value> 
            </context-param>
        Code:
        <init-param>
              <param-name>application</param-name>
              <param-value>ApplicationResources</param-value>
            </init-param>
        Both of the above additions to web.xml didn't get Spring to read values from the properties file. What am i doing wrong?..or what am i not doing?
        Thanks in advance.

        Comment


        • #5
          The errors being generated are Spring's FieldError objects which resolve their messages from MessageSource beans rather than the 'Struts' way.

          Here's the doc:
          http://static.springframework.org/sp...-messagesource

          So, I suppose you could just add ApplicationResources to the list of resource bundles.

          Hope that helps,

          Cuong.

          Comment


          • #6
            Worked, thanks.

            Cuong, thanks again.That solved it.

            Comment


            • #7
              Server side also

              It might be a stupid thing to ask, but is common-validators from springmodules supposed to validate on the server side also, when JavaScript is not an option ? I have it configured and working on the client side (JavaScript) but when I disable JavaScript it doesn't seem to validate.

              I'm not sure if it is an issue, but I have 2 validators for this SimpleFormController extender, the first one is the commons-validator , the other is a normal spring validator.

              ???

              J

              Comment

              Working...
              X