Skip to content

Commit a878938

Browse files
committed
docs: update page controller
1 parent 876dce4 commit a878938

File tree

1 file changed

+84
-80
lines changed

1 file changed

+84
-80
lines changed

page-controller/README.md

Lines changed: 84 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,75 @@
11
---
22
title: Page Controller
3-
categories: Structural
3+
categories: Architectural
44
language: en
55
tags:
6-
- Decoupling
6+
- API design
7+
- Business
8+
- Client-server
9+
- Decoupling
10+
- Enterprise patterns
11+
- Layered architecture
12+
- Presentation
13+
- Web development
714
---
815

9-
## Name / classification
16+
## Also known as
1017

11-
Page Controller
18+
* Dispatcher
1219

1320
## Intent
1421

15-
It is an approach of one page leading to one logical file that handles actions or requests on a website.
22+
The Page Controller pattern is intended to handle requests for a specific page or action within a web application, processing input, and determining the appropriate view for rendering the response.
1623

1724
## Explanation
1825

1926
Real-world example
2027

21-
> In a shopping website, there is a signup page to register a user profile.
22-
> After finishing to signup, the signup page will be redirected to a user page to show the user's registered information.
28+
> Imagine a large department store with multiple specialized counters: Customer Service, Returns, Electronics, and Clothing. Each counter has a dedicated staff member who handles specific tasks for that department.
29+
>
30+
> In this analogy, the department store is the web application, and each specialized counter represents a Page Controller. The Customer Service counter (Page Controller) handles customer inquiries, the Returns counter processes returns and exchanges, the Electronics counter assists with electronic goods, and the Clothing counter manages clothing-related requests. Each counter operates independently, addressing the specific needs of their respective department, just as each Page Controller handles requests for a specific page or action within the web application.
2331
2432
In plain words
2533

26-
> Page controller manages HTTP requests and data in a specific page using MVC idea.
27-
> The idea is that one page contains one Controller that handles Model and View.
34+
> The Page Controller pattern handles requests for specific pages or actions within a web application, processing input, executing business logic, and determining the appropriate view for rendering the response.
2835
2936
**Programmatic Example**
3037

31-
Here's Signup controller when a user signup their information for a website.
38+
The Page Controller design pattern is a pattern used in web development where each page of a website is associated with a class or function known as a controller. The controller handles the HTTP requests for that page and determines which model and view to use. This pattern is commonly used in MVC (Model-View-Controller) architectures.
39+
40+
In the provided code, we have an example of the Page Controller pattern implemented using Spring Boot in Java. Let's break it down:
41+
42+
1. **SignupController**: This is a Page Controller for the signup page. It handles HTTP GET and POST requests at the "/signup" path. The GET request returns the signup page, and the POST request processes the signup form and redirects to the user page.
3243

3344
```java
34-
@Slf4j
3545
@Controller
3646
@Component
3747
public class SignupController {
3848
SignupView view = new SignupView();
39-
/**
40-
* Signup Controller can handle http request and decide which model and view use.
41-
*/
42-
SignupController() {
43-
}
4449

45-
/**
46-
* Handle http GET request.
47-
*/
4850
@GetMapping("/signup")
4951
public String getSignup() {
5052
return view.display();
5153
}
5254

53-
/**
54-
* Handle http POST request and access model and view.
55-
*/
5655
@PostMapping("/signup")
5756
public String create(SignupModel form, RedirectAttributes redirectAttributes) {
58-
LOGGER.info(form.getName());
59-
LOGGER.info(form.getEmail());
6057
redirectAttributes.addAttribute("name", form.getName());
6158
redirectAttributes.addAttribute("email", form.getEmail());
6259
redirectAttributes.addFlashAttribute("userInfo", form);
6360
return view.redirect(form);
6461
}
6562
}
6663
```
67-
Here's Signup model and view that are handled by Signup controller.
68-
69-
```java
70-
@Component
71-
@Getter
72-
@Setter
73-
public class SignupModel {
74-
private String name;
75-
private String email;
76-
private String password;
77-
78-
public SignupModel() {
79-
}
80-
}
81-
```
82-
83-
```java
84-
@Slf4j
85-
public class SignupView {
86-
public SignupView() {
87-
}
88-
89-
public String display() {
90-
LOGGER.info("display signup front page");
91-
return "/signup";
92-
}
93-
94-
/**
95-
* redirect to user page.
96-
*/
97-
public String redirect(SignupModel form) {
98-
LOGGER.info("Redirect to user page with " + "name " + form.getName() + " email " + form.getEmail());
99-
return "redirect:/user";
100-
}
101-
}
102-
```
10364

104-
Here's User Controller to handle Get request in a user page.
65+
2. **UserController**: This is another Page Controller, this time for the user page. It handles HTTP GET requests at the "/user" path, returning the user page.
10566

10667
```java
10768
@Slf4j
10869
@Controller
10970
public class UserController {
11071
UserView view = new UserView();
11172

112-
public UserController() {}
113-
114-
/**
115-
* Handle http GET request and access view and model.
116-
*/
11773
@GetMapping("/user")
11874
public String getUserPath(SignupModel form, Model model) {
11975
model.addAttribute("name", form.getName());
@@ -123,40 +79,88 @@ public class UserController {
12379
}
12480
```
12581

126-
Here's User Model and View that are handled by User controller.
82+
3. **SignupModel and UserModel**: These are the data models used by the controllers. They hold the data to be displayed on the page.
83+
12784
```java
85+
@Component
12886
@Getter
12987
@Setter
130-
public class UserModel {
88+
public class SignupModel {
13189
private String name;
13290
private String email;
91+
private String password;
92+
}
13393

134-
public UserModel() {}
94+
@Getter
95+
@Setter
96+
public class UserModel {
97+
private String name;
98+
private String email;
13599
}
136100
```
137101

102+
4. **SignupView and UserView**: These are the views used by the controllers. They determine how the data is presented to the user.
103+
138104
```java
105+
@Slf4j
106+
public class SignupView {
107+
public String display() {
108+
return "/signup";
109+
}
110+
111+
public String redirect(SignupModel form) {
112+
return "redirect:/user";
113+
}
114+
}
115+
139116
@Slf4j
140117
public class UserView {
141-
/**
142-
* displaying command to generate html.
143-
* @param user model content.
144-
*/
145118
public String display(SignupModel user) {
146-
LOGGER.info("display user html" + " name " + user.getName() + " email " + user.getEmail());
147119
return "/user";
148120
}
149121
}
150122
```
151123

124+
In this example, the controllers (SignupController and UserController) are the Page Controllers. They handle the HTTP requests for their respective pages and determine which model and view to use. The models (SignupModel and UserModel) hold the data for the page, and the views (SignupView and UserView) determine how that data is presented. This separation of concerns makes the code easier to manage and maintain.
125+
152126
## Class diagram
153-
![alt text](./etc/page-controller.urm.png)
127+
128+
![Page Controller](./etc/page-controller.urm.png)
154129

155130
## Applicability
156-
Use the Page Controller pattern when
157-
- you implement a site where most controller logic is simple
158-
- you implement a site where particular actions are handled with a particular server page
131+
132+
* When developing a web application where each page or action needs specific processing.
133+
* When aiming to separate the request handling logic from the view rendering logic.
134+
* In scenarios where a clear separation of concerns between different layers (controller, view) is required.
135+
136+
## Known Uses
137+
138+
* Spring MVC (Java)
139+
* Apache Struts
140+
* JSF (JavaServer Faces)
141+
142+
## Consequences
143+
144+
Benefits:
145+
146+
* [Separation of Concerns](https://java-design-patterns.com/principles/#separation-of-concerns): Clearly separates the controller logic from the view, making the application easier to manage and maintain.
147+
* Reusability: Common logic can be reused across multiple controllers, reducing code duplication.
148+
* Testability: Controllers can be tested independently of the view, improving unit test coverage.
149+
150+
Trade-offs:
151+
152+
* Complexity: Can add complexity to the application structure, requiring careful organization and documentation.
153+
* Overhead: May introduce performance overhead due to additional layers of abstraction and processing.
154+
155+
## Related Patterns
156+
157+
* [Front Controller](https://java-design-patterns.com/patterns/front-controller/): Often used in conjunction with Page Controller to handle common pre-processing logic such as authentication and logging.
158+
* View Helper: Works alongside Page Controller to assist in preparing the view, often handling formatting and other presentation logic.
159+
* [Model-View-Controller (MVC)](https://java-design-patterns.com/patterns/model-view-controller/): Page Controller is a fundamental part of the MVC architecture, acting as the Controller.
159160

160161
## Credits
161-
- [Page Controller](https://www.martinfowler.com/eaaCatalog/pageController.html)
162-
- [Pattern of Enterprise Application Architecture](https://www.martinfowler.com/books/eaa.html)
162+
163+
* [Core J2EE Patterns: Best Practices and Design Strategies](https://amzn.to/4cAbDap)
164+
* [Page Controller - Martin Fowler](https://www.martinfowler.com/eaaCatalog/pageController.html)
165+
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
166+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)

0 commit comments

Comments
 (0)