Play控制器如何将具有正确名称的变量注入到模板中?

Ada*_*ung 11 playframework

Play入门文档中,他们显示了此控制器:

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);
}
Run Code Online (Sandbox Code Playgroud)

然后,在模板中使用frontPost和olderPosts而没有任何特殊的映射!

<a href="#">${frontPost.title}</a>
Run Code Online (Sandbox Code Playgroud)

Play怎么保留这些名字?

Ben*_*ine 15

它是通过代码注入完成的.

在编译时,一些类被增强(使用Javassist的代码注入),以便添加一些信息,例如变量名.

在render方法中,此操作由"play.classloading.enhancers.LocalvariablesNamesEnhancer.LocalVariablesNamesTracer"类完成.