Considérons le contrôleur suivant :

app/controllers/Application.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package controllers;
 
import play.mvc.After;
import play.mvc.Before;
import play.mvc.Controller;
 
public class Application extends Controller {
 
    @Before
    static void before() {
        System.out.println("before");
        renderArgs.put("nom", "Michel");
    }
 
    @After
    static void after() {
        System.out.println("after");
        renderArgs.put("nom", "Serge");
    }
 
    public static void index() {
        System.out.println("index");
        redirect();
    }
    public static void redirect() {
        System.out.println("redirect");
        renderArgs.put("nom", "Robert");
        render();
    }
}

Ainsi que le template associé :
app/views/Application/redirect.html

1
2
3
4
#{extends 'main.html' /}
#{set title:'Home' /}
 
${nom}

Lequel des trois prénoms suivants, Michel, Serge et Robert, va t’il s’afficher dans votre navigateur à l’adresse http://localhost:9000 ?

La réponse est Robert.

Conclusion : Un renderArgs.put dans un @After ne sert à rien!

  4 Responses to “Play! Framework Tips : un renderArgs.put dans un @After sert t’il à quelque chose”

  1. Bien sur si quelqu’un à une explication concrète qu’il n’hésite pas à le partager ici.

  2. Hi, I’m Jitesh, and I work for Packt. I am looking for reviewers for our recently published book, Play Framework Cookbook written by Alexander Reelsen .

    I found that your blog is informative, and has potential articles on Play! If you are interested in reviewing the book, then I’d be glad to send you an e-copy of it.

    More information about the book: http://www.packtpub.com/play-framework-cookbook/book

    For more details, contact me on: jiteshg@packtpub.com

  3. Bonjour,

    Explication concrète : les méthodes annotées en “@after” sont appelées après le render. Par conséquent, modifier les renderArgs ne sert effectivement plus à rien, c’est trop tard.

    Il y aurait un effet si les méthodes @after étaient appelées au moment du render, ce qui n’est pas le cas. Un petit coup d’oeil dans play.mvc.ActionInvoker permet de le vérifier (méthode invoke).

    // 3. Invoke the action
    try {
    // @Before
    handleBefores(request);

    // Action
    Result actionResult = null;
    ...
    ...invokeControllerMethod...etc...
    ... (la controllerMethod fait appel au render)
    ...

    // @After
    handleAfters(request);

    Bonne journée !

  4. Ce qu’ils sont fort ces Zergs :p

 Leave a Reply

(required)

(required)


nine × 4 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

   
© 2011 NooCodeCommit Suffusion theme by Sayontan Sinha