Results 1 to 10 of 13

Thread: How to throw ServletException on missing a required field

Hybrid View

  1. #1

    Default How to throw ServletException on missing a required field

    We have a SimpleFormController and a formObject which requires a field "id". If the "id" is not given, a MissingServletRequestParameterException should be thrown.

    What is the correct workflow?

    We did it like this:

    Code:
    protected void onBindOnNewForm ( HttpServletRequest request, Object command ) throws Exception {
    	CreateForm form = (CreateForm) command;
    	if (form.getId() == null) {
    	   throw new MissingServletRequestParameterException("id", "Long");
    	}
    }
    Another approach would be to override initBinder() like this:
    Code:
    @Override
    public void initBinder ( HttpServletRequest request, ServletRequestDataBinder binder ) throws Exception
    {
    	binder.setRequiredFields(new String[] {"id"});
    }
    But this way it won't throw an Exception, so we looked at DefaultBindingErrorProcessor to search for a way to throw an exception if a required field is not given.

    we are a little confused right now, how to choose the "correct" workflow for this scenario, which seems to me very common.

    any suggestions? Your help is very appreciated.

    kind regards
    Janning

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

    Default

    Why do you want to throw an exception. The initBinder method works fine, however as you noticed it won't throw an exception. It will generate an error message in the binder result. This message can be displayed on your screen.

    Check the sample about spring mvc and validation also check the samples they should give you an idea.
    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

    Default

    Quote Originally Posted by mdeinum View Post
    Why do you want to throw an exception. The initBinder method works fine, however as you noticed it won't throw an exception. It will generate an error message in the binder result. This message can be displayed on your screen.
    The Controller and View does not function if an id is not selected. Showing the form view doesn't make sense because i can' get the object which id i need.

    Think of "adding a new address for a customer". I click on a link like addNewAddress?customerId=1

    the controller prepares referencedata to let the view render a nice screen.

    Customer Name:John doe
    Add new Address [ ]

    I need the id of the customer in NewAddressForm and i need the id to collect referenceData. The view won't work without it and it is an exception beacuse the user shoul duse the correct link. If the user enter the url directly without the id i would like to throws an exception and let an ExceptionHandler handle it.

    I hope this example was comprehensible why i need to throw an Exception.
    i guess this is what MissingServletRequestParameterException is for, isn't it?

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

    Default

    Well if you need the id then why not simply make sure it is there in the formBackingObject method. You might want to take a look at ServletRequestUtils

    Code:
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        NewAddressForm form = new NewAddressForm();
        final long customerId = ServletRequestUtils.getRequiredLong(request, "customerId");
        form.setCustomerId(customerId);
        return form;
    }
    Last edited by Marten Deinum; Jun 6th, 2007 at 08:42 AM. Reason: Added link.
    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

    Quote Originally Posted by mdeinum View Post
    Well if you need the id then why not simply make sure it is there in the formBackingObject method.
    That's exactly what i asked myself :-)

    if i just need an id it seems ok. What if i need something more complex. Wouldn't it make sense to let it do the binder and throw the exception AFTER binding? it WORKS in formBackingObject but is it the place to be? Isn't it something that smells like binding?

    it would be fine if i can say (no real code):

    Code:
    initBinder () {
      boolean throwExceptionIdMissing = true;
      binder.setRequiredFields("id", throwExceptionIdMissing)
    }
    But i am fine with checking in formBackingObject if this is the place to be. Sometimes i think it's rather difficult to understand where to place what in SimpleFormController.

    thanks for the clarification

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Well you stated yourself

    The Controller and View does not function if an id is not selected. Showing the form view doesn't make sense because i can' get the object which id i need.
    So why let the binder handle it? Binding occurs after posting the form (ok you can set the bindOnNewForm to true). If you set the required field on the binder then it will generate an errorcode ('required.id') stating that the id is missing. The form is still being displayed, referenceData is being called.

    I would check it in the formbackingobject (and make my object a sessionForm) if not available the getRequiredLong will throw an exception, stopping everything from continuing.
    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

Posting Permissions

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