播放框架-设置资产的URL [英] Play Framework - Set URL for Assets

查看:77
本文介绍了播放框架-设置资产的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在生产中,我想使用nginx提供静态信息.如何在Play框架中为资产设置URL,以便我们在开发和生产中都可以使用它.我喜欢Django在设置中设置STATIC_URL的方式.

In production, I want to use nginx for serving statics. How to set a URL for assets in Play framework that we can use in both development and production. I like the way Django set STATIC_URL in settings.

在Django中,您可以在settings.py中设置STATIC_URL = 'https://static.domain.com/'.在模板中,您可以调用以下值:

In Django, you can set STATIC_URL = 'https://static.domain.com/' in settings.py. In templates, you can call the value for:

<script src='{{ STATIC_URL }}js/jquery.js'></script>

推荐答案

您可以在application.conf中添加所需的任何内容,因此对于一个实例,它可以是静态域(当前应用程序的域除外),接下来您可以IE.在您的控制器(Java版本)中编写简单的getter:

You can add anything you need to the application.conf, so for an instance it can be the static domain (other than current app's domain), next you can ie. write simple getter in your controller (Java version):

application.conf

staticUrl = "https://static.domain.com/"

控制器 Application.java

public class Application extends Controller {
    public static final String STATIC_URL = Play.application().configuration().getString("staticUrl", "http://localhost:9000");
    public static String getStaticUrl(String path){
        return STATIC_URL + path;
    }

    //other stuff
}

视图:

<script src='@Application.getStaticUrl("js/your_script.js")'></script>
<!-- or just -->
<script src='@(Application.STATIC_URL)js/your_script.js'></script>

顺便说一句...

如果只需要使用指向当前域的绝对URL,则可以直接在视图中使用Assets.at函数和absoluteURL()来完成此操作,即:

By the way...

If you just need to use absolute url pointing to the current domain, you can do it directly in the view using Assets.at function with absoluteURL() ie:

<script src='@routes.Assets.at("js/your_script.js").absoluteURL()'></script> 
<!-- or for https version -->
<script src='@routes.Assets.at("js/your_script.js").absoluteURL(true)'></script>

这篇关于播放框架-设置资产的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆