开发者

spring form is not refilling on form submit

When I load this form, the countries table is populated from a database with 240 countries. if I submit it, with some empty required fields, the page reloads again with error messages. But i didnt get any country listed. I am using the same code to populate the list on the both the GET and POST methods -- see below

      <form:form commandName="student_personal_info" method="post">
               <table>
                        <tr>
                        <td><form:label path="country"开发者_JS百科>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";
    }

all other my configuration works fine


I guess you can't populate ModelAndView without returning it, so you need to use another argument type:

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


The problem is that you can not populate a ModelAndView parameter.

You need to change your method signature to you a ModelMap instead of a 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 is not even a mentioned valid parameter in the Spring reference. It seams only be a valid return type.

In your special case you could also consider to use a ModelAtttribute method to populate your model:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜