In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use Play Framework". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Prepare for
Install Java 5 or above and Play.
Project creation
Open CMD and execute:
Play new helloworld
The Play new command creates a helloworld directory under the current path that contains a series of files and directories, the important ones are as follows:
App/ contains the application core, divided into models,controllers and views directories. Where .java lives ^ _ ^
Conf/ contains all the configurations of the application. The application.conf application master configuration defines url routing rules, which are used by messages internationalization.
Lib/ contains standard .jar files that the application depends on.
Public/ contains all externally accessible resources: js,css and image.
Test/ contains test programs for all applications. The test program is based on JUnit or Selenium.
Note: Play requires that all files must be UTF-8 encoded.
Wait, where is the .class file for the application? Well, instead of using class files, Play reads the Java source files directly and compiles them using Eclipse compiler.
This leads to two important things. First, the runtime Play will check your changes to the source files and load them automatically. Second, when an exception occurs, Play creates a better error report and appends the relevant code.
Run the application
Type play run helloworld,play in cmd to start Web Server and listen on port 9000
Open the browser and type in the http://localhost:9000, app to display a default welcome page
Now, look at how this page is displayed.
The main entry point of the application is configured in the conf/routes file. It defines all the URL that is accessible to the application. Open the routes file and you will see * route:
GET / Application.index
It tells Play to call the Application.indexJava method when the / path receives the GET request. It is an abbreviation for controllers.Application.index because the controllers package is implicitly attached.
When creating a standard Java application, you usually use an entry point, the main method. There are multiple Play applications, one for each URL. These methods are called action methods. The class that defines the action method is called controller.
Open helloworld/app/controllers/Application.java:
Package controllers; import play.*; import play.mvc.*; import java.util.*; import models.*; public class Application extends Controller {public static void index () {render ();}}
See that Application extends the play.mvcController class. It provides all the methods that Controller needs to use, such as the render method used in index actions.
The index method is defined as public static void because Controller never needs to instantiate and return a value. Note: it is designed to prevent the introduction of state by users and to make Controller natural and clean. But the side effect is that render can only throw the results through throw, using exceptions as GOTO, which can be described as trickery.
The default index action calls the render method, which tells Play to render a template. The template is a simple text file in the app/views directory. Use Application/index.html here
Open the helloworld/app/views/Application/index.html file:
# {extends' main.html' /} # {set title:'Home' /} # {welcome /}
The content is Play tag, similar to JSP taglib.# {welcome/} tag that generates the welcome message you saw earlier. # {extends/} tags tells Play that this template integrates another main.html template. Template inheritance can be used to create complex web and reuse common parts.
Open the helloworld/app/views/main.html template
# {get 'title' /} # {get' moreStyles' /} # {get 'moreScripts' /} # {doLayout /}
See # {doLayout/} tag? Is the location where the Application/index.html is inserted.
Create FORM
Edit helloworld/app/views/Application/index.html template
# {extends' main.html' /} # {set title:'Home' /}
We use @ {... } the symbol requests that the Play automatically generate a method to invoke the Application.sayHello action. Refresh the browser.
Oops, there's been a mistake. Because it refers to an action that doesn't exist. You need to create in helloworld/app/controllers/Application.java:
Package controllers; import play.mvc.*; public class Application extends Controller {public static void index () {render ();} public static void sayHello (String myName) {render (myName);}}
We declare the myName parameter, which automatically maps to the myName parameter of the HTTP request submitted by form. Refresh the browser.
Enter name to submit, and another error occurred.
Because when Play renders the default template for this action, it is not found. We create the file helloworld/app/views/Application/sayHello.html
# {extends' main.html' /} # {set title:'Home' /} Hello ${myName?: 'guest'}! Back to form
Then refresh:
Provide better URL
Take a look at the submitted url:
Http://localhost:9000/application/sayhello?myName=chaos
It's not enough for RESTful. Because Play captured this URL through the default rule
* / {controller} / {action} {controller}. {action}
You can edit the helloworld/conf/routes file to add a rule before the default rule to provide a more natural hello url
GET / hello Application.sayHello
Custom layout
You can modify the template to change the layout. Edit the helloworld/app/views/main.html file:
Add validation
Add a validation to form, requiring the name field to be required. We implement it through Play validation. Edit helloworld/app/controllers/Application.java, at sayHello action:
Public static void sayHello (@ Required String myName) {if (validation.hasErrors ()) {flash.error ("Oops, please enter your name!"); index ();} render (myName);}
And import play.data.validation.*. Required tells Play to automatically check whether the myName field is filled in. If the validation fails, we add a message to the flash scope and redirect to the index action. Flash scope allows messages to be maintained during redirection.
Edit helloworld/app/views/Application/index.html to display error message
# {extends' main.html' /} # {set title:'Home' /} # {if flash.error} ${flash.error}
# {/ if}
Enter null parameters and submit, and OK works.
Automated testing
Selenium Test
Run the application in test mode. Enter play test helloworld in cmd.
Open a browser and type http://localhost:9000/@tests to start the tester.
Perform a test
Selenium test cases are usually written as a html file. Play uses the Play template engine to generate these files. Helloworld/test/Application.test.html file:
* {You can use plain selenium command using the selenium tag} * # {selenium} / / Open the home page, and check that no error occured open ('/') assertNotTitle ('Application error') # {/ selenium}
This test opens the home page and confirms that there is no "Application error" in the response.
Let's write our own tests. Edit the test content:
* {You can use plain selenium command using the selenium tag} * # {selenium} / / Open the home page, and check that no error occurred open ('/') assertNotTitle ('Application error') / / Check that it is the form assertTextPresent (' The Hello world app.') / / Submit the form clickAndWait ('css= input [type = submit]') / / Check the error assertTextPresent ('Oops Please enter your name') / / Type the name and submit type ('css= input [type = text]', 'bob') clickAndWait (' css= input [type = submit]') / / Check the result assertTextPresent ('Hello bobbin') AssertTextPresent ('The Hello world app.') / / Check the back link clickAndWait (' link=Back to form') / / Home page? AssertTextNotPresent ('Hello bobbing') # {/ selenium}
Re-execute
That's all for the content of "how to use Play Framework". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 204
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.