Skip to content

Commit 4a50a84

Browse files
committed
Improve Modularity section
1 parent b8fc15a commit 4a50a84

File tree

1 file changed

+77
-7
lines changed

1 file changed

+77
-7
lines changed

_posts/2015-03-17-modularity.md

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ order: 120
88

99
Pippo was designed since the first version with the modularity in mind. Many aspects (extension points) of this framework can be changed:
1010

11-
- `WebServer` (using _Pippo setServer()_ or auto discovery mechanism)
12-
- `TemplateEngine` (using _Application.setTemplateEngine()_ or auto discovery mechanism)
11+
- `WebServer` (using _Pippo#setServer()_ or auto discovery mechanism)
12+
- `TemplateEngine` (using _Application#setTemplateEngine()_ or auto discovery mechanism)
1313
- `Router` (using _Application.setRouter()_)
14-
- `ErrorHandler` (using _Application.setErrorHandler()_)
14+
- `ErrorHandler` (using _Application#setErrorHandler()_)
15+
- `ContentTypeEngine` (using _Application#registerContentTypeEngine_)
1516

16-
Also you can set some parameters related to file upload process (_Application.setUploadLocation()_ and _Application.setMaximumUploadSize()_).
17+
Also you can set some parameters related to file upload process (_Application#setUploadLocation()_ and _Application#setMaximumUploadSize()_).
1718
You can modify some settings for an embedded WebServer using _WebServerSettings_.
1819

19-
We chose the Service Loader mechanism from Java as built in modules system in Pippo because is a standard and easy to use.
20+
We chose the ServiceLoader mechanism from Java as built in modules system in Pippo because is a standard and easy to use.
2021
You can create a modular application using [ServiceLocator]({{ site.coreurl }}/src/main/java/ro/pippo/core/util/ServiceLocator.java) class (trivial wrapper over Service Loader concept).
2122

2223
To improve the modularity mechanism, we added the concept of [Initializer]({{ site.coreurl }}/src/main/java/ro/pippo/core/Initializer.java).
@@ -60,7 +61,12 @@ public class ContactInitializer implements Initializer {
6061
application.GET("/contacts", routeContext -> routeContext.render("contacts"));
6162

6263
// show contact page for the contact with id specified as path parameter
63-
application.GET("/contact/{id}", routeContext -> routeContext.render("contact"));
64+
application.GET("/contact/{id}", routeContext -> {
65+
int id = routeContext.getParameter("id").toInt(0);
66+
Map<String, Object> model = new HashMap<>();
67+
model.put("id", id);
68+
routeContext.render("contact", model);
69+
});
6470
}
6571

6672
@Override
@@ -83,7 +89,12 @@ public class UserInitializer implements Initializer {
8389
application.GET("/users", routeContext -> routeContext.render("users"));
8490

8591
// show user page for the user with id specified as path parameter
86-
application.GET("/user/{id}", routeContext -> routeContext.render("user"));
92+
application.GET("/user/{id}", routeContext -> {
93+
int id = routeContext.getParameter("id").toInt(0);
94+
Map<String, Object> model = new HashMap<>();
95+
model.put("id", id);
96+
routeContext.render("user", model);
97+
});
8798
}
8899

89100
@Override
@@ -94,4 +105,63 @@ public class UserInitializer implements Initializer {
94105
}
95106
```
96107

108+
The above example can be improved as readability if we use the `RouteGroup` concept described in [Routes](/doc/routes.html) section
109+
```java
110+
public class ContactRoutes extends RouteGroup {
111+
112+
public UserRoutes() {
113+
super("/contact");
114+
115+
GET("/", routeContext -> routeContext.render("contacts"));
116+
GET("/{id: [0-9]+}", routeContext -> {
117+
int id = routeContext.getParameter("id").toInt(0);
118+
Map<String, Object> model = new HashMap<>();
119+
model.put("id", id);
120+
routeContext.render("contact", model);
121+
});
122+
}
123+
124+
}
125+
```
126+
127+
```java
128+
@MetaInfServices
129+
public class ContactInitializer implements Initializer {
130+
131+
@Override
132+
public void init(Application application) {
133+
application.addRouteGroup(new ContactRoutes());
134+
}
135+
}
136+
```
137+
138+
```java
139+
public class UserRoutes extends RouteGroup {
140+
141+
public UserRoutes() {
142+
super("/user");
143+
144+
GET("/", routeContext -> routeContext.render("users"));
145+
GET("/{id: [0-9]+}", routeContext -> {
146+
int id = routeContext.getParameter("id").toInt(0);
147+
Map<String, Object> model = new HashMap<>();
148+
model.put("id", id);
149+
routeContext.render("user", model);
150+
});
151+
}
152+
153+
}
154+
```
155+
156+
```java
157+
@MetaInfServices
158+
public class UserInitializer implements Initializer {
159+
160+
@Override
161+
public void init(Application application) {
162+
application.addRouteGroup(new UserRoutes());
163+
}
164+
}
165+
```
166+
97167
>__NOTE__ The order of the initializers depends on the order of the jars in the classpath.

0 commit comments

Comments
 (0)