How do Play controllers inject variables with the proper name into templates?

StackOverflow https://stackoverflow.com/questions/4036904

  •  26-09-2019
  •  | 
  •  

문제

In the Play getting started docs, they show this controller:

public static void index() {
  Post frontPost = Post.find("order by postedAt desc").first();
  List<Post> olderPosts = Post.find("order by postedAt desc").from(1).fetch(10);
  render(frontPost, olderPosts);
}

Then, in the template the frontPost and olderPosts are used without any special mapping!

<a href="#">${frontPost.title}</a>

How is Play preserving these names?

도움이 되었습니까?

해결책

It is made by code injection.

At compile, some classes are enhanced (with code injection, by Javassist), in order to add some informations, such as variable names.

In the render method, this operation is done by the "play.classloading.enhancers.LocalvariablesNamesEnhancer.LocalVariablesNamesTracer" class.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top