Home > Enterprise >  Cannot get value from simple form user input in Spring boot application?
Cannot get value from simple form user input in Spring boot application?

Time:11-27

I'm trying to implement a login form in a Spring boot application. It has an email and a password field. The email field failed to get user input, here is the form:

  <form th:action="@{/login}" method="get" th:object="${loginForm}" style="max-width: 600px; margin: 0 auto;">
    <div >

      <div >
        <label >E-mail: </label>
        <div >
          <input type="text" th:field="*{email}" name="q"  required />
        </div>
      </div>

      <div >
        <label >Password: </label>
        <div >
          <input type="password" th:field="*{password}"  required/>
        </div>
      </div>

      <div>
        <button type="submit" >Log in</button>
      </div>
    </div>
  </form>

Here is the controller:

    @GetMapping("login")
    public ModelAndView login(Model model, @RequestParam(name = "q", required = false) Optional<String> email) {
        Optional<UserDto> aUser;
        System.out.println(email);
        if (email.isPresent()) {
            aUser = userService.getAUserByEmail(email.get());
            model.addAttribute("user", aUser);
            var mv = new ModelAndView("user/user-list", model.asMap());
            return mv;
        } else {
            model.addAttribute("loginForm", new LoginForm());
            return new ModelAndView("/login/login-form", model.asMap());
        }
    }

I thought the @RequestParam(name = "q") and name="q" in html would do the job, but I always get Optional.empty for email. Any idea what's wrong here?

UPDATE: From the answers I changed controller to this:

    @GetMapping("login")
    public ModelAndView login(Model model, LoginForm loginForm) {
        Optional<UserDto> aUser;
        if (loginForm.getEmail() != null) {
            aUser = userService.getAUserByEmail(loginForm.getEmail());
            model.addAttribute("user", aUser);
            var mv = new ModelAndView("user/user-list", model.asMap());
            return mv;
        } else {
            model.addAttribute("loginForm", new LoginForm());
            return new ModelAndView("/login/login-form", model.asMap());
        }
    }

login-form.html to this:

  <form th:action="@{/login}" method="get" th:object="${loginForm}" style="max-width: 600px; margin: 0 auto;">
    <div >

      <div >
        <label >E-mail: </label>
        <div >
          <input type="text" th:field="*{email}"  required />
        </div>
      </div>

      <div >
        <label >Password: </label>
        <div >
          <input type="password" th:field="*{password}"  required/>
        </div>
      </div>

      <div>
        <button type="submit" >Log in</button>
      </div>
    </div>
  </form>

I also have LoginForm.java like this

@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginForm {
    private String email;
    private String password;
}

but still not getting user email field input?

CodePudding user response:

The way you have set up your form, it's mapping the value of your email input field to the property email (that's what th:field="*{email}" means) of an object called loginForm (that's what th:object="${loginForm}" means). Neither of these seem to be used or even exist in your login() method. You need to either change what you use in your controller to match what you have in your Thymeleaf template, or change your Thymeleaf template to actually reference what you are using in your controller.

CodePudding user response:

The problem in your code is located under th:object="${loginForm}"

With this you inform spring to bind the data sent from the form into an object named loginForm.

So Spring actually expects the controller to be

@GetMapping("login")
    public ModelAndView login(Model model, LoginForm loginForm) {
   ....

and inside LoginForm a field named email will contain the value sent from the form, as you have declared with <input type="text" th:field="*{email}" .... >

If you don't want the data to be bound into an object from Spring Mvc then

  1. remove the th:object="${loginForm}"

  2. use the

     <input type="text" th:name="q"  required />
    

    and then the controller will receive the sent value as a query parameter

        @GetMapping("login")
        public ModelAndView login(Model model, @RequestParam(name = 
        "q", required = false) Optional<String> email) {
    
  • Related