You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
16
23
17
24
## Explanation
18
25
19
26
Real-world example
20
27
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.
23
31
24
32
In plain words
25
33
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.
28
35
29
36
**Programmatic Example**
30
37
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.
32
43
33
44
```java
34
-
@Slf4j
35
45
@Controller
36
46
@Component
37
47
publicclassSignupController {
38
48
SignupView view =newSignupView();
39
-
/**
40
-
* Signup Controller can handle http request and decide which model and view use.
41
-
*/
42
-
SignupController() {
43
-
}
44
49
45
-
/**
46
-
* Handle http GET request.
47
-
*/
48
50
@GetMapping("/signup")
49
51
publicStringgetSignup() {
50
52
return view.display();
51
53
}
52
54
53
-
/**
54
-
* Handle http POST request and access model and view.
Here's Signup model and view that are handled by Signup controller.
68
-
69
-
```java
70
-
@Component
71
-
@Getter
72
-
@Setter
73
-
publicclassSignupModel {
74
-
privateString name;
75
-
privateString email;
76
-
privateString password;
77
-
78
-
publicSignupModel() {
79
-
}
80
-
}
81
-
```
82
-
83
-
```java
84
-
@Slf4j
85
-
publicclassSignupView {
86
-
publicSignupView() {
87
-
}
88
-
89
-
publicStringdisplay() {
90
-
LOGGER.info("display signup front page");
91
-
return"/signup";
92
-
}
93
-
94
-
/**
95
-
* redirect to user page.
96
-
*/
97
-
publicStringredirect(SignupModelform) {
98
-
LOGGER.info("Redirect to user page with "+"name "+ form.getName() +" email "+ form.getEmail());
99
-
return"redirect:/user";
100
-
}
101
-
}
102
-
```
103
64
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.
105
66
106
67
```java
107
68
@Slf4j
108
69
@Controller
109
70
publicclassUserController {
110
71
UserView view =newUserView();
111
72
112
-
publicUserController() {}
113
-
114
-
/**
115
-
* Handle http GET request and access view and model.
@@ -123,40 +79,88 @@ public class UserController {
123
79
}
124
80
```
125
81
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
+
127
84
```java
85
+
@Component
128
86
@Getter
129
87
@Setter
130
-
publicclassUserModel {
88
+
publicclassSignupModel {
131
89
privateString name;
132
90
privateString email;
91
+
privateString password;
92
+
}
133
93
134
-
publicUserModel() {}
94
+
@Getter
95
+
@Setter
96
+
publicclassUserModel {
97
+
privateString name;
98
+
privateString email;
135
99
}
136
100
```
137
101
102
+
4.**SignupView and UserView**: These are the views used by the controllers. They determine how the data is presented to the user.
103
+
138
104
```java
105
+
@Slf4j
106
+
publicclassSignupView {
107
+
publicStringdisplay() {
108
+
return"/signup";
109
+
}
110
+
111
+
publicStringredirect(SignupModelform) {
112
+
return"redirect:/user";
113
+
}
114
+
}
115
+
139
116
@Slf4j
140
117
publicclassUserView {
141
-
/**
142
-
* displaying command to generate html.
143
-
* @param user model content.
144
-
*/
145
118
publicStringdisplay(SignupModeluser) {
146
-
LOGGER.info("display user html"+" name "+ user.getName() +" email "+ user.getEmail());
147
119
return"/user";
148
120
}
149
121
}
150
122
```
151
123
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
+
152
126
## Class diagram
153
-

127
+
128
+

154
129
155
130
## 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.
0 commit comments