Skip to content

Commit 9e9b894

Browse files
committed
Add Controllers approach in getting started
1 parent 6b769b1 commit 9e9b894

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

_posts/2015-03-17-getting-started.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ order: 10
99
We provide a pippo-demo module that contains many demo applications (submodules): pippo-demo-basic and pippo-demo-crud are some.
1010
For a list with all demo please see [Demo](demo.html) section.
1111

12+
#### Routes approach
13+
1214
For [pippo-demo-basic]({{ site.demourl }}/pippo-demo-basic) you have two java files: [BasicDemo.java]({{ site.demourl }}/pippo-demo-basic/src/main/java/ro/pippo/demo/basic/BasicDemo.java) and [BasicApplication.java]({{ site.demourl }}/pippo-demo-basic/src/main/java/ro/pippo/demo/basic/BasicApplication.java)
1315
We split our application in two parts for a better readability.
1416

@@ -99,4 +101,55 @@ Open your internet browser and check the routes declared in Application:
99101
- `http://localhost:8338/negotiate`
100102
- `http://localhost:8338/template`
101103
104+
#### Controllers approach
105+
106+
Another approach to handling a request and producing a response is using Controllers. After routing has determined what controller to use, an action method will be invoked.
107+
In Pippo, controllers are instances of [Controller]({{ site.codeurl }}/pippo-controller-parent/pippo-controller/src/main/java/ro/pippo/controller/Controller.java).
108+
109+
Defining a new controller is simple:
110+
111+
```java
112+
@Path("/contacts")
113+
public class ContactsController extends Controller {
114+
115+
@GET("/?")
116+
public void index() {
117+
List<Contact> contacts = contactService.getContacts();
118+
getResponse().bind("contacts", contacts).render("contacts");
119+
}
120+
121+
@GET("/{id: [0-9]+}")
122+
public void getContact(@Param int id) {
123+
Contact contact = contactService.getContact(id);
124+
getResponse().bind("contact", contact).render(contact);
125+
}
126+
127+
@GET("/text")
128+
@Named("text")
129+
@Produces(Produces.TEXT)
130+
@NoCache
131+
public void complex(@Param int id, @Param String action, @Header String host, @Session String user) {
132+
// do something
133+
}
134+
135+
}
136+
```
137+
138+
Methods attached to the controller (for example `index` from above snippet) are known as action methods. When Pippo receives a request, it will create a new instance of the controller and call the appropriate action method.
139+
You can register a controller's action in application with a simple line:
140+
141+
```java
142+
public class BasicApplication extends ControllerApplication {
143+
144+
@Override
145+
protected void onInit() {
146+
// add controller(s)
147+
addControllers(ContactsController.class); // one instance for EACH request
148+
// or
149+
addControllers(new ContactsController()); // one instance for ALL requests
150+
}
151+
152+
}
153+
```
154+
102155
For a detailed overview please see section [Under the hood](/dev/under-the-hood.html).

0 commit comments

Comments
 (0)