您好,欢迎访问一九零五行业门户网

javaTemplates-学习笔记三_html/css_WEB-ITnose

routes入口 后台语言的应用入口都是从routes开始的,想要新建一个页面得学会配置routes. conf/routes 文件定义了全部应用url的动作(action),如果当浏览器请求访问http://localhost:9000/,应用将会返回一个页面,此时 routes 初始格式如下
# routes# this file defines all application routes (higher priority routes first)# ~~~~# home pageget / controllers.application.index
字面意思就是首页了[默认的首页是 view 下的index.scala.html].
此定义告知play接收到http get/post类型请求且路径为[/]时调用 controllers 包含application类的index方法,对应的代码如下:
package controllersimport play.api._import play.api.mvc._object application extends controller { def index = action { ok(views.html.index(your new application is ready.)) }}
如果想要指定是哪个文件可以修改routes:
# home pageget /index.html controllers.application.index
这个时候访问的地址就需要加上文件名了[http://localhost:9000/index.html].
理解routes和 controller 上面的 routes 定义 / 和 /index.html 对应了 application.scala 代码块中的 index 方法来显示网页内容:
//所有的控制台代码按play规范均归入controllers包package controllers//导入play应用开发所需要的类库import play.api._import play.api.mvc._//application全局对象实例化,因此使用object来声明application并继承play的controller类object application extends controller {//定义index方法,任何toutes文件中指定调用的方法,必须放回action对象来处理http请求 def index = action { //任何action对象必须获得反返回的result对象 //ok继承于result对象,所以返回ok表示其包含的内容为http 200 ok的状态 //在scala最后一行代码等同于 return ok(views.html.index(your new application is ready.)) ok(views.html.index(your new application is ready.)) }}
def 这个声明好像rython,ruby中的代码块声明.
ok表示http请求成功状态,可以修改内容try一下:
ok(views.html.index(hello world!))
模板页面中的头部变成了 hello world! .
view新建文件 了解了 routes application controller 之间的关系就可以自己创建文件了.
get /show.html controllers.application.show
def show = action { ok(views.html.index(hello world!)) }
在 view 中新建文件show.scala.html copy index.scala.html中的代码块运行之...然后可以http://localhost:9000/show.html访问.
其它类似信息

推荐信息