My last chapter summarizes the basic use of play frame. In this chapter, I will revise and increase the response.
The HTTP protocol works in a "request-response" manner. The core of the play framework is to complete the request-response with action. An action is responsible for handling a request. A project may have many actions to define. Complex sites, you might want to define hundreds of actions. Therefore, play uses the controller (Controller) and URL routing (URL routing) to organize the management action. The controller is used to classify actions. URL routing (routes) records the correspondence between the URL and the action.
Ide
Before developing the code, let's briefly explain how to use eclipse to develop the play project.
In the root directory of the project, use the command:
Play Eclipse
After successful, open Eclipse, in File->import, select General->existing Projects into Workspace. In select root directory, select the root directory of the project. The project was then introduced to eclipse.
Java is a static language, you can determine the type of object at compile time, so it is convenient to implement code automatically prompt to complement, automatically look for the package and import the class. In the process of writing a program, you can also have a lot of friendly hints.
In the following code, if the import package is not specified, it can be found automatically in the Eclipse environment.
Understanding Responses
Let me first introduce the controller and URL routing. I take the action default generated by play as an example to explain. You can find the following file in the play project.
After a request enters the server, the URL route is directed to the correct action. URL routing identifies the request based on the requested method and URL, and then looks for the corresponding action.
A URL route is a file, that is, the conf/routes under the project root. Each row of the file is a record that sets the corresponding action for a URL. Like what:
# home Page Get/controllers. Application.index ()
#开始的行是注释
The record is divided into three parts. The first part is the method of the request, the second is the URL of the request, and the third is the corresponding action of the request. Here application is a controller, located in the App/controllers/application.java. A controller is also a Java class. and action Index () is a method of class.
I have slightly modified the default generated Application.java. The new App/controllers/application.java is:
package controllers;
Import play.*;
Import play.mvc.*;
Import views.html.*;
public class Application extends Controller {public
static result index () {return
ok ("Hello world!");
}
}