加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

playframework – Play Framework 2.1.0和AngularJS集成

发布时间:2020-12-17 10:34:40 所属栏目:安全 来源:网络整理
导读:1)我想将AngularJS与Play Framework 2.1.0结合使用,将Play服务 JSON内容作为RESTful服务器后端,它从版本1.x迁移到2.x后出现,无法为静态HTML服务内容来自使用以下方法的Public文件夹: Routes.conf: GET / staticFile:/public/index.html 这导致Controller方
1)我想将AngularJS与Play Framework 2.1.0结合使用,将Play服务 JSON内容作为RESTful服务器后端,它从版本1.x迁移到2.x后出现,无法为静态HTML服务内容来自使用以下方法的Public文件夹:

Routes.conf:

GET     /                           staticFile:/public/index.html

这导致Controller方法调用预期异常被引发.

我发现在Play 2.1.0中工作的一种方法是在Controller中编写一个Action方法,如下所示:

public static Result index() {
    return ok(Play.application().getFile("public/index.html"));
}

这是最好的方法还是有更优雅和功能性的解决方案?

2)在使用客户端JavaScript框架而不是基于Scala的模板引擎的方法中是否存在任何潜在的缺点或“陷阱”?

任何指针都将非常感激.

使用Angular路由时,一种方法是使用Play为索引页面提供服务,并将部分内容作为公共目录中的静态资源提供. routes文件将包含以下内容:
GET         /                               controllers.Application.index
GET         /assets/*file                   controllers.Assets.at(path="/public",file)

Play控制器看起来像:

def index = Action {
     Ok(views.html.index())
  }

这允许您使用Plays模板来执行资源导入(它也适用于WebJars).例如,在index.scala.html中:

<script src="@routes.Assets.at("javascripts/app.js")" type="text/javascript"></script>
 <script type='text/javascript' src='@routes.WebJarAssets.at(WebJarAssets.locate("angular.min.js"))'></script>

然后,您可以将所有部分放在公共目录中并将它们作为静态文件提供,这些可以从您的app.js中引用,如下所示:

when('/partial-1',{templateUrl: '/assets/partials/partial-1.html',controller: CtrlPartial1}).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读