Skip to content

Commit 06d90b0

Browse files
committed
Improve Routes section
1 parent c1f30ab commit 06d90b0

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

_posts/2015-05-05-routes.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,33 @@ GET("/", new RouteHandler() {
2525
GET("/", routeContext -> routeContext.send("Hello World"));
2626
```
2727

28-
Routes in Pippo are created using methods named after HTTP verbs. For instance, in the previous example, we created a route to handle GET requests to the root of the website. You have a corresponding method in Application for all commonly used HTTP verbs (GET, POST, DELETE, HEAD, PUT). For a basic website, only GET and POST are likely to be used.
28+
Routes in Pippo are created using methods named after HTTP verbs. For instance, in the previous example, we created a route to handle GET requests to the root of the website. You have a corresponding method in Application for all commonly used HTTP verbs (__GET__, __POST__, __DELETE__, __HEAD__, __PUT__, __PATCH__, __CONNECT__ and __OPTIONS__). For a basic website, only GET and POST are likely to be used.
29+
Pippo comes with a "pseudo" verb `ANY` that meant the route applied to all HTTP methods/verbs.
30+
```java
31+
ANY("/example", new CSRFHandler());
32+
```
2933

3034
The route that is defined first takes precedence over other matching routes. So the ordering of routes is crucial to the behavior of an application.
3135

3236
Each defined route has an __urlPattern__.
3337
The route can be static or dynamic:
3438

3539
- `static` ("/", "/hello", "/contacts/1")
36-
- `dynamic` (regex: "/.*" or parameterized: "/contact/{id}", "/contact/{id: [0-9]+}")
40+
- `dynamic`
41+
- regex ("/.*")
42+
- parameterized ("/contact/{id}", "/contact/{id: [0-9]+}")
3743

3844
As you can see, it's easy to create routes with parameters. A parameter is wrapped by curly braces `{name}` and can optionally specify a regular expression.
3945

4046
You can retrieve the path parameter value for a request in type safe mode using:
4147

4248
```java
4349
GET("/contact/{id}", routeContext -> {
50+
// retrieve some parameters from request
4451
int id = routeContext.getParameter("id").toInt(0);
4552
String action = routeContext.getParameter("action").toString("new");
4653

54+
// render a template using above parameters
4755
Map<String, Object> model = new HashMap<>();
4856
model.put("id", id);
4957
model.put("action", action)
@@ -57,6 +65,37 @@ If you want to be more rigorous you can use something like:
5765
GET("/contact/{id: [0-9]+}", routeContext -> ... );
5866
```
5967

68+
Pippo comes with some builtin route handlers that can be useful for you:
69+
- `TemplateHandler` (render a template)
70+
- `RedirectHandler` (redirect to another route/url)
71+
- `TrailingSlashHandler` (add or remove the trailing slash to/from path)
72+
- `CSRFHandler` (generates and validates a CSRF token)
73+
- `UrlResourceHandler`
74+
- `ClasspathResourceHandler`
75+
- `PublicResourceHandler`
76+
- `WebjarsResourceHandler`
77+
- `MeteredHandler`, `TimedHandler`, `CountedHandler` (metrics)
78+
79+
and the list can continue.
80+
81+
Now, we will talk a little bit about `TrailingSlashHandler` because is important and the problem that it solves is not so visible.
82+
If we add a route as in the below example
83+
```java
84+
GET("/test", routeContext -> routeContext.send("Test"));
85+
```
86+
and we type `localhost/test/` in browser we will se that the result is 404 (not found) http code.
87+
Of course that `localhost/test` works OK. Explanation for 404 on `localhost/test/` is that we registered a route
88+
only for `/test` path.
89+
To obtain the same result for both urls (with and without `\` character at end) we will use `TrailingSlashHandler` (as before filter)
90+
```java
91+
// add trailing slash filter
92+
ANY("/.*", new TrailingSlashHandler(false)); // remove trailing slash
93+
94+
// add test route
95+
GET("/test", routeContext -> routeContext.send("Test"));
96+
```
97+
Now, if you type `localhost/test` or `localhost/test/` in browser, the result is the same.
98+
6099
#### Named routes
61100

62101
`Named` routes make referring to routes when generating redirects or URLs more convenient. You may specify a name for a route like so:
@@ -184,4 +223,4 @@ In above snippet I added (in DemoApplication) `AdminRoutes` as route group which
184223
I don't need to know when I create a RouteGroup where the group will be mount. For example I can have a modular web application where each plugin comes with own independent routes (user management -> `UserRoutes`, customer management -> `CustomerRoutes`).
185224

186225
You can see a more complex example in [Matilda](https://github.com/decebals/matilda/blob/master/src/main/java/ro/fortsoft/matilda/PippoApplication.java#L108) (a real life application built with Pippo).
187-
226+

0 commit comments

Comments
 (0)