Announcement Announcement Module
Collapse
No announcement yet.
How to do validation using MultiActionController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #31
    Pls Help me out

    I got struck with validating form using Multiaction controller...
    i've pasted my code in the above post...

    thanks in advance.....

    Comment


    • #32
      can you post your error?

      Comment


      • #33
        Pls help me while displaying errors--Multiaction controller

        Hi Jerry,


        I am not getting any exceptions in the console...but while displaying errors in jsp page...they are not getting displayed properly...


        following are the messages getting diaplyed on jsp Page


        [email protected]
        org.springframework.validation.BindingResult.comma nd=org.springframework.validation.BeanPropertyBind ingResult: 2 errors Error in object 'command': codes [firstName.emptyerror.command,firstName.emptyerror]; arguments []; default message [null] Error in object 'command': codes [lastName.emptyerror.command,lastName.emptyerror]; arguments []; default message [null]

        where Person is the command i'm using and firstName,lastName are properties in that...


        Thanks in advance.....

        Comment


        • #34
          Hi,

          I want to see ur JSP code containing the properties firstName,lastName.

          or

          Tell me, have u used the following attribute in form tag, commandName="---"

          Comment


          • #35
            If i use below code for dispalying errors i'm not even getting them displayed in my JSP:

            CODE :
            ------

            <spring:bind path="personForm.*">
            <c:forEach items="${errorMessages}" var="errormsg">
            <c:out value="errormsg"/>
            </c:forEach>
            </spring:bind>


            But if i use

            <spring:bind path="personForm.*">
            <c:forEach items="${errorMap}" var="msg">
            <c:out value="${msg}"/>
            </c:forEach>
            </spring:bind>

            i'm able to display the following as error messages



            [email protected] org.springframework.validation.BindingResult.perso nForm=org.springframework.validation.BeanPropertyB indingResult: 2 errors Field error in object 'personForm' on field 'firstName': rejected value []; codes [firstName.emptyerror.personForm.firstName,firstNam e.emptyerror.firstName,firstName.emptyerror.java.l ang.String,firstName.emptyerror]; arguments []; default message [First Name should not be Empty!] Field error in object 'personForm' on field 'lastName': rejected value []; codes [lastName.emptyerror.personForm.lastName,lastName.e mptyerror.lastName,lastName.emptyerror.java.lang.S tring,lastName.emptyerror]; arguments []; default message [LastName should not be Empty!]


            what i used in controller is:

            if (null != errors && null != errors.getModel()) {
            //check for existance of errors
            mav.addObject("errorMap",errors.getModel());
            return mav;
            }


            and i'm using following code to configure my poperties files in application context xml file

            <bean id="messageSource"
            class="org.springframework.context.support.Reloada bleResourceBundleMessageSource">
            <property name="basenames">
            <list>
            <value>/WEB-INF/resources/person</value>
            </list>
            </property>
            </bean>


            person.properties contains


            firstName.emptyerror=LastName is required
            lastName.emptyerror=LastName is required

            please lety me know the wrong place....do i need to change my resourcebundle in applicationcontext.xml file????



            Thanks in advance....

            Comment


            • #36
              Error resolved

              Hi..
              i found where i'm doing mistake....
              now i'm able to get the errors displayed on my jSP page .....


              Thanks for helping me......

              Comment


              • #37
                How to validate nested properties using Validators (MultiActionController)?

                I'm facing similar problems as member have discussed previously..but there is some difference I'm trying to vaildate nested property..


                My Validate method

                public void validate(Object obj, Errors errors) {
                ElementBean elembean = (ElementBean) obj;
                HuYdgElementsT element = elembean.getYdgElements();
                if (element == null) {
                //errors.rejectValue("Element", "error.Element.not-specified", null,"Value required.");
                } else {
                errors.pushNestedPath("ydgElements");
                if(element.getElementTitleC()==null||element.getEl ementTitleC().equals("")){
                errors.reject("error.elementTitleC");
                }
                errors.popNestedPath();
                }
                }


                Exception Occuring
                org.springframework.web.bind.ServletRequestBinding Exception: Errors binding onto object 'command'; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBinding Result: 1 errors
                Error in object 'command': codes [error.elementTitleC.command,error.elementTitleC]; arguments []; default message [null]


                Please provide some sample code if possible

                Thanks in Advance
                Last edited by prande; Jun 29th, 2009, 02:58 AM.

                Comment


                • #38
                  Hi

                  paste u r jsp and controller code from where you r calling valiadtor.....

                  Comment


                  • #39
                    Controllers Code where validator is getting called

                    public ModelAndView submitForm(HttpServletRequest httpServletRequest,
                    HttpServletResponse httpServletResponse, ElementBean command) {
                    logger.info("Entered addElement Method###################");
                    ServletRequestDataBinder binder;
                    ModelAndView mav= null;
                    try {
                    binder = createBinder(httpServletRequest, command);
                    errors = binder.getBindingResult();
                    if(errors.hasErrors())
                    {
                    mav = new ModelAndView("Multi",errors.getModel());
                    }
                    mav =new ModelAndView("Multi","elementBean",command);

                    }catch (Exception e) {
                    e.printStackTrace();
                    }
                    return mav;
                    }

                    Form in the JSPhaven't wrriten anything yet to print errors

                    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
                    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
                    <%@ taglib prefix="springform" uri="http://www.springframework.org/tags/form"%>
                    <springform:form name="elementForm" method="post" action="/Univ/Multi.htm?testParam=submitForm" commandName="elementBean">
                    <div id="titleHead">
                    <div id="status">
                    <span class="itemLabel">Status:</span>Active at 2:13 p.m., Tues., May 12, 2009
                    </div>
                    <h1 class="guideBuildTitle"><springform:hidden path="ydgElements.elementTitleC"/> <c:out value="${elementBean.ydgElements.elementTitleC}"/></h1><a class="titleEdit" href="#" />Edit Title</a>
                    </div>
                    <div class="rightColHead"><h5 class="headTitle">Element Attributes</h5><a class="collapse" href="#"></a></div>
                    <div id="attributes">
                    <div id="attribLeftFields">
                    <div class="attribField largeField">
                    <h5>Description</h5>
                    <springform:input path="ydgElements.elementDescC" />
                    <!--
                    <input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${ydgElements.elementDescC}"/>"/>
                    -->
                    </div>


                    spring mappings

                    <bean name="/Multi.htm" class="com.ui.controllers.TestMultiActionControlle r">
                    <property name="methodNameResolver">
                    <ref bean="paramResolver"/>
                    </property>
                    <property name="elementBO" ref="elementBO"></property>
                    <property name="validators"><list><ref bean="elementFormValidator"/></list></property>
                    </bean>
                    Last edited by prande; Jun 29th, 2009, 03:43 AM.

                    Comment


                    • #40
                      I think u have to use commandName instead of ydgElements...in jsp input path elements

                      and tell me the type of errors variable u r using....

                      Comment


                      • #41
                        otherwise write the below function and call validator from with the controlelr



                        protected Errors bindObject(HttpServletRequest request,
                        Person personCommand,Validator validator) throws Exception {
                        Errors errors = new BindException(personCommand,
                        getCommandName(personCommand));
                        if (validator.supports(personCommand.getClass())) {
                        ValidationUtils.invokeValidator(validator, personCommand, errors);
                        }
                        logger.error("Errors Count : "+errors.getErrorCount());
                        return errors;
                        }

                        i think tis may help u...

                        and plz remove the validtor config from displatcher servlet ..

                        Comment


                        • #42
                          I guess that it will not help me ..

                          my elementBean is the command object which contains ydgElements as property which is another class and it contains the elementTitle

                          still I tried and I got following error

                          LoggingInterceptor::afterThrowing Exception in method: handleRequest
                          Jun 29, 2009 1:20:03 PM org.apache.catalina.core.StandardWrapperValve invoke
                          SEVERE: Servlet.service() for servlet ydg threw exception
                          org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBinding Result: 1 errors
                          Error in object 'command': codes [error.elementTitleC.command,error.elementTitleC]; arguments []; default message [null]
                          at org.springframework.web.bind.ServletRequestDataBin der.closeNoCatch(ServletRequestDataBinder.java:121 )

                          even before calling the submitForm method

                          Type of varialbe I'm using

                          BindingResult errors;
                          Last edited by prande; Jun 29th, 2009, 04:04 AM.

                          Comment


                          • #43
                            Originally posted by saravanansivaji View Post
                            I think the code is already available in this thread, given by one of our friend jerry, thanks to him. Anyhow I will repaste the code here

                            The following is my multi action controller,

                            public class MyController extends MultiActionController
                            {

                            BindingResult errors; //Declaring errors as instance variable to catch error


                            /** My overidden bind() method */

                            protected void bind(HttpServletRequest request, Object command) throws Exception
                            {
                            ServletRequestDataBinder binder = createBinder(request, command);
                            binder.bind(request);
                            errors = binder.getBindingResult();
                            }



                            /** My validator */

                            public void MyValidator(Object command)
                            {
                            /** Invoking validator **/
                            Validator[] validators = getValidators();
                            if (validators != null)
                            {
                            for (int index = 0; index < validators.length; index++)
                            {
                            Validator validator = validators[index];
                            if (validator instanceof CustomerInfoValidator)
                            {
                            if (((CustomerInfoValidator)validator).supports(comma nd.getClass())) ValidationUtils.invokeValidator(validators[index], command, errors);
                            }
                            else if (validator.supports(command.getClass()))
                            ValidationUtils.invokeValidator(validators[index], command, errors);
                            }
                            }
                            }



                            /** this is my method handler */

                            public ModelAndView newcontactHandler(HttpServletRequest request, HttpServletResponse response)
                            {
                            if(errors.hasErrors())
                            {
                            return new ModelAndView("addcontact",errors.getModel());
                            }
                            return new ModelAndView("add-success");
                            }
                            }
                            }

                            }


                            I hope this help, If u can't understand let me know...



                            Hi,
                            Thnx for your reply, i m sorry i m lilbit late to reply you.. I understood your code but stilll something is missing.. Can you tell me from where are you going to use "newcontactHandler" and moreover cn i hv an overview of bean code and a jsp code so that i cn understand the variable used for binding.
                            Moreover.. Aren't you implementing the validator interface?
                            And are these methods written in the controller classes only?
                            Waiting for your reply
                            Waiting for your reply
                            Thnx
                            Last edited by levis; Jun 29th, 2009, 08:30 AM.

                            Comment


                            • #44
                              Bean scope

                              using BindingResult errors as instance variable can have undesirable consequences, if bean scope is not appropriate. A singleton will be shared across all users, so will be the instance variable.

                              Comment


                              • #45
                                re:How to do validation using MultiActionController

                                It will be great if somebody will provide some working sample application with all the files for validation in multiactioncontroller if possible provide validation for nested properties

                                Comment

                                Working...
                                X