* Remove unused member which was also causing a false positive sonar issue. Fixes sonar issue https://sonarcloud.io/project/issues?open=AY3gHwu5DIZTZkppqVEG&id=iluwatar_java-design-patterns * Fixes sonar issue https://sonarcloud.io/project/issues?open=AXK0OzDA-CiGJS70dLki&id=iluwatar_java-design-patterns related to "Refactor the code of the lambda to not have multiple invocations throwing the same checked exception." Also, updated the code to use Instant and Duration to deal with time instead of int. Added the awaitility library to perform assertions in test which is more reliable than using Thread.sleep directly to wait for events to happen. * checkstyle fix * Add sneaky throws to fix sonar lint issue. This is fine as the newFile method is not being tested but instead the new SimpleFileWriter(...) is. * The first booking needs to happen outside the assertions. Fixed other warnings * Use records to pass around related objects instead of using a large number of individual params, which sonar did not like. * Checkstyle fixes * checkstyle fixes * Remove complexity to keep sonar happy. * Split into different methods to reduce complexity. Could be broken down even further but currently Sonar is happy. * Move files to correct package * Add valid assertions to tests * rename constants to avoid confusion * Sonar warning related to cognitive complexity can be suppressed as the methods are quite generic and not functional enough to be separated out. * Use constants to keep Sonar happy * Use correct constant naming conventions * Use correct constant naming conventions * Use lombok to define noargsconstructor * Use a single method to do the logging * Remove unused constructor and redundant method * Use a reusable method for logging
title, categories, language, tags
| title | categories | language | tags | |
|---|---|---|---|---|
| Page Controller | Structural | en |
|
Name / classification
Page Controller
Intent
It is an approach of one page leading to one logical file that handles actions or requests on a website.
Explanation
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.
@Slf4j
@Controller
@Component
public class SignupController {
SignupView view = new SignupView();
/**
* Signup Controller can handle http request and decide which model and view use.
*/
SignupController() {
}
/**
* Handle http GET request.
*/
@GetMapping("/signup")
public String getSignup() {
return view.display();
}
/**
* Handle http POST request and access model and view.
*/
@PostMapping("/signup")
public String create(SignupModel form, RedirectAttributes redirectAttributes) {
LOGGER.info(form.getName());
LOGGER.info(form.getEmail());
redirectAttributes.addAttribute("name", form.getName());
redirectAttributes.addAttribute("email", form.getEmail());
redirectAttributes.addFlashAttribute("userInfo", form);
return view.redirect(form);
}
}
Here's Signup model and view that are handled by Signup controller.
@Component
@Getter
@Setter
public class SignupModel {
private String name;
private String email;
private String password;
public SignupModel() {
}
}
@Slf4j
public class SignupView {
public SignupView() {
}
public String display() {
LOGGER.info("display signup front page");
return "/signup";
}
/**
* redirect to user page.
*/
public String redirect(SignupModel form) {
LOGGER.info("Redirect to user page with " + "name " + form.getName() + " email " + form.getEmail());
return "redirect:/user";
}
}
Here's User Controller to handle Get request in a user page.
@Slf4j
@Controller
public class UserController {
UserView view = new UserView();
public UserController() {}
/**
* Handle http GET request and access view and model.
*/
@GetMapping("/user")
public String getUserPath(SignupModel form, Model model) {
model.addAttribute("name", form.getName());
model.addAttribute("email", form.getEmail());
return view.display(form);
}
}
Here's User Model and View that are handled by User controller.
@Getter
@Setter
public class UserModel {
private String name;
private String email;
public UserModel() {}
}
@Slf4j
public class UserView {
/**
* displaying command to generate html.
* @param user model content.
*/
public String display(SignupModel user) {
LOGGER.info("display user html" + " name " + user.getName() + " email " + user.getEmail());
return "/user";
}
}
Class diagram
Applicability
Use the Page Controller pattern when
- you implement a site where most controller logic is simple
- you implement a site where particular actions are handled with a particular server page
