views:

1589

answers:

2

I have a question that is a point difference between ModelAndView and ModelMap.

I want to maintain modelAndView when requestMethod is "GET" and requestMethod is "POST". My modelAndView saved others.

So I made modelAndView return type to "GET", "POST" methods.

But, Request lost commandObject, form:errors..., if request return showForm on "POST" because request validation failed.

example)

private ModelAndView modelAndView;    

public ControllerTest{
   this.modelAndView = new ModelAndView();
}

@RequestMapping(method = RequestMethod.GET)
public ModelAndView showForm(ModelMap model) {
    EntityObject entityObject = new EntityObject();
    CommandObject commandObject = new CommandObject();
    commandObject.setEntityObject(entityObject);
    model.addAttribute("commandObject", commandObject);
    this.modelAndView.addObject("id", "GET");
    this.modelAndView.setViewName("registerForm");

    return this.modelAndView;
}

@RequestMapping(method = RequestMethod.POST)
public ModelAndView submit(@ModelAttribute("commandObject") CommandObject commandObject,
        BindingResult result, SessionStatus status) {

    this.commandValidator.validate(commandObject, result);

    if (result.hasErrors()) {
        this.modelAndView.addObject("id", "POST");
        this.modelAndView.setViewName("registerForm");
        return this.modelAndView;

    } else {
        this.modelAndView.addObject("id", "after POST");
        this.modelAndView.setViewName("success");
    }
    status.setComplete();
    return this.modelAndView;

}
A: 

It's not clear what your question is but as to the difference between ModelMap and ModelAndView, they come from two different "generations" of Spring MVC. ModelAndView is from the Spring 2.0 style, whereas ModelMap was introduced in 2.5.

Generally speaking, if your controller subclasses a Spring 2.0 controller, such as SimpleFormController (which I think your code fragment is), then ModelAndView is the thing to use. If you're using Spring 2.5 @Controller annotatations, ModelMap is preferable.

skaffman
A: 

My question was more directed as to how to bind html form objects to a POJO on submit.

Java code:

@RequestMapping(method = RequestMethod.POST)
public String processRequest(ModelMap map, @ModelAttribute("accessRequestBean") AccessRequestBean accessRequestBean){
    logger.debug(accessRequestBean);

    return("NOTHING");
}

HTML Code:

    <@spring.bind "accessRequestBean" /> 
    <form>
    ...
wuntee