[JAVA] First Play Framework personal notes

Play Framework

Frequently used items

app folder

How to display Welcome to Play!

Routing settings in a file called routes

(Link the combination of the type of request sent from the client to the server and the URL to the method of the program)

When you access the root (localhost: 9000 by default) with the GET method, The ʻindex method of the HomeController class in the ʻapp / controllers folder is called


GET     /                           controllers.HomeController.index



@main("Welcome to Play") {
  <h1>Welcome to Play!</h1>

Call template index by index method Execute the main function. (As an argument

  • title: String "Welcome to Play"
  • content: Html \

    Welcome to Play! Is passing) ↓


    @(title: String)(content: Html)
    <!DOCTYPE html>
    <html lang="en">
            <link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
            <link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png ")">
          <script src="@routes.Assets.versioned("javascripts/main.js")" type="text/javascript"></script>

    Further call main by template index To embed the passed variable in Html, use @variable name.

    How to paste the link

    Use the information described in routes


    GET     /                           controllers.HomeController.index
    GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)
    GET     /viewtest                   controllers.ViewTestController.index   #I want to put a link here

    In html

    <a href="@routes.ViewTestController.index">view test</a>

    To do.

    important point

    • Even if you enclose it in " ", it works properly as a Twirl template.
    • In routes, it iscontrollers. ~~, But when you paste the link, change it to routes. ~~

    How to pass variables to the template

    Suppose you have a template like this.


    @(title: String="File created by myself")
    @viewtest_main("File created by myself"){

    I am trying to receive a variable called title, but since the initial value is set, the controller

    package controllers
    import javax.inject._
    import play.api._
    import play.api.mvc._
    @Singleton //Designation of "singleton". Instruction that "only one object can be created from this class"
    class ViewTestController @Inject()(cc: ControllerComponents)
      extends AbstractController(cc) { // @Inject Instruction to the inside of the framework that "cc is made by the framework"
      def index() = Action { implicit request: Request[AnyContent] =>

    This description is OK.


    @(title: String="File created by myself")(rnum: Int = 1)
    @viewtest_main("File created by myself"){
        <p>The end [email protected] chance for rnum! !!</p>

    In this way, if two variables are defined in the template and they have initial values, in the controller, ** You have to tell us that you don't pass a value to either of the two variables and leave the default value **. In that case,


     Not (an error occurs)
    #### **`Ok(views.html.viewtest_index()())`**

    In this way, you have to write two () (for the number of variables) in the method call to convey the intention to the program.

    At the top of the template, in @ (title: String =" File you created ") (rnum: Int = 1), The controller's views.html.viewtest_index () () supports it, isn't it?

    And even if you specify the value of a variable from the controller without using the initial value, arrange two () as many as the number of variables.

    Ok(views.html.viewtest_index("Title passed from the controller")(5))

    Also, on the template side, how to receive variables @ (title: String =" File created by myself ", rnum: Int = 1) If you put them in one () separated by commas like this, the way of passing is also ʻOk (views.html.viewtest_index ("title passed from controller", 5) ` In this way, it becomes "writing like a normal function".

    ** Need to be used properly depending on the situation ** How to arrange ()

    • The value you pass is easy to see in function objects and HTML blocks </ font>
    • You can easily write "do not pass variables" just by writing () </ font> On the other hand,
    • Compile takes a long time when there are many arguments </ font>

    Because it has the characteristic.

    How to write a For statement in Twirl

    @(title: String= "for loop practice", topics: Array[String])
        @for(topic <- topics) {

    The location of @ is a little confusing.