play1跳转指定其他模板
来源:互联网 发布:js date对象方法 编辑:程序博客网 时间:2024/06/07 06:12
环境
play framework:1.2.7
操作系统:win7
java: 1.7 (8 不可以)
场景
今天在做公司的一个项目时,需要对某个网页添加权限控制。
但是我们知道,默认情况下,play
的默认模板是views
目录。
也就是使用render("html/aiRadar.html")
时,会去views
目录中寻找页面。
而我希望它去public
目录中的html
目录中去寻找页面;而public
目录本身就是存放静态资源的。也就是不需要java
来进行控制。
知识点
刚开始由于不知道如何跳转到指定的模板,浪费了很多时间。
在折腾了很久后,查看文档后才知道,跳转指定模板的方法。
renderTemplate(...)
上面这个方法就是play1
用来跳转指定模板的方法。
有了这个之后,就好办啦!
添加路由
由于public
目录在routes
文件中,已经定义啦:
GET / staticDir:public
即将其作为资源目录,无需java
代码来控制。
所以我们需要在其之前再添加一个针对该页面的路由,进行控制:
* /html/aiRadar.html controllers.ai.AuthAction.authExecute
使用renderTemplate
文档中给出的示例是:
public static void show(Long id) { Client client = Client.findById(id); renderTemplate("Clients/showClient.html", id, client); }
我的代码是:
public static void authExecute(){ Map<String, String> params = request.params.allSimple(); params.remove("body");// String token = params.get("token"); String token = null; Cookie cookie = request.cookies.get("tk"); if(cookie != null){ token = request.cookies.get("tk").value; Logger.info(token); } String namespace = "aiRadar_publicity.html";//(公司)宣称页面 if (token != null) {// namespace = params.get("namespace"); // 检查是否有访问权限 Map<String, String> paramsToken = new HashMap<String, String>(); paramsToken.put("token", token); paramsToken.put("button_id", "ztwy_01"); String jsonStr = null; try { jsonStr = GoGoalAPI.get("v1/permission/newisonepermi", paramsToken, DataHandler.getClientIp(request)); } catch (Exception e) { e.printStackTrace(); } Logger.info(jsonStr); JSONObject tokenObj = JSONObject.parseObject(jsonStr); JSONObject dataJson = (JSONObject)tokenObj.get("data"); boolean result = dataJson.getBooleanValue("result"); if(result){//有权限 namespace = "aiRadar.html"; } } //重点就是这里啦 renderTemplate("public/html/" + namespace);// public/html/aiRadar.html }
这里稍微强调下:
renderTemplate("public/html/" + namespace);renderTemplate("/public/html/" + namespace);
这两种写法都是一样的,play
底层的做法是:
public static VirtualFile search(Collection<VirtualFile> roots, String path) { for (VirtualFile file : roots) { if (file.child(path).exists()) { return file.child(path); } } return null; }
而file.child(path)
方法调用如下:
public VirtualFile child(String name) { return new VirtualFile(new File(realFile, name)); }
这个VirtualFile
类记录的就是找到的文件(就是你要跳转的html文件),而这个文件哪里来的呢?根据参数,它是new File()
出来的;
参数:realFile
是项目的路径,比如我的路径为D:\sts\workspace\ggfinance
,
参数:name
是public/html/aiRadar.html
或者/public/html/aiRadar.html
(也就是在renderTemplate(“public/html/aiRadar.html”)方法中设置的)
这样经过new File()
之后,就变成了D:\sts\workspace\ggfinance\public\html\aiRadar.html
。
接着会去判断文件是否存在。。。后面我就不分析啦!
总结
render():方法默认模板是views
目录。模板路径是控制器和Action的名称
。
比如:
public class Clients extends Controller { public static void index() { render(); }}
那么它的路径是:
app/views/Clients/index.html
renderTemplate():方法作用是跳转指定模板。是从项目根目录去找文件。(根目录就是名称为项目名的文件夹)。
play1框架虽然市场占有率很低,但是真的用起来很方便。
参考地址:
http://www.biliyu.com/article/1080.html#cmtoc_anchor_id_6
- play1跳转指定其他模板
- Android跳转其他app指定activity
- Thinkphp 模板 html跳转其他网站
- 使用onclick跳转到其他页面/跳转到指定url
- 使用onclick跳转到其他页面/跳转到指定url
- 使用onclick跳转到其他页面/跳转到指定url
- 使用onclick跳转到其他页面/跳转到指定url
- 使用onclick跳转到其他页面/跳转到指定url
- 使用onclick跳转到其他页面/跳转到指定url
- 使用onclick跳转到其他页面/跳转到指定url
- 使用onclick跳转到其他页面/跳转到指定url
- 使用onclick跳转到其他页面/跳转到指定url
- 使用onclick跳转到其他页面/跳转到指定url
- android 跨应用跳转 启动其他应用指定界面
- 锚链接页面跳转其他页面指定位置
- 安卓开发跳转其他app指定activity
- DEDE单页面模板如何指定其他风格文件?
- 使用onclick跳转到其他页面。使用button跳转到指定url
- 系统权限管理设计
- 空格在c语言中的scanf函数中做参数
- 数据结构——队列(queue)
- SQL技巧:如何统计博客每天的总点击量和每天的总点击人数
- ECharts 柱状图动态获取json数据
- play1跳转指定其他模板
- spring aop JoinPoint 用法
- 阿里云服务器不能发送smtp协议邮件问题
- 又见回文(串)
- Android Framework--启动流程
- win7-----xampp结合php5.6安装redis扩展
- Android 开发绕不过的坑:你的 Bitmap 究竟占多大内存?
- Python3 学习札记(三)
- 语音增强简介