Why Spring MVC 4 submit button not triggering post

2019-07-31 23:51发布

This is my JSP code

<form:form action="multiplebuttons.html"  commandName="buttonForm">
<table>
<tr>
        <td width="7%">Name:</td>
        <td width="11%"><form:input path="name" /></td><td width="82%"><form:errors cssStyle="color:red" path="name"/></td>                 
    </tr>
<tr>
        <td><input type="submit" value="update" /></td><td><input type="submit" value="save" /></td>
    </tr>
</table>

</form:form>

This is my Controller code

@Autowired
@Qualifier("formValidator")
private Validator validator;

@InitBinder
private void initBinder(WebDataBinder binder) {
    binder.setValidator(validator);
}

@RequestMapping(value="/views", method = RequestMethod.GET)
public ModelAndView getdata() {
    System.out.println("do something");
    ModelAndView model = new ModelAndView("views");

    model.addObject("name", "Girish");
    return model;
}

@RequestMapping(value="/multiplebuttons", method = RequestMethod.GET)
public String showForm(Map model) {
    System.out.println("Show form");
    ButtonForm Form = new ButtonForm();
    model.put("buttonForm", Form);
    return "multiplebuttons";
}

@RequestMapping(method = RequestMethod.POST)
public String processForm(  @Validated  ButtonForm buttonForm, BindingResult result,
        Map model) {
    System.out.println("Processing..........");             

    if (result.hasErrors()) {
        System.out.println("Has errors");
        return "multiplebuttons";
    }
    return "redirect:views";
}

@RequestMapping("/{name}")
public String editItem(@PathVariable("name") String name, Model model){
    ButtonForm form = new ButtonForm();

    form.setName(name);

    model.addAttribute("buttonForm", form);
    return "multiplebuttons";
}

Please help. Can you please tell me why processForm method is not triggered?

Thanks in advance

1条回答
爷、活的狠高调
2楼-- · 2019-07-31 23:59

you should modify method processForm():

@RequestMapping(value="/multiplebuttons", method = RequestMethod.POST)
public String processForm(  @Validated  ButtonForm buttonForm, BindingResult result,
        Map model) {
    System.out.println("Processing..........");             

    if (result.hasErrors()) {
        System.out.println("Has errors");
        return "multiplebuttons";
    }
    return "redirect:views";
}
查看更多
登录 后发表回答