Complete the installation to start writing programs. The cat is done in accordance with the examples in the book. The book is an example of a running club.
First define the domain object, here are two objects, Podio and registration. It's a 1:n relationship.
The code is as follows:
Runway
class Race {
string name//name
date StartDateTime//start time
string city//urban
String State State
float distance
float cost
Integer maxrunners = 10000//maximum number of
static hasmany = [ Registrations:registration]//race There are many registration
}
//Registered
class registration {
Race Race//The runway
string name//name
Date dateofbirth
string gender= ' F '
string postaladdress
string EmailAddress
New Date ()
static belongto = Race//indicates registration belongs to Race
Static optionals =["postaladdress"]//indicates that the property is optional
}
|
Use the Grails create-domain-class command to create an object under the project root directory. Prompt for object name after execution, enter race. Then build the registration in the same way. As you can see, in Grails-app's domain directory, there are two new files Race.groovy and Registration.groovy. The inside class is empty and the code is copied in. Ok. So the domain model is built. Grails automatically builds database tables based on domain objects. A system like object-relational mapping will help you do it automatically.
Among them, the Hasmany in race indicates that a runway has multiple registrations. At the same time, using belongto in registration means that registration belongs to the runway. This allows you to remove the registration at the same time as you delete the runway. Deleting the registration will not delete the runway.
Then we create the controller and we can do a simple crud. Enter "Grails Create-controller" under the project root and enter race to build the controller for the race object. And then build the registration again.
You can now see the Racecontroller.groovy and Registrationcontroller.groovy files under Grails-app\controllers. This is the source file for the controller. The unit test case is also generated in the test directory.
Now you want the controller to make the object crud. Modify the two controllers as follows:
Racecontroller.groovy
class Racecontroller {
def scaffold = Race
}
Registrationcontroller.groovy
class Registrationcontroller {
def scaffold = Registration
}
|
That's all you can do. Grails sees these two lines of code and can automatically generate all of the pages and code. But now only non-null validations are supported. Next time the cat will add a variety of verification. Let's run a little bit now. Execute the "grails run-app" command. Then you can go into the browser and see the results. URL http://localhost:8080/MyMiao/.
You can see that there are two controllers connected on the page. A simple crud operation can be done by clicking inside.