出现意外错误(type = Bad Request,status = 400),缺少参数?

时间:2015-05-25 14:39:51

标签: javascript html spring

尝试RequestMapping一个提交,但我收到了这个错误,我真的不明白为什么,因为我认为每个人都提供了:

There was an unexpected error (type=Bad Request, status=400).
Required Integer parameter 'idVac' is not present

[...]

function deleteRecord(idVac) {

var newwindow;
newwindow=window.confirm("Are you sure");
if(newwindow == true) {
    alert("Condition is true");
    var form = document.createElement("form");
    form.setAttribute("name", "form");
    form.setAttribute("method", "post");
    form.setAttribute("action", "/vacEditDelete");
    var vacancyId = document.createElement("input");
    vacancyId.setAttribute("type", "hidden");
    vacancyId.setAttribute("name", "vacancyId");
    vacancyId.setAttribute("value", idVac);

    var csrf = document.createElement("input");
    csrf.setAttribute("type", "hidden");
    csrf.setAttribute("name", "${_csrf.parameterName}");
    csrf.setAttribute("value", "${_csrf.token}");
    form.appendChild(csrf);

    document.body.appendChild(form);
    form.submit();

}
}

方法是在我的Controller中映射此方法:

@RequestMapping(value = "/vacEditDelete", method = RequestMethod.POST)
public String deleteVacancy(@AuthenticationPrincipal User currentuser,
                            @RequestParam(value = "idVac", required = true) Integer idVac){
    System.out.println("Method called");
    vacService.deleteVacancyByID(idVac);

    return "redirect:/vacEdit";


}

1 个答案:

答案 0 :(得分:1)

在你的代码中

你期待@RequestParam(值= “idVac”,必需= true)

在JS中你发送vacancyId.setAttribute(“name”,“vacancyId”);

参考Spring Docs

元素明细

value

public abstract String value

The name of the request parameter to bind to.

Default: