create scala.html files in play 2 framework [close

2019-08-02 18:43发布

hope to explain my problem better now , I am using play 2 framework with java to develop a sketching system with html5 canvas.

the front end will be composed of 3 different views (pages). one for rendering the html5 canvas , one for rendering a submit form and display tags. And a third page as an administrator page. want to create these views and be able to navigate from one rendered view or page to another.

Sorry if it is a basic question but kind of new to play framework.

any suggestion for me.

I know the @helper tags are used but don't seem to know how to go about it.

thank you.

1条回答
孤傲高冷的网名
2楼-- · 2019-08-02 19:35

You don't need to use @helper or @tags they are for including templates in other templates, just use common rendering, first create the files:

  • app/views/canvas.scala.html
  • app/views/edit.scala.html
  • app/views/admin.scala.html

So then in your Appliaction controller create three actions representing each view

public static Result canvas(){
    return(views.html.canvas.render());    
}

public static Result canvas(){
    return(views.html.edit.render());    
}

public static Result canvas(){
    return(views.html.admin.render());    
}

For each action you also need to create a route in conf/routes to 'translate' given URL to proper action (first is default):

GET   /             controllers.Application.canvas()
GET   /edit         controllers.Application.edit()
GET   /admin        controllers.Application.admin()

Finally in each view add that block, to get the 'main menu' displayed on every page. Note: use reverseRouting as a href of links to make sure they are always correct - even if you change something in routes (de facto, here you could use @tags for including this block from one file to many views, however place it manually now):

<div class="main-nav">
    <a href='@routes.Application.canvas()'>Canvas page</a>
    <a href='@routes.Application.edit()'>Edit form</a>
    <a href='@routes.Application.admin()'>Admin area</a>
</div>

You have now sample for basic application with 3 actions, with separate view for each.

At the end, don't be angry with me, but you need to spent more time studying official documentation and included Java samples. I showed you basic scenario, which allows you to navigate between three pages and nothing else. Most probably it's not enought to write working app, however describing it makes no sense - as it's described yet in the docs and demonstrated in samples.

查看更多
登录 后发表回答