2009-07-10 31 views
1

ModelAndViewとModelMapの間に相違点があります。AnnotationController、SpringframeworkのModelAndViewとModelMapの問題

requestMethodが「GET」で、requestMethodが「POST」の場合、modelAndViewを維持したいと考えています。 私のmodelAndViewは他の人を保存しました。

私はmodelAndViewの戻り値の型を "GET"、 "POST"メソッドにしました。

しかし、要求はcommandObject、form:errors ...(要求が失われた)要求の検証が失敗したために "POST"でshowFormを返します。

例)

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; 

} 

答えて

4

それはあなたの質問が何であるかは明らかではないのですが、ModelMapとのModelAndViewの差にように、彼らは春のMVCの二つの異なる「世代」から来ます。 ModelAndViewはSpring 2.0スタイルのものですが、ModelMapは2.5で導入されました。

一般的に言えば、あなたのコントローラーがSimpleFormController(コードフラグメントと思われる)のようなSpring 2.0コントローラーをサブクラス化している場合は、ModelAndViewを使用します。 Spring 2.5 @Controllerアノテーションを使用している場合は、ModelMapを使用することをお勧めします。

0

私の質問は、提出時にhtmlフォームオブジェクトをPOJOにバインドする方法についてより詳しく説明しました。

Javaコード:

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

    return("NOTHING"); 
} 

HTMLコード:

<@spring.bind "accessRequestBean" /> 
    <form> 
    ... 
関連する問題