Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the "Welcome to Play 2.0" page creation

I have just started to try to work with the play framework and tried to understand this default application you get when creating a new project.

So as far as I understand, when loading the localhost page, a http get request comes in and is directed to the Application.index() method because of the specification in the routes file? In the index() method a lot of HTML code is generated and returned in a Result object and passed to the index.scala.html file which hands it over to the main.scala.html where it is surrounded with more html.

Now, a few things are unclear to me:

  • in Application.java there is this line return ok(index.render("Hello World!")); index belongs to views.html.*; but where is this package? I can't find it in the API :(

  • why can't I rename the index.scala.html file? Is the name a convention?

  • in the index.scala.html file itself: what does @play20.welcome(message, style = "Java") do? is this getting the "Welcome to Play 2.0" site? and again - I can't find the documentation/API entry for the play20 thing :(

I hope, somebody can help me to get an overview, as I am really confused at the moment..^^ I have done a lot of java, but no web programming at all, therefore this mix of all the different technologies and languages is quite strange at the moment!

like image 547
chris Avatar asked Oct 11 '12 11:10

chris


1 Answers

In the TodoList example, the site root GET / is mapped to the method Application.index(), so this method creates the html you are seeing.

Everything the controller does happens in this line: return ok(index.render("Hello World!"));. Basically it calls some templates which generate html and returns the output.

  • return ok( ... ) is used to indicate a successful request, return code 200.
  • index.render( ... ) returns the rendered html output of a template named index. The package used here is views.html.* but it actually refers to views/index.scala.html. So the index part is no convention, you can rename that part as you like. The template needs to get compiled before it is usable like a Java class, that's why the package names are different. The framework takes care of this.
  • "Hello World!" is the argument, as specified as message parameter in the very first line of index.scala.html
  • @play20.welcome( ... ) just calls another template, a welcome page which is included in the framework. It's not a subject to change - it's just static page, after removing that line from the view you can put any HTML you want.
like image 69
kapex Avatar answered Oct 14 '22 00:10

kapex