javaTemplates-学习笔记四

 2023-09-11 阅读 17 评论 0

摘要:应用的调用顺序理解 这个地方很薄弱,浏览器http://localhost:9000/index.html ->conf/routes ->app/controllers/Application.scala ->def index = Action ->{Ok(views.html.index("Hello World!"))}-> 返回正文的内容纯文本或者HTML格式. 这一块应

应用的调用顺序理解

这个地方很薄弱,浏览器 http://localhost:9000/index.html  ->  conf/routes  ->  app/controllers/Application.scala  ->  def index = Action  ->  {Ok(views.html.index("Hello World!"))} -> 返回正文的内容纯文本或者HTML格式.

这一块应该也是这么理解的,语言大多都差不多的构造.(好像是这样的)

理解view层

自己研究了一下才发现默认文件下的 index.scala.html 是内容显示页面 main.scala.html 是layouts.既然不懂那就查资料

play模板在HTML基础上直接基于Scala语言,模板文件通常放在 view目录下 ,文件以".scala.html"双扩展名.每个模板文件都是一个Scala代码,都需要通过Scala编译器检查类型和语法,并编译成.class可执行的JVM二进制文件.

编译时Play首相会将.scala.html的Play模板文件自动生成为.scala的源代码文件,如 /app/views/index.scala.html的模板文件将会生成/target/scala-2.9.1/src_managed/main/views/html/index.template.scala文件,该文件将会继而被Scala编译器编译成index.class。

解析 index.scala.html 文件:

@(message: String)
@*模板入参,类型为String类型,命名为message*@@*调用main.scala.html模板,传入参数message*@
@main("Welcome to Play") {@*这里调用了play中自带的一个欢迎模板插入message*@@play20.welcome(message)}

在play模板中@符号代表后面的代码为Scala代码块,因为.scala.html文件最终要编译成Scala. index.scala.html 中的代码块把模板文件当成了一个对象,传入的参数都声明了类型.Java数据类型得看一下了.

这里的注释代码解释一下写法为:

@*我是注释*@

既然以@为Scala代码起始,那么 @**@ 为注释很好记.

结合上面的 index.scala.html 来看 Application.scala 中的 Action 

  def index = Action {Ok(views.html.index("Hello World!"))}

index里面接收的是一个参数,试试接收多个参数 index.scala   Application.scala    main.scala.html 三者之间该怎么写:

首先 Application.scala 中定义第二个参数

  def index = Action {Ok(views.html.index("Your new application is ready.","我在哪里"))}

 接着在 index.scala.html 定义参数

@(message: String,exmple: String)
@*这里声明的格式与Application中的格式一致*@@*此处main中接收的格式也是2个*@
@main(message,exmple) {@*方法体里面(也不知道这里该用什么专业的名称,暂且叫方法体.)就是HTML页面中显示的内容*@<p>学习笔记</p><a href="">凡事不可以</a>
}

然后看 main.scala.html 中要如何调用

@(title: String,exmple: String)(content: Html)<!DOCTYPE html><html><head><title>@title</title><link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")"><link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")"><script src="@routes.Assets.at("javascripts/hello.js")" type="text/javascript"></script></head><body>@exmple@content</body>
</html>

头部的参数定义正确,参数可以随意显示了.其中 @content 是 index.scala.html 文件中 @main(){...} 方法体里面的HTML代码.

转载于:https://www.cnblogs.com/gzzfans/p/4057412.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/46010.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息