amit_vickey
amit_vickey

Reputation: 45

POST Method not working in Spring MVC

I have created a login form

<form class="login100-form validate-form p-b-33 p-t-5" method="POST">
<div class="wrap-input100 validate-input" data-validate = "Enter username">
    <input class="input100" type="text" name="username" placeholder="User name">
    <span class="focus-input100" data-placeholder="&#xe82a;"></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Enter password">
    <input class="input100" type="password" name="pass" placeholder="Password">
    <span class="focus-input100" data-placeholder="&#xe80f;"></span>
</div>
<div class="container-login100-form-btn m-t-32">
    <input class="login100-form-btn" type="button" onclick="location.href='eLibrary/login'" value="Login" >
</div>

And my controller function is

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

But, when I'm trying to login, it's giving error

HTTP Status 405 - Method Not Allowed

Request method 'GET' not supported

I even have tried

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

But in the above case, request.getParameter("username") is giving null.

Can anyone please help me out.

Upvotes: 0

Views: 1746

Answers (2)

Adam Lesiak
Adam Lesiak

Reputation: 501

Update the line:

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

with remove the onclick:

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

and update the FORM line to:

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

Onclick will always send a GET request. If you wanna do POST with javascript you have to use AJAX action.

Upvotes: 1

Nidhi257
Nidhi257

Reputation: 977

Update below line

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

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

Upvotes: 0

Related Questions