为什么Spring MVC的4 submit按钮没有触发POST方法(Why Spring MVC

2019-10-21 03:49发布

这是我的JSP代码

<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>

这是我的控制器代码

@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";
}

请帮忙。 你能告诉我为什么processForm方法不会被触发?

提前致谢

Answer 1:

你应该修改方法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";
}


文章来源: Why Spring MVC 4 submit button not triggering post method