在playframework中应用 在play框架中提供的都是动态文件响应,前端工作内容大部分是静态文件.assets大概起的就是这个作用.
默认路径看 conf/routes 里:
# map static resources from the /public folder to the /assets url pathget /assets/*file controllers.assets.at(path=/public, file)
#注释大意是静态文件从 /public 文件夹到 /assets 中url路径.
规则声明http get请求 /assets/ 时映射到 controllers.assets.at 方法,该方法使用两个参数,告诉该方法 path 路径和 file 文件.
缺省使用 /public/filename 路径,如果需要指定详细点,可以定义路由规则:
get /images/*file controllers.assets.at(path=/public/images, file)get /styles/*file controllers.assets.at(path=/public/styles, file)
使用assets逆向路由来避免hardocded(硬编码[百度百科词条])的url,assets.at也是一个普通的action方法,因此你可以使用assets逆向路由,例如:
除了逆向路由的优点,使用asset控制器的另外一个优点是内置的缓存支持以及和http entity tag(etag)的支持.从而允许客户端根据需要是否要从服务器请求资源还是可以使用cached中的文件.
assets这个从rails中发展出来的,可以看看[ruby-china]中的介绍.
