The HTTP protocol works in a "request-response" manner. The core of the play framework is the action to complete the request-response. An action is responsible for handling a request. A project may have to define many actions. Complex website, you may want to define hundreds of actions. So, play uses the controller and URL routing (URL routing) to organize administrative actions. Controllers are used to classify actions. URL routing (routes) records the correspondence between URLs and actions.
Start with a simple demo:
1. Add a new file under Views dong.scala.html (template)
The template separates the view from the data. The server can pass different data to the same template, resulting in different pages. Play also has a template system. Most of the template content can be written in HTML, as a view, and in some special places, reserved for data parameters. In the template, use the Scala language to invoke the parameters. The Views.html.index.render () is received in OK (), which is actually the render () method of the app/views/index.scala.html template. The play will automatically generate the appropriate classes based on the template.
You can also use import to introduce views.html.index instead of using the complete classpath.
@ (message:string,dong:string) indicates that this page accepts two parameters message and Dong are all String. @dong Call Parameters
Note: Add comments to @* *@
Due to the special function of @, @@. Required in order to display the "@" character in the template.
@ (message:string,dong:string)
<! DOCTYPE html>
2. Modify the routes file to add your own URL mappings
Get/dong controllers. Application.dong ()
Represents a GET request that is mapped to controllers by/dong (Http://localhost:9000/dong). Application.dong () method
Extension: (1) get/record/:id controllers. Application.record (Id:long)
HTTP://LOCALHOST:9000/RECORD/1234 1234 represents the ID. Http://localhost:9000/record/1234/123 is the wrong way to access the record
(2) Get/newrecord/*name controllers. Application.newrecord (name:string)
http://localhost:9000/record/1234 and http://localhost:9000/record/1234/123 can go to the record method.
This is the difference between: and *, there can be only one/, and * after the number of/.
# Routes
# This file defines all application Routes (higher priority Routes first)
# ~ ~ ~ ~ ~ ~
# Home page
get< c4/>/ controllers. Application.index ()
# Home Dong add by me
GET /dong controllers. Application.dong ()
# Map static resources from The/public folder to the/assets URL path
GET /assets/*file controllers. assets.at (path= "/public", file)
3. Modify the application file to add your own method
An action must be a static method. An action returns an object of type result. Ok ("Hello world!") The return is a result object that represents an HTTP response. The response returned by OK () is 200 status, which is ok (normal reply). In this example, the main content of the response is "Hello world!".
package controllers;
Import play.*;
Import play.mvc.*;
Import views.html.*;
public class Application extends Controller {public
static Result index () {
return ok (Index.render ("Your new APPL Ication is ready. "));
public static Result Dong () {
return ok (dong.render ("Dong Ge Wei Wu", "Winter"));
}
}
Extension: Depending on the monitoring of the Network tool, the response status code is 200. The type of response is text/plain. This is determined automatically when the result object is generated by OK (). I can manually control the response type, such as changing the return statement of Index () to: Return OK ("Hello world!"). As ("text/html");
In this way, the body type of the response is HTML.
In addition to OK (), play offers other handy ways to generate responses in different states. The names of these methods are the same as the names of the states, for example:
Return badrequest ("Bad Request"); 400, Bad Request
Return Unauthorized ("You are Forbidden"); 401, not authorized
Return Redirect ("/new"); 303, redirect
In addition, I can also use status () directly to illustrate the number form of the state code
Return status ("good");
More responses can be generated by referencing results
4. Then visit url:http://localhost:9000/dong
See the following page: Dong Ge Wei Wu
Congratulations to you. It's another success.
Great God quotes:
Once a request enters the server, it is handled by the URL route to the correct action. URL routing identifies this request based on the requested method and URL, and then finds the corresponding action.
I can also use Scala's syntax to implement more complex logic in the template. For example, the following template uses loops:
@ (title:string, content:string, lines:list[string])
<! DOCTYPE html>
Loops are implemented by @for. The @ can be followed not only by an object, but also by a complete Scala syntax.
The template can also have an if selection structure, such as
@if (item) {
<p>True</p>
} else {
<p>False</p>
}
Thank Vamei, I am winter.