The first play Framework Program

Source: Internet
Author: User

The first program will first introduce the relationship between the controller of the play framework and the view, rewrite the URL, introduce the association of the database, and introduce the unit test.

Create form

Start the service ~ $ Play run playmusic, replace it with the following code in APP/views/application/index.html:

Index.html code

#{extends 'main.html' /}  

<form action="@{Application.sayHello()}" method="GET">
<input type="text" name="myName" />
<input type="submit" value="Say hello!" />
</form>  

The above Code adds a form that responds to the sayhello method of the application controller in get mode. # {extends 'main.html '/} is the syntax of a template language and does not need to be concerned at the moment.

Enter http: // localhost: 9000/in the browser. The following message is displayed:

The cause of the error is that you try to associate a method without a controller. I will create this method below. Replace APP/controllers/application. Java

Application. Java code

package controllers;  

import play.mvc.*;

public class Application extends Controller {

public static void index() {
render();
}

public static void sayHello(String myName) {
render(myName);
}

}

When the myname parameter is added to the method signature line, it becomes a captured parameter in the HTTP request,NoSetter getter.

Refresh the page. The page is displayed normally. Yes, you don't have to be surprised that the modified Java code will take effect immediately, leaving less and less time for Daze.

Click "sayhello". A new error occurs.



If the error message is clear, the default ing between the sayhello.html controller and the page is not displayed. We will immediately create an app/views/application/sayhello.html

Sayhello.html code

#{extends 'main.html' /}  
#{set title:'Home' /}


<a href="@{Application.index()}">Back to form</a>

Refresh page


The structure of a webpage comes out. By the way, this template language is groovy. More advanced applications need to query documents and mainly act as the template engine in play (similar to freemark ).

Rewrite the URL

The URL http: // localhost: 9000/application/sayhello? Myname = Gordon, not friendly enough,

Edit the APP/CONF/routes file

XML Code

# Routes  
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET / Application.index
GET /hello/{myName}.html Application.sayHello
# Map static resources from the /app/public folder to the /public path
GET /public/ staticDir:public


# Catch all
* /{controller}/{action}.html {controller}.{action}

The get/Hello/custom myname).html application. sayhello line is added, which means that the sayhello method of the application controller is mapped to the/Hello/parameter myname and the HTML suffix.


URL rewriting is coming to an end.

Unit Test

Stop Service ~ $ Play stop playmusic

Start the service in test mode ~ $ Play test playmusic

In the browser http: // localhost: 9000/@ tests

The following view is displayed:

Select Start test all, green, and pass all.


Play has built-in memory databases for testing. Take a closer look at the code in the app/test directory:

Basictest code

import org.junit.*;  

import groovy.ui.text.FindReplaceUtility;

import java.util.*;
import play.test.*;
import models.*;

public class BasicTest extends UnitTest {

@Before
public void setup() {
Fixtures.deleteAll();
}

@Test
public void aVeryImportantThingToTest() {
assertEquals(2, 1 + 1);
}

}


A constant pass test. Junit4 is used for unit testing. Other Default test codes are not described.

Add an object

Add an object class music under APP/models /.

 Music. Java code

package models;  

import java.util.Date;

import javax.persistence.Entity;

import play.db.jpa.Model;

@Entity
public class Music extends Model {

public String name;

public Date publishDate;

public Music(String name, Date publishDate) {
super();
this.name = name;
this.publishDate = publishDate;
}

}

Using JPA annotations to map data tables, music inherits play. DB. JPA. model and model obtain many useful entity operation methods, such as Save, merge, and delete. The congestion model should be discussed in the domain model.

Add test code in basictest

Basictest code

@Test  
public void music_test() {
new Music("play around", new Date()).save();

List<Music> musics = Music.findAll();

Assert.assertNotNull(musics);
Assert.assertTrue(musics.size() != 0);
Assert.assertEquals("play around", musics.get(0).name);
}


Insert a piece of data and query the size of the result set. Refresh the page and the test is successful.


A basic MVC came out.

The first play program is finished.

Source: http://realgodo.iteye.com/blog/883756

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.