Search code examples
javaspring-mvcfreemarker

User Registration Form in HTML - Required String parameter 'user_id' is not present


I have an html page (format - FTL) in which I am trying to send data to a controller method. I am only trying to print the items that the user typed, nothing fancy for the beginning. I get the error - Required String parameter 'user_id' is not present

I work with spring mvc and freemarker.

This is the signup.ftl file -

    <form method="POST", action="GET">
  <div class="container">
    <h1>Register</h1>
    <p>Please fill in this form to create an account.</p>
    <hr>

    <label for="email" id="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" required>

    <label for="password" id="password"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="password" required>

    <label for="user_id" id="user_id"><b>User ID</b></label>
    <input type="text" placeholder="user_id" name="user_id" required>
    <hr>
    <p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p>

    <button type="submit" class="registerbtn">Register</button>
  </div>

  <div class="container signin">
    <p>Already have an account? <a href="#">Sign in</a>.</p>
  </div>
</form>

</body>
</html>

This is the controller -

@RequestMapping(value = "/signup", method = RequestMethod.GET)    
public String signup(@RequestParam("user_id")String user_id, @RequestParam("password")String password, @RequestParam("email")String email, Model model) {
    System.out.println("coming in controller" + user_id + " " + password);
    return "signup";
}

Solution

  • You are passing wrong action method and value.

    You have to match the controller method signature which is

    @RequestMapping(value = "/signup", method = RequestMethod.GET)
    

    Change form tag to below line of code.

    <form method="GET", action="signup">