Play Framework First App

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.