2011-01-09 81 views
0

當我加載這個表格時,國家表格是從240個國家的數據庫填充的。如果我提交它,並帶有一些空的必填字段,頁面會重新加載錯誤消息。但我沒有得到任何國家上市。我使用相同的代碼來填充在GET和POST方法的清單 - 見下文彈簧表格不填寫表格提交

 <form:form commandName="student_personal_info" method="post"> 
       <table> 
         <tr> 
         <td><form:label path="country">Country:</form:label></td> 
         <td><form:select path="country"> 
          <form:option value="NONE" label=" --Select-- "></form:option> 
          <form:options items="${countries}"/> 
          </form:select> 
         </td> 
      </tr></table> 
     </form:form> 



@RequestMapping(value = "student_personal_info", method = RequestMethod.GET) 
    public ModelAndView DisplayPersonalForm(ModelAndView model) { 
     StudentPersonalInfo personalInfo = new StudentPersonalInfo(); 
     model.addObject("student_personal_info", personalInfo); 

     model.addObject("countries",getCountries()); 
     return model; 
    } //this works fine 

    @RequestMapping(value = "student_personal_info", method = RequestMethod.POST) 
    public String PersonalFormSubmitted(
      @ModelAttribute("student_personal_info") @Valid StudentPersonalInfo student_personal_info, 
      BindingResult result, ModelAndView model) { 
     model.addObject("countries", getCountries()); 
     if (result.hasErrors()) { 
      logger.info("From student personal info, there are " 
        + String.valueOf(this.getCountries().size()) 
        + " Countries"); //This prints 240 countries on the consule 
      return "student_personal_info"; 
     } 

     else 
      return "redirect:/display_program_of_study.tsegay"; 
    } 

其他所有我的配置能正常工作

回答

1

我猜你無法不還填充ModelAndView ,所以你需要使用另外一個參數類型:

@RequestMapping(value = "student_personal_info", method = RequestMethod.POST)   
public String PersonalFormSubmitted(
     @ModelAttribute("student_personal_info") @Valid StudentPersonalInfo student_personal_info, 
     BindingResult result, ModelMap model) { ... } 
+0

謝謝,這解決了這個問題。 – tkt986 2011-01-09 14:04:07

1

的問題是,你不能填充ModelAndView參數

您需要將您的方法簽名更改爲ModelMap而不是ModelAndView

@RequestMapping(value = "student_personal_info", method = RequestMethod.POST) 
    public String PersonalFormSubmitted(
      @ModelAttribute("student_personal_info") @Valid StudentPersonalInfo student_personal_info, 
      BindingResult result, ModelMap model) { 

BTW:ModelAndView甚至沒有在Spring reference提到過的有效參數。它接縫只是一個有效的返回類型。

在你的特殊情況下,你也可以考慮使用ModelAtttribute方法來填充你的模型:

@ModelAttribute("countries") 
public Collection<Country> populateCountries() { 
    return getCountries(); 
} 
...