Page 5 of 8 FirstFirst ... 34567 ... LastLast
Results 41 to 50 of 73

Thread: How to do validation using MultiActionController

  1. #41
    Join Date
    Jun 2009
    Posts
    10

    Default

    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 ..

  2. #42
    Join Date
    Jun 2009
    Posts
    9

    Default

    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 at 04:04 AM.

  3. #43
    Join Date
    May 2009
    Posts
    6

    Default

    Quote 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 at 08:30 AM.

  4. #44
    Join Date
    Jun 2009
    Posts
    1

    Default 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.

  5. #45
    Join Date
    Jun 2009
    Posts
    9

    Default 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

  6. #46

    Default

    Quote Originally Posted by bdontu View Post
    if (null != errors && null != errors.getModel()) {
    //check for existance of errors
    mav.addObject("errorMap",errors.getModel());
    return mav;
    }
    A better way to do this is the following.
    BindingResult errors = getBindingResult(request, yourCommandBean);
    if (errors.hasErrors()) {
    return new ModelAndView(getFormView(), errors.getModel());
    }

  7. #47
    Join Date
    Jun 2009
    Posts
    9

    Post re:jerry.yan.mj

    Hi jerry.yan.mj,

    I guess it is only possible with SimpleFormController and we are talking about

    MultiActionController

    Can you please tell us more..

  8. #48

    Default

    Quote Originally Posted by prande View Post
    Hi jerry.yan.mj,

    I guess it is only possible with SimpleFormController and we are talking about

    MultiActionController

    Can you please tell us more..
    Please do not jump to the conclusion before you actually try it.

    I've been doing this for 3 different projects and it is proven to work fine.

  9. #49

    Default My Complete Code demonstrating how to do validation with MultiActionController

    Points to be noted:

    I am using MultiActionController, with InternalPathMethodNameResolver


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

    my bean class Customer.java

    package domain;

    public class Customer
    {
    String firstName;
    String surName;

    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }
    public String getSurName() {
    return surName;
    }
    public void setSurName(String surName) {
    this.surName = surName;
    }
    }

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

    my controller CustomerMultiController.java

    package controllers;

    public class CustomerMultiController extends MultiActionController
    {

    BindingResult errors;

    public BindingResult getErrors() {
    return errors;
    }

    public void setErrors(BindingResult errors) {
    this.errors = errors;
    }

    public ModelAndView stage2_or_3Handler(HttpServletRequest request, HttpServletResponse response)
    {
    try
    {
    Customer newCustomer = (Customer)newCommandObject(Customer.class);
    bind(request,newCustomer);
    validate(newCustomer);

    if(errors.hasErrors())
    {

    return new ModelAndView("Stage1_GetCustomerDetails",errors.ge tModel());
    }
    else
    {
    return new ModelAndView("Stage3");
    }
    catch(Exception expObj)
    {
    return new ModelAndView("ShowException");
    }
    }



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

    binder.bind(request);

    errors = binder.getBindingResult();
    }


    public void validate(Object command)
    {
    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);
    }
    }
    }
    }


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

    my validator CustomerInfoValidator.java

    package validation;
    import org.springframework.validation.Errors;
    import org.springframework.validation.ValidationUtils;
    import org.springframework.validation.Validator;

    import domain.Customer;

    public class CustomerInfoValidator implements Validator
    {
    public boolean supports(Class cls)
    {
    return Customer.class.equals(cls);
    }

    public void validate(Object arg0, Errors arg1)
    {
    //Is fields are empty validation
    ValidationUtils.rejectIfEmpty(arg1, "firstName", "firstName.emptyerror");
    ValidationUtils.rejectIfEmpty(arg1, "surName", "surName.emptyerror");
    }
    }

    ---------------------------------------------------------------------
    here is my messages.properties

    firstName.emptyerror=Enter first name.
    surName.emptyerror=Enter surname.

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


    my customer.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

    <form:form action="stage2_or_3.do">
    <table>
    <caption align="left"><font color="green"><b>Tell us about yourself</b></font></caption>
    <tr>
    <td align="left">First Name *</td>
    <td></td>
    <td></td>
    <td></td>
    <td align="left"><form:input path="firstName" maxlength="20" size="27"/></td>
    <td></td>
    <td colspan="2"><form:errors path="firstName" cssClass="errorBox"/></td>
    </tr>
    <tr></tr>
    <tr></tr>

    <!-- Field Surname -->
    <tr>
    <td align="left">Surname *</td>
    <td></td>
    <td></td>
    <td></td>
    <td align="left"><form:input path="surName" maxlength="20" size="27"/></td>
    <td></td>
    <td colspan="2"><form:errors path="surName" cssClass="errorBox"/></td>
    </tr>
    <!-- Submit button 'Next' -->
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr>
    <td></td>
    <td></td>
    <td colspan="2">
    <input type="submit" value="Next"/>
    </td>
    </tr>
    </table>

    </form:form>


    </body>
    </html>

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

    finally here is my bean-definition file

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- ################################ View Resolver ################################ -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jspfiles/"/>
    <property name="suffix" value=".jsp"/>
    </bean>

    <!-- ################################ Handler Mappings ################################ -->
    <bean class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="mappings">
    <value>

    /stage2_or_3.do = customerMultiController


    </value>
    </property>
    </bean>


    <!-- ################################ Controller of this Insurance application ########## -->
    <bean id="customerMultiController" class="controllers.CustomerMultiController">
    <property name="methodNameResolver" ref="internalPathMethodNameResolver"/>
    <property name="validators">
    <list>
    <bean class="validation.CustomerInfoValidator"></bean>
    <bean class="validation.DependentInfoValidator"></bean>
    </list>
    </property>
    </bean>

    <!-- ################################ Defining Validator ################################ -->
    <bean id="customerInfoValidator" class="validation.CustomerInfoValidator"/>
    <!-- ################################ Defining type of the method name resolver used by the controller ########## -->
    <bean id="internalPathMethodNameResolver" class="org.springframework.web.servlet.mvc.multiac tion.InternalPathMethodNameResolver">
    <property name="suffix" value="Handler"/>
    </bean>

    <!-- ################################ Message Source ################################ -->
    <bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
    <property name="basename" value="messages"/>
    </bean>
    </beans>

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

    I hope this help....Enjoy.....

    regards
    SARAVANAN SIVAJI

  10. #50
    Join Date
    Jun 2009
    Posts
    9

    Default

    Can you please check if your form tag contains attribute commandName?

Posting Permissions

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