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
参数:namepublic/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

原创粉丝点击