I am trying to get user inputs from form on a Spring Boot application. What do I need to do?
I implement a login controller uses a @RequestParam
annotation to request the data for validation but getting a result that parameter is not present.
// LoginController.java
@PostMapping(path = "/login")
public String userLogin(@RequestParam("email") String email,
@RequestParam("password") String password) {
// validate username and password
User user = userRepository.findUserByEmail(email);
System.out.println(user.getEmail()+" "+user.getPassword());
if(!(user.getEmail()==email) || !(user.getPassword()==password)){
logger.info("Invalid user details or not register");
return "login failed";
}
try{
loginService.login(email, password);
logger.info("User successfully login");
return "redirect:dashboard/profile";
}catch (Exception ex){
logger.info("",ex);
}
return "Successfully logged in ..";
}
Login.html
<form th:action="@{/login}" method="post" class="form-horizontal" role="form">
<form:hidden path/>
<div class="form-group">
<div class="mb-3">
<input type="email" class="form-control" name="email" id="email" placeholder="[email protected]" required="required">
</div>
</div>
<div class="form-group">
<div class="mb-3">
<input type="password" class="form-control" name="password" id="password" placeholder="" required="required">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<label>
<input type="checkbox"> Remember me
</label>
<button type="submit" class="btn btn-success">Login</button>
</div>
</div>
</form>
I expect the user email and password before he can login but get
2019-07-27 20:30:54.393 WARN 2900 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'email' is not present]
requestBody
and bind data in a POJO to use later. – user404