关于html:POST方法无法在Spring MVC中运行

POST Method not working in Spring MVC

我创建了一个登录表单

1
2
3
4
5
6
7
8
9
10
11
<form class="login100-form validate-form p-b-33 p-t-5" method="POST">

    <input class="input100" type="text" name="username" placeholder="User name">
    <span class="focus-input100" data-placeholder="&#xe82a;"></span>


    <input class="input100" type="password" name="pass" placeholder="Password">
    <span class="focus-input100" data-placeholder="&#xe80f;"></span>


    <input class="login100-form-btn" type="button" onclick="location.href='eLibrary/login'" value="Login">

我的控制器功能是

1
2
3
4
5
6
@RequestMapping(value ="/login", method = RequestMethod.POST)
public String login(HttpServletRequest request, HttpServletResponse response) {
    String userName = request.getParameter("username");
    String pass = request.getParameter("pass");
    return"list-books";
}

但是,当我尝试登录时,它会给出错误

HTTP Status 405 - Method Not Allowed

Request method 'GET' not supported

我甚至试过了

1
2
3
4
5
6
@PostMapping("/login")
public String login(HttpServletRequest request, HttpServletResponse response) {
    String userName = request.getParameter("username");
    String pass = request.getParameter("pass");
    return"list-books";
}

但在上面的例子中,request.getParameter("username")给出了null。

任何人都可以帮助我。


更新行:

1
<input class="login100-form-btn" type="button" onclick="location.href='eLibrary/login'" value="Login">

删除onclick:

1
<input class="login100-form-btn" type="submit" value="Login">

并将FORM行更新为:

1
<form class="login100-form validate-form p-b-33 p-t-5" method="POST" action="eLibrary/login">

Onclick将始终发送GET请求。 如果您想使用javascript进行POST,则必须使用AJAX操作。


更新下面的行

1
<input class="login100-form-btn" type="button" onclick="location.href='eLibrary/login'" value="Login">