playframework文档未提及,但你能做的事

时间:2023-03-09 14:47:37
playframework文档未提及,但你能做的事

这里记录一些play!框架的文档未提及,但是可以做的一些事
playframe版本1.1

1.application.conf文件可以拆分
可以把application.conf文件拆成多个,需要在application.conf文件中加上@include.xxx=zzz.conf
xxx任意命名,xxx.conf放在conf目录下(与application.conf同一目录)
如:
application.conf
@include.site=mysite.conf
@include.hibernate=hibernate.conf
@include.hibernate_search=hibernate_search.conf

2.模版
在标签中,可以通过_caller.xxx引用控制器中render的对象
如:
public class My extends Controller{
public static void info(){
renderArgs.put("user",User.current());
render();
}
}

标签中这么引用:
_caller.user.username

3。在groovy模版吕,可以直接调用java类如:
%{
//需要全限类名
user = models.User.current()
}%

在routes中,可以使用标签,和调用方法如:
#{crud.types}
GET /? ${type.controllerClass.name.substring(12).replace('$','')}.index
GET /${type.controllerName} ${type.controllerClass.name.substring(12).replace('$','')}.list
GET /${type.controllerName}/new ${type.controllerClass.name.substring(12).replace('$','')}.blank
GET /${type.controllerName}/{id} ${type.controllerClass.name.substring(12).replace('$','')}.show
GET /${type.controllerName}/{id}/{field} ${type.controllerClass.name.substring(12).replace('$','')}.attachment
GET /${type.controllerName}/{id}/edit ${type.controllerClass.name.substring(12).replace('$','')}.edit
POST /${type.controllerName} ${type.controllerClass.name.substring(12).replace('$','')}.create
POST /${type.controllerName}/{id} ${type.controllerClass.name.substring(12).replace('$','')}.save
DELETE /${type.controllerName}/{id} ${type.controllerClass.name.substring(12).replace('$','')}.delete
#{/crud.types}

用纯java定义标签:如
//继承自FastTags,并定义命令空间
@FastTags.Namespace("lugua")
public class LuguaFastTag extends play.templates.FastTags{

public static final String PAGE_CACHE = "page-caches";

/**
* 缓存
* @param args
* @param body
* @param out
* @param template
* @param fromLine
*/
public static void _cache(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine){
String key = args.get("arg").toString();
String live = null;
if(args.containsKey("live")) {
live = args.get("live").toString();
}
Object cached = CacheManager.get(PAGE_CACHE,key);
if(cached != null) {
out.print(cached);
return;
}
String result = null;
if(body!=null)
result = JavaExtensions.toString(body);
CacheManager.set(PAGE_CACHE, key, result,live);
out.print(result);
}

}

使用方法跟普通的play标签一样
#{lugua.cache 'hotProduct','1h'}
<h1>热销产品</h1>
#{/}

http://www.anool.net/?p=182