Page 1 of 8 123 ... LastLast
Results 1 to 10 of 73

Thread: How to do validation using MultiActionController

  1. #1

    Default How to do validation using MultiActionController

    Dear friends,

    I am trying to validation with MultiActionController, Could anyone help me in this regard?

    Could anyone have sample code how to do validation with MultiActionController?

    ----------

    I am doing sample 'phonebook' application using MultiActionController, here is my code,

    public class PhoneBookController extends MultiActionController
    {
    public ModelAndView newcontactHandler(HttpServletRequest request, HttpServletResponse response)
    {
    try
    {
    Contact newContact = (Contact)newCommandObject(Contact.class);
    bind(request, newContact);
    phoneBook.addNewContact(newContact);
    return new ModelAndView("add-success");
    }
    catch(Exception e){}
    }
    }

    -----------------------------------

    here is my bean definition file,

    <bean id="phoneBookController" class="controller.PhoneBookController">
    <property name="methodNameResolver" ref="internalPathMethodNameResolver"/>
    <property name="phoneBook" ref="phoneBook"/>
    <property name="validators" ref="contactInfoValidator"/>
    </bean>

    <bean id="contactInfoValidator" class="validator.ContactInfoValidator"/>


    but I am getting the following error,******************
    org.springframework.web.bind.ServletRequestBinding Exception: Errors binding onto object 'command'; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBinding Result: 2 errors
    Field error in object 'command' on field 'contactName': rejected value []; codes [contactName.emptyerror.command.contactName,contact Name.emptyerror.contactName,contactName.emptyerror .java.lang.String,contactName.emptyerror]; arguments []; default message [null]
    Field error in object 'command' on field 'phoneNumber': rejected value []; codes [phoneNumber.emptyerror.command.phoneNumber,phoneNu mber.emptyerror.phoneNumber,phoneNumber.emptyerror .java.lang.String,phoneNumber.emptyerror]; arguments []; default message [null]

  2. #2

    Default

    As you might understand, a validator is only applicable for a form based controller. And a MultiaActionController is NOT a form based controller.
    [URL="http://vicina.info"] 新闻,社区新闻,分类广告

  3. #3

    Default

    Then, could you tell me what is the purpose of the following method,

    public final void setValidators(Validator[] validators)

    When I use the above method,

    I am ending up with the following error,
    org.springframework.web.bind.ServletRequestBinding Exception: Errors binding onto object 'command'; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBinding Result: 2 errors

    Field error in object 'command' on field 'contactName': rejected value []; codes [contactName.emptyerror.command.contactName,contact Name.emptyerror.contactName,contactName.emptyerror .java.lang.String,contactName.emptyerror]; arguments []; default message [null]

    Field error in object 'command' on field 'phoneNumber': rejected value []; codes [phoneNumber.emptyerror.command.phoneNumber,phoneNu mber.emptyerror.phoneNumber,phoneNumber.emptyerror .java.lang.String,phoneNumber.emptyerror]; arguments []; default message [null]

    Thanks

  4. #4

    Default

    You can of course do validation for multiactioncontroller.

    The error you got is not from validation first of all. it is from the data binding. The data binder is not able to bind the input to the command object. Your code hasn't reached validation.

    I don't know if you really understand the multiactioncontroller or not.

    In multiactioncontroller source, this is really important to understand. what is your command object.

    // If last parameter isn't of HttpSession type, it's a command.
    if (paramTypes.length >= 3 &&
    !paramTypes[paramTypes.length - 1].equals(HttpSession.class)) {
    Object command = newCommandObject(paramTypes[paramTypes.length - 1]);
    params.add(command);
    bind(request, command);
    }

    There is a final method invokeNamedMethod. It includes this piece of code. What it really means is the flexibility of command object of this type of controller. So, you can want to pass your command object as the 3rd parameter to your method signature: public ModelAndView newcontactHandler(HttpServletRequest request, HttpServletResponse response, Phonebook phoneBook) for example.

  5. #5

    Default

    hi jerry.yan.mj,

    Yes, I am unable to understand the MultiActionController to apply Validation.

    Actually command class is Contact.java containing 2 attributes contactName and phoneNumber.

    I have written validator class called ContactInfoValidator.java which implements the interface validator

    and mycontroller extends MultiActionController,

    As you suggested I have used 3 argument handler

    public ModelAndView newcontactHandler(HttpServletRequest request, HttpServletResponse response, Contact newContact)

    but again I end up with the same error

    *****************************************
    I am ending up with the following error,
    org.springframework.web.bind.ServletRequestBinding Exception: Errors binding onto object 'command'; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBinding Result: 2 errors

    Field error in object 'command' on field 'contactName': rejected value []; codes [contactName.emptyerror.command.contactName,contact Name.emptyerror.contactName,contactName.emptyerror .java.lang.String,contactName.emptyerror]; arguments []; default message [null]

    Field error in object 'command' on field 'phoneNumber': rejected value []; codes [phoneNumber.emptyerror.command.phoneNumber,phoneNu mber.emptyerror.phoneNumber,phoneNumber.emptyerror .java.lang.String,phoneNumber.emptyerror]; arguments []; default message [null]
    ***************************



    Could u tell me exactly how to do validation with the MultiActionController?


    Thanking you
    Last edited by saravanansivaji; Jun 1st, 2009 at 11:51 PM.

  6. #6

    Default Could you please attach your validator source?

    Could you please attach your validator source?

  7. #7

    Default

    hi jerry.yan.mj,

    First, Thank u for ur reply. Here is my complete code,

    ****************
    The following is my domain class, Contact.java
    ****************

    package domain;

    import java.io.Serializable;

    public class Contact implements Serializable
    {
    String contactName;
    String phoneNumber;

    public String getContactName() {
    return contactName;
    }
    public void setContactName(String contactName) {
    this.contactName = contactName;
    }
    public String getPhoneNumber() {
    return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
    }
    }
    -----------------------------------------------------------------

    The following is my validator class, ContactInfoValidator.java

    package validator;

    import org.springframework.validation.*;

    import domain.Contact;

    public class ContactInfoValidator implements Validator {

    public boolean supports(Class cls)
    {
    return Contact.class.equals(cls);
    }

    public void validate(Object obj, Errors errors)
    {
    System.out.println("############");
    ValidationUtils.rejectIfEmpty(errors, "contactName", "contactName.emptyerror", "Contact Name should not be Empty!");
    ValidationUtils.rejectIfEmpty(errors, "phoneNumber", "phoneNumber.emptyerror", "Phone Number should not be Empty!");
    }
    }

    //Just checking for isEmpty or not
    --------------------------------------------------------------------

    The following is my controller class file, extends MultiActionController,

    package controller;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;


    import org.springframework.validation.Validator;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.SimpleFormCont roller;
    import org.springframework.web.servlet.mvc.multiaction.Mu ltiActionController;

    import domain.Contact;

    import service.PhoneBook;
    import validator.ContactInfoValidator;

    public class PhoneBookController extends MultiActionController
    {
    public ModelAndView newcontactHandler(HttpServletRequest request, HttpServletResponse response)
    {
    try
    {
    Contact newContact = (Contact)newCommandObject(Contact.class);
    bind(request, newContact);
    phoneBook.addNewContact(newContact);
    return new ModelAndView("add-success");
    }
    catch(Exception expObj)
    {
    System.out.println(expObj);
    return new ModelAndView("add-fail");
    }
    }
    }

    //in the above code, phoneBook is an interface, containing 2 methods, addNewContact() and getContactsList()
    I have written separate class to implement this interface, this is not a problem
    ---------------------------------------------------------------------

    The following is my app-servlet.xml

    <bean id="phoneBookController" class="controller.PhoneBookController">
    <property name="methodNameResolver" ref="internalPathMethodNameResolver"/>
    <property name="phoneBook" ref="phoneBook"/>
    <property name="validators" ref="contactInfoValidator"/>
    </bean>

    <bean id="contactInfoValidator" class="validator.ContactInfoValidator"/>

    <bean id="internalPathMethodNameResolver" class="org.springframework.web.servlet.mvc.multiac tion.InternalPathMethodNameResolver">
    <property name="suffix" value="Handler"/>
    </bean>

    <bean id="phoneBook" class="service.PhoneBookManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>

    //PhoneBookManager is a class that implements the interface called "PhoneBook" as I said above
    -------------------------------------------------------------------

    The following is my jsp file called addcontact.jsp

    <form:form action="newcontact.do" commandName="command">
    <table>
    <tr>
    <td align="left">Contact Name:</td>
    <td><form:input path="contactName"/></td>
    </tr>
    <tr>
    <td colspan="2">
    <form:errors path="contactName" cssClass="errorBox"/>
    </td>
    </tr>
    <tr></tr>
    <tr></tr>
    <tr>
    <td align="left">Phone Number:</td>
    <td><form:input path="phoneNumber"/></td>
    </tr>
    <tr>
    <td colspan="2">
    <form:errors path="phoneNumber" cssClass="errorBox"/>
    </td>
    </tr>
    <tr></tr>
    <tr></tr>
    <tr>
    <td colspan="2">
    <input type="submit" value="Submit New Contact"/>
    </td>
    </tr>
    </table>
    </form:form>


    ********* My requirement is **********

    when I click the submit button in the above form, the controller should validate the 2 fields and display the error message. I am checking only for isEmpty or not. when validation is success, it has to insert record in the DB (which I alread did) and should fetch the add-success.jsp else add-fail.jsp

    All I need is how to apply validation class with the multiactioncontroller?


    Thanking you,

    Waiting for you replay jerry.........

  8. #8

    Default You are almost correct, but odd

    My friend,

    You are doing it in a very special way. First of all, if there is binding error, you should let spring return to a form view.
    sth like:
    BindingResult errors = getBindingResult(request, eventFormData);
    if (errors.hasErrors()) {
    Map<String, Object> modelMap = prepareErrorViewForEditEvent(eventFormData);
    return new ModelAndView(getFormView(), errors.getModel()).addAllObjects(modelMap);
    }

    second, remember that i told you if you use the third parameter, you should not call bind the method in your action method because it is already called.

    Let's try this:

    public ModelAndView newcontactHandler(HttpServletRequest request, HttpServletResponse response, Contact contact)
    {
    BindingResult errors = getBindingResult(request, contact);
    if (errors.hasErrors()) {
    return new ModelAndView("***Your form jsp path*******");
    }
    try
    {
    phoneBook.addNewContact(contact);
    return new ModelAndView("add-success");
    }
    catch(Exception expObj)
    {
    System.out.println(expObj);
    return new ModelAndView("add-fail");
    }

  9. #9

    Default Thank you for your Great Job Jerry

    hi jerry,

    if (errors.hasErrors()) {
    Map<String, Object> modelMap = prepareErrorViewForEditEvent(eventFormData);
    return new ModelAndView(getFormView(), errors.getModel()).addAllObjects(modelMap);
    }

    could you tell me what "eventFormData" refers to from the above code?

    Thank you

  10. #10

    Default It is an example

    Hi,

    It's just an example I've included. Please read my reply. I asked you to try this:

    public ModelAndView newcontactHandler(HttpServletRequest request, HttpServletResponse response, Contact contact)
    {
    BindingResult errors = getBindingResult(request, contact);
    if (errors.hasErrors()) {
    return new ModelAndView("***Your form jsp path*******");
    }
    try
    {
    phoneBook.addNewContact(contact);
    return new ModelAndView("add-success");
    }
    catch(Exception expObj)
    {
    System.out.println(expObj);
    return new ModelAndView("add-fail");
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •