Results 1 to 7 of 7

Thread: Multiple @ModelAttribute ?

  1. #1

    Question Multiple @ModelAttribute ?

    Hello,

    I have a problem using Validator in annotated controller.
    I'm using controller with annotation, everything is ok but when I use a method with two ModelAttribute, the validator doesn't care about the object that I give and try to validate the last ModelAttribute of the method signature.
    Here is a part of the controller :

    Code:
    @RequestMapping(value="/contact/createCompanyContact.do", method = RequestMethod.POST)
    	public ModelAndView doCreateLawCompanyContactAction(@ModelAttribute Contact contact,
    			@ModelAttribute Integer Id, BindingResult result, SessionStatus status) throws Exception {
    
            contactValidation.validate(contact, result);
    
            if (result.hasErrors()){
                return new ModelAndView(...);
            } else {
                status.setComplete();
                ...
                return new ModelAndView(new RedirectView(...));
            }
    
    }
    The Contact object and the Integer come from the session :
    Code:
    @Controller
    @SessionAttributes(types = {Contact.class, Integer.class})
    public class ContactController { ... }
    When I debug, Contact is correctly filled with data from the form, and the Integer has the correct value, but the Validator doesn't validate the Contact object but the Integer !!
    Last edited by jybosse; Dec 5th, 2008 at 08:39 AM.

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

    Default

    The BindingResult only is valid for the object that it follows. So in your case it is only containing the stuff for the Integer. You should have 2 bindingresults in there... Also why do you need 2 ModelAttributes especially when 1 is an integer?!...
    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

    I've just discovered what you are saying
    I need the integer because the contact to create need to be added to another company which is given before.
    Flow is :
    1 create a company
    2 display the created Company with the link "add a contact"
    3 display the add contact page (if the user clicked the previous link) and keep the id of the company in the session
    4 create the contact and add it to the company using the id in session.

    Is it incorrect ?

  4. #4

    Default

    And ?
    What about :
    Also why do you need 2 ModelAttributes especially when 1 is an integer?!...

  5. #5
    Join Date
    Dec 2008
    Location
    Brussels, Bucharest
    Posts
    3

    Default Multiple @ModelAttribute ?

    let's say you have something like this(many form in page)

    @RequestMapping(method = RequestMethod.POST)
    public String processSubmit(@RequestParam("formBeanName") String formBeanName,
    @ModelAttribute("getter") GetterForm getter,
    @ModelAttribute("terset") TersetForm terset,
    @ModelAttribute("tertmt") TertmtForm tertmt,
    @ModelAttribute("getttr") GetttrForm getttr, BindingResult result, Model model,
    HttpServletRequest request, HttpSession session)
    {


    In this case I reistantiate BindingResult param:
    result = new BeanPropertyBindingResult( target, objectName);

    For knowing what bean the target object is, I add for every form in the page a hidden attribute eg formbeanName
    and I select the bean like this:
    target = model.asMap().get( formBeanName.toLowerCase() ).

  6. #6
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    This makes no sense to me. There can be only one form submission at a time and only one command object can represent the data on a form. If you want access to other model attributes you can take a Model as input parameter.

  7. #7
    Join Date
    Apr 2009
    Posts
    27

    Default

    i got the same problem.. what's spring allow multiple @ModelAttribute declaration...?

Posting Permissions

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