Write code for the controller class (RegisterController.java) as follows:
File: src/main/java/net/codejava/spring/controller/RegisterController.java
package net.codejava.spring.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.codejava.spring.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/register")
public class RegisterController {
@RequestMapping(method = RequestMethod.GET)
public String viewRegistration(Map<String, Object> model) {
User userForm = new User();
model.put("userForm", userForm);
List<String> professionList = new ArrayList<>();
professionList.add("Developer");
professionList.add("Designer");
professionList.add("IT Manager");
model.put("professionList", professionList);
return "Registration";
}
@RequestMapping(method = RequestMethod.POST)
public String processRegistration(@ModelAttribute("userForm") User user,
Map<String, Object> model) {
// implement your own registration logic here...
// for testing purpose:
System.out.println("username: " + user.getUsername());
System.out.println("password: " + user.getPassword());
System.out.println("email: " + user.getEmail());
System.out.println("birth date: " + user.getBirthDate());
System.out.println("profession: " + user.getProfession());
return "RegistrationSuccess";
}
}
We can see that this controller is designed to handle the request URL /register:
@RequestMapping(value = "/register")
We implement two methods viewRegistration() and processRegistration() to handle the GET and POST requests, respectively. Writing handler methods in Spring is very flexible, as we can freely choose our own method names and necessary parameters. Let’s look at each method of the above controller class in details:
viewRegistration(): in this method we create a model object and put it into the model map with the key “userForm”:
User userForm = new User();
model.put("userForm", userForm);
This creates a binding between the specified object with the form in the view returned by this method (which is the registration form). Note that the key “userForm” must match value of the commandName attribute of the tag.
Another interesting point is that we create a list of Strings and put it into the model map with the key “professionList”:
List<String> professionList = new ArrayList<>();
professionList.add("Developer");
professionList.add("Designer");
professionList.add("IT Manager");
model.put("professionList", professionList);
This collection will be used by the tag in the Registration.jsp page in order to render the profession dropdown list dynamically.
Finally this method returns a view name (“Registration”) which will be mapped to the registration form page above.
processRegistration(): this method handles the form submission (via POST request). The important parameter here is:
@ModelAttribute("userForm") User user
This will make the model object which is stored under the key “userForm” in the model map available to the method body. Again, the key “userForm” must match value of the commandName attribute of the tag.
When the form is submitted, Spring automatically binds the form’s field values to the backing object in the model, thus we can access the form values inputted by the user through this backing object like this:
System.out.println("username: " + user.getUsername());
For demo purpose, this method only prints out details of the User object, and finally returns a view name of the success page (“RegistrationSuccess”).