简体   繁体   中英

How do i get form inputs to controller using @RequestParam Annotation

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="example@gmail.com" 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] .wsmsDefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'email' is not present]

You must send the paramater name from front end :

   const payload: FormData = new FormData();
   payload.append("email", emai);
   payload.append("password", password);

The issue because of bad API design and RequestParam mismatch in your URL.

Change your API to this:

@PostMapping(path = "/login") public String userLogin(@RequestBody Map<String, Object> user) {
          String username=user.get("user").toString();
          String password=user.get("password").toString();
}

And your request body JSON should be like this.

{
"user":"USER",
"password":"pass5&234rf"
}

This is the JSON payload(body) for the POST request from the client side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM