Page Controller
It is an approach of one page leading to one logical file that handles actions or requests on a website.
Real-world example
In a shopping website, there is a signup page to register a user profile. After finishing to signup, the signup page will be redirected to a user page to show the user’s registered information.
In plain words
Page controller manages HTTP requests and data in a specific page using MVC idea. The idea is that one page contains one Controller that handles Model and View.
Programmatic Example
Here’s Signup controller when a user signup their information for a website.
1@Slf4j
2@Controller
3@Component
4public class SignupController {
5 SignupView view = new SignupView();
6 /**
7 * Signup Controller can handle http request and decide which model and view use.
8 */
9 SignupController() {
10 }
11
12 /**
13 * Handle http GET request.
14 */
15 @GetMapping("/signup")
16 public String getSignup() {
17 return view.display();
18 }
19
20 /**
21 * Handle http POST request and access model and view.
22 */
23 @PostMapping("/signup")
24 public String create(SignupModel form, RedirectAttributes redirectAttributes) {
25 LOGGER.info(form.getName());
26 LOGGER.info(form.getEmail());
27 redirectAttributes.addAttribute("name", form.getName());
28 redirectAttributes.addAttribute("email", form.getEmail());
29 redirectAttributes.addFlashAttribute("userInfo", form);
30 return view.redirect(form);
31 }
32}
Here’s Signup model and view that are handled by Signup controller.
1@Component
2@Getter
3@Setter
4public class SignupModel {
5 private String name;
6 private String email;
7 private String password;
8
9 public SignupModel() {
10 }
11}
1@Slf4j
2public class SignupView {
3 public SignupView() {
4 }
5
6 public String display() {
7 LOGGER.info("display signup front page");
8 return "/signup";
9 }
10
11 /**
12 * redirect to user page.
13 */
14 public String redirect(SignupModel form) {
15 LOGGER.info("Redirect to user page with " + "name " + form.getName() + " email " + form.getEmail());
16 return "redirect:/user";
17 }
18}
Here’s User Controller to handle Get request in a user page.
1@Slf4j
2@Controller
3public class UserController {
4 UserView view = new UserView();
5
6 public UserController() {}
7
8 /**
9 * Handle http GET request and access view and model.
10 */
11 @GetMapping("/user")
12 public String getUserPath(SignupModel form, Model model) {
13 model.addAttribute("name", form.getName());
14 model.addAttribute("email", form.getEmail());
15 return view.display(form);
16 }
17}
Here’s User Model and View that are handled by User controller.
1@Getter
2@Setter
3public class UserModel {
4 private String name;
5 private String email;
6
7 public UserModel() {}
8}
1@Slf4j
2public class UserView {
3 /**
4 * displaying command to generate html.
5 * @param user model content.
6 */
7 public String display(SignupModel user) {
8 LOGGER.info("display user html" + " name " + user.getName() + " email " + user.getEmail());
9 return "/user";
10 }
11}
Use the Page Controller pattern when