This page runs through the quick exercise of implementing a “Hello World”application.

    Let’s start with the myapp project that was created previously.

    Edit the app/views/App/Index.html template to add this form, under theincluded flash.html template:

    1. <form action="/App/Hello" method="GET">
    2. <input type="text" name="myName" /><br/>
    3. <input type="submit" value="Say hello!" />
    4. </form>

    Refresh the page to see our work.

    The Say Hello form

    Enter some data and submit the form.

    Route not found

    That makes sense. Add the method to app/controllers/app.go:

    1. func(cApp)Hello(myNamestring)revel.Result{returnc.Render(myName)}

    Next, we have to create the view. Create a fileapp/views/App/Hello.html, with this content:

    1. {{set . "title" "Hello page"}}
    2. {{template "header.html" .}}
    3. <h1>Hello {{.myName}}</h1>
    4. <a href="/">Back to form</a>
    5. {{template "footer.html" .}}

    Finally, add the following to conf/routes file, just below the App.Index entry.

    1. GET /App/Hello App.Hello

    Refresh the page, and you should see a greeting:

    Hello revel

    Lastly, let’s add some validation. The name should be required, and at leastthree characters.

    To do this, let’s use the validation module. Edityour method in app/controllers/app.go:

    1. func(cApp)Hello(myNamestring)revel.Result{c.Validation.Required(myName).Message("Your name is required!")c.Validation.MinSize(myName,3).Message("Your name is not long enough!")ifc.Validation.HasErrors(){c.Validation.Keep()c.FlashParams()returnc.Redirect(App.Index)}returnc.Render(myName)}

    Now it will send the user back to Index() if they have not entered a validname. Their name and the validation error are kept in theFlash, which is a temporary cookie.

    The provided flash.html template will show any errors or flash messages:

    1. {{if .flash.success}}
    2. <div class="alert alert-success">
    3. {{.flash.success}}
    4. </div>
    5. {{end}}
    6. {{if or .errors .flash.error}}
    7. <div class="alert alert-error">
    8. {{if .flash.error}}
    9. {{.flash.error}}
    10. {{end}}
    11. {{if .errors}}
    12. <ul style="margin-top:10px;">
    13. {{range .errors}}
    14. <li>{{.}}</li>
    15. {{end}}
    16. </ul>
    17. {{end}}
    18. </div>
    19. {{end}}

    When we submit that form with a name that fails validation, we want the form to retain the bad name, so that the user can edit it before re-submitting. Amend the form you had added to your app/views/App/Index.html template:

    1. <form action="/App/Hello" method="GET">
    2. {{with $field := field "myName" .}}
    3. <input type="text" name="{{$field.Name}}" value="{{$field.Flash}}"/><br/>
    4. {{end}}
    5. <input type="submit" value="Say hello!" />
    6. </form>

    Now when we submit a single letter as our name:

    Example error

    Success, we got an appropriate error and our input was saved for us to edit.

    原文: https://revel.github.io/tutorial/firstapp.html