Announcement Announcement Module
Collapse
No announcement yet.
Setting up errors obj in MultiactionController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting up errors obj in MultiactionController

    In my form, first user enters customer number and if the customer exists in the database, customer data
    should be displayed on the screen. Else error msg should be displayed to user saying that entered
    customer doesn't exist in DB. Customer can edit the data and save it in DB or delete customer.
    Once the data is successfully saved to DB, I should show message to user that customer is added successfully
    to Db.
    For this, I have a form with MultiactionController and methodname resolver. In my method getCustomerData() I am
    receiving the Custmer data from CustomerService in customer Object, setting Customer as command object
    and displaying it on getCustomerInfo.jsp. How do I send back the message to the form if the customer
    doesn't exist in the DB.

    How do I set up the Errors obj and use the errors object in jsp to send the msg back to form if the errors
    occur while saving the record to DB?

    My Controller:
    public ModelAndView saveCustomer(HttpServletRequest request, HttpServletResponse response, Customer customer
    throws ServletException, IOException {

    ...
    customerService.saveCustomer(customer);
    ..
    return new ModelAndView("getCustomer","customer ", customer);

    }

  • #2
    According to the JavaDoc for the MultiActionController:

    Note: For maximum data binding flexibility, consider direct usage of a ServletRequestDataBinder in your controller method, instead of relying on a declared command argument. This allows for full control over the entire binder setup and usage, including the invocation of Validators and the subsequent evaluation of binding/validation errors.
    So, I would suggest the following in your saveCustomer method:

    Code:
    public ModelAndView saveCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	Object command = newCommandObject(Customer.class); 
    	ServletRequestDataBinder binder = createBinder(request, command);
    	binder.bind(request);
    	if (this.validators != null) {
    		for (int i = 0; i < this.validators.length; i++) {
    			if (this.validators[i].supports(command.getClass())) {
    				ValidationUtils.invokeValidator(this.validators[i], command, binder.getBindingResult());
    			}
    		}		
    	}
    	
    	// Don't save if errors occurred
    	if(!binder.getBindingResult().hasErrors())
    	{
    		...
    		customerService.saveCustomer(customer);
    		..
    	
         		if(errorOccurred)
         		{
    			ObjectError error = new ObjectError(getCommandName(command), "<error_message>");
             		binder.getBindingResult().addError(error);
         		}
    	}
    
         	ModelAndView modelAndView = new ModelAndView("getCustomer");
    	// This will add the command object and the errors (along with your custom message) into the model
         	modelAndView.addAllObjects(binder.getBindingResult().getModel());
    	return modelAndView;
    }

    Comment


    • #3
      Thank you for your posting. I will try this out. Can you post the jsp code displaying the message from ObjectError?

      Comment


      • #4
        According to the JavaDoc for the ErrorsTag:

        Form tag for displaying errors for a particular field or object.

        This tag supports three main usage patterns:

        1. Field only - set 'path' to the field name (or path)
        2. Object errors only - omit 'path'
        3. All errors - set 'path' to '*'
        So you'll want to call the tag without a path which is:

        Code:
        <form:errors path="" />

        Comment

        Working...
        X