Results 1 to 7 of 7

Thread: How to do Server side validation in MultiActionController ?

  1. #1

    Question How to do Server side validation in MultiActionController ?

    Hey All,

    I need to do the server side validation for the values entered in Input form by user.

    I am using MultiActionController for that request. so how can i do this.
    means which methods i need to implement ?. and how can i get the values of command class ?

    Please reply ASAP

    Thanks and Regards,
    Arun D.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Just implement the Validator interface on some object you want to be the Validator. When binding occurs the Validation is handled for you. So just create a Validator, register it in the context file and inject it in your MultiActionController.

    Values from the command class can be accessed by calling the getters, as you would do otherwise.

    Check chapter 13 of the reference guide for more information, and you also might want to check the Petstore sample shipped with Spring.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Dec 2006
    Posts
    2

    Default

    Implementing a Validator wont work. I mean it will work and it will validate but only in an academic sense of the word. The multiactioncontroller implementation will simply throw an exception onto the screen if your validator doesnt validate. We, in the real world, cannot have users stare at a massive stack trace every time they mistype a character. We need to be able to gracefully prompt them and verbosely let them know where they screwed up. If we dont, each of those "validations" will result back on our desks as a support ticket. The only way to get around all this and still use the multiactioncontroller is to overwrite a bunch of functionality yourself, which is, ofcourse, very doable, but I mean, isnt it the point of a framework to reduce the amount of code and allow developers not to worry about the piping and such?

    I'm sorry if I come off a little aggressive, but I mean if you are going to provide functionality, either document in big bold letters what it cant do versus the rest of your stuff or make sure most visible use cases pass.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    And why wouldn't it work if a Validator?! If you implemented your Validator correctly is shouldn't be a problem... We use validators all over the place, in multi screen WizardForm, MulitActionControllers and in Spring Web Flow without much trouble (We had some issues with multiple validators on a SimpleFormController and FormAction).

    The default is indeed throwing an exception, however you can easily define a exceptionHandling method which in turn returns a ModelAndView which can contain anything you want. The thrown exception contains the Errors instance with the errors.

    Maybe reading a bit more documentation next time saves you the trouble from overriding/reimplementing al those methods...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default where should i implement exception handler function, ?

    Hi, as you said earlier that we can handle exception by implementing exception handler function.

    But i am confused that i need to implment this function ?

    PLease reply with some snapshot code.
    Success, a ongoing journey !

  6. #6
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    From the Javadoc of MultiActionController:
    Subclasses can implement custom exception handler methods with names such as:

    ModelAndView anyMeaningfulName(HttpServletRequest request, HttpServletResponse response, ExceptionClass exception);

    The third parameter can be any subclass or Exception or RuntimeException.
    For example, if you wanted to catch a BusinessRuleException, you might add:

    Code:
    ModelAndView handleBusinessRuleException(HttpServletRequest request, HttpServletResponse response, BusinessRuleException exception) {
     return new ModelAndView("/path/to/error.do", "exception", exception);
    }
    I haven't tested the code to make sure it complies, etc. but that should be pretty darn close.

  7. #7
    Join Date
    Dec 2006
    Posts
    2

    Default

    Indeed, the exception handling interceptor will work and may be used. My point is, think about what a developer has to go through to get things working as they should. I need to create the error handling interceptor, somehow figure out which view was the original view (remember, this is a multiactioncontroller). Then I have to figure out how to fool the view into thinking that I'm using a formcontroller that sets the "status" on my form backing object so I can use the existing error displaying logic tags. I also need to make sure manually my form gets populated with the user input just like it does if a formcontroller is used (we all know how important consistent behavior is). In addition to all this, I have to make sure my exception interceptor code is in sync with my controller code if, goodness forbid, I ever change my view flow. You must agree, this sounds like a world of pain. In our case, what we did was, we used the multiactioncontroller for some functionality, but implemented simpleformcontrollers for where we needed to collect user input. It made for a bunch of unnecessary classes, but it worked.

Posting Permissions

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