Familiar Hello World
Create a new project to understand the structure of important documents
. \app
Controllers\models\views
The more popular MVC architecture at the moment
. \conf
APPLICATION.CONF engineering configuration, including database connection, etc.
Routes routing configuration, for resolving URLs
Find the. \app\views\application\index.html
Modify page content, add an input box and a button
#{extends ' main.html '/}#{set title: ' Home '/}<formAction= "@{application.sayhello ()}"Method= "Get"> <inputtype= "text"name= "MyName" /> <inputtype= "Submit"value= "Say Hello" /></form>
#{extends ' main.html '/} indicates that the page inherits from Main.html
#{set title: ' Home '/} set page title
Run the program, go to page localhost:9000, will prompt error,No route able to invoke action Application.sayhello is found
Adding SayHello methods in Application.java
Public Static void SayHello (String myName) { render (myName);}
Add SayHello page, add sayhello.html under. \app\views\application
#{extends ' main.html '/}#{set title: ' Home '/} < H1 > Hello ${myname?: ' Guest '}! </ H1 > < href= "@{application.index ()}">back to form</A >
The results are as follows:
Optimization of URLs Http://localhost:9000/application/sayhello?myName=Alex
Configure routing. \conf\routes
In */{controller}/{action} {controller}. Add after {action}:
GET /hello Application.sayhello
You can use the new URL to access the page Http://localhost:9000/hello?myName=Alex
Custom layout
Layout is the public part of all pages, modified. \app\views\main.html
< Body > Hello World APP </> #{dolayout/}</ body>
#{dolayout/} will replace the contents of the sayhello.html
Add validation
Modify the SayHello method
Import play.data.validation.Required; Public Static void SayHello (@Required String myName) { if(Validation.haserrors ()) { Flash.error ("Oops, please enter your name!" ); Index (); } Render (MyName);}
Modify Index.html, add the following code
#{if flash.error} <p style= "color: #c00" > ${flash.error} </p> #{/if}
。。
Play Framework First App