After-action-before-render view of the interceptor (Play 1.x)

I know about the existence @Beforeand @After, but @Afteris executed after the template is rendered

Almost all my controllers extend my own class, and I want, unobtrusively, to be able to execute some code in my superclass, which checks some conditions and sets some flash / renderArgs after the code is executed in the subclass, but before rendering is done

any easy way to achieve this?

edit: here is the code snippet

class MyController extends Controller {
   @After
   static void checkStage() {
      if (xyz) {
         flash.put("stage", "bla");
      }
   }
}

all my controllers are expanding MyController, but since the code checkStageis called AFTER rendering is done, the stageflash attribute will be displayed next page rendering time

Actually, I would like to use flash.nowinstead flash.put, but since the code of the code @Afteris executed after rendering, it never appears

+5
source share
2 answers

Well, if you want the simplest: just insert the verification code before you call render (). This is the easiest approach!

- , , loadTemplate https://github.com/playframework/play/blob/master/framework/src/play/PlayPlugin.java#L157 , . : https://github.com/playframework/play/blob/master/framework/src/play/templates/TemplateLoader.java#L58 - , .

, , - , .


: , , . , , cookie ( flash). cookie - HTTP .

Cache.set(Session.getId(), map) Map. , @After

; http://www.playframework.com/documentation/1.2.5/controllers#session

+2

: , Play 1.x, .

, - :

public static void myMethod() {
   // myMethod code here

   // some common code here

   renderTemplate("myTemplate.html", id, client);
}

, :

public static void renderMyTemplate(String template, Object... params){
   // some common code here
   renderTemplate(template, params);
}

render . .

0

All Articles