Last updated by tyama 3 years ago
???????? {excerpt:hidden=true}Quick Start {excerpt}
{excerpt:hidden=true}The following makes it simple to start a grails project. There's also a screencast that follows these steps for creating a small app. {excerpt} ??????????Grails????????????????????????????????????????????? Grails????????? {excerpt:hidden=true}Create a Grails project {excerpt}
{excerpt:hidden=true}Once you have installed Grails you can use the built-in target for creating new projects: {excerpt} Grails?????????????????????(????)?????????????????????grails create-app
code: null%PROJECT_HOME% + grails-app + conf ---> location of configuration artifacts like data sources ??????????????????? + controllers ---> location of controller artifacts ????????????? + domain ---> location of domain classes ?????????? + i18n ---> location of message bundles for i18n i18n??????????????? + services ---> location of services ??????? + taglib ---> location of tag libraries ??????????? + views ---> location of views ?????? + layouts ---> location of layouts ???????? + lib + spring ---> optional spring config spring ?????????? + hibernate ---> optional hibernate config hibernate?????????? + war + WEB-INFcode: null{excerpt}%PROJECT_HOME% + grails-app + conf ---> ??????????????????? + controllers ---> ????????????? + domain ---> ?????????? + i18n ---> i18n??????????????? + services ---> ??????? + taglib ---> ??????????? + views ---> ?????? + layouts ---> ???????? + lib + spring ---> ?????????? + hibernate ---> hibernate?????????? + war + WEB-INF????????? ???? {excerpt:hidden=true} Configure a Data Source (Optional) {excerpt}
{excerpt:hidden=true} The "create-app" target created several Grails data source artifacts for you in the "<..>/grails-app/conf" directory, one for each of the standard environments: DevelopmentDataSource, TestDataSource, and ProductionDataSource. All the examples that follow operate on the development environment. For more information on environments see Configuration#environments. {excerpt} "create-app" ???????"<..>/grails-app/conf" ???????????????????DevelopmentDataSource, TestDataSource, ProductionDataSource???????Grails???????????????????????? ????? ?????????????????.{excerpt:hidden=true} By default, each data source is configured with an in-memory HSQLDB database (great for testing, but probably not that useful for live deployment) so this step is optional:{excerpt} ??????????????????????????HSQLDB????????????????????????????????????????????????????{excerpt:hidden=true}Configuring the data source is a simple matter of changing the values for the desired database and driver and placing the driver jar file in the <..>/lib directory {excerpt} ???????????????????????jar?????<..>/lib????????????????????????????????class DevelopmentDataSource { boolean pooling = true String dbCreate = "create-drop" // one of 'create', 'create-drop','update' String url = "jdbc:hsqldb:mem:testDB" String driverClassName = "org.hsqldb.jdbcDriver" String username = "sa" String password = "" }?????????? {excerpt:hidden=true}Create a Domain Class {excerpt}
{excerpt:hidden=true}Make sure you are in the root directory of your project (for argument sake "my-project) by typing "cd my-project" then run the "grails create-domain-class" target and type in the name of your domain class. {excerpt} ??????????????????????????????????"my-project"????"cd my-project"????"grails create-domain-class"??????????????????????? {excerpt:hidden=true}A domain class is a persistent artifact and all properties are by default persisted to the database (Go the the section on GORM (Grails Object Relational Mapping) for more info): {excerpt} ??????????????????????????????????????????????????????????GORM (Grails Object Relational Mapping)??????????{excerpt:hidden=true}At this point you may want to create some test data, an appropriate place for this may be in the "init" closure of the Grails application bootstrap class found in "<..>/grails-app/conf": {excerpt} ?????????????????????"<..>/grails-app/conf"???Grails????????? bootstrap??????????"init"?????????class Book { Long id Long version String title String author }new Book(author:"Stephen King",title:"The Shining").save() new Book(author:"James Patterson",title:"Along Came a Spider").save()?????????? {excerpt:hidden=true}Create a controller {excerpt}
{excerpt:hidden=true}Controllers are central to Grails applications they handle web requests and URLs of the request map to a controller class and a closure within the class. {excerpt} ??????????????????URL??????????????????????? ??????????????Grails???????????????????????? {excerpt:hidden=true}Run the "grails generate-all" target and type in the name of the controller. {excerpt} ?????"grails generate-all"???????????????????? {excerpt:hidden=true}In our example we type "book" which generates a controller called {{grails-app/controllers/BookController.groovy}}. {excerpt} ???"book"????? {{grails-app/controllers/BookController.groovy}} ?????????????? {excerpt:hidden=true}Open up this controller and change it as follows to use dynamic Scaffolding which dynamically generates your application at runtime: {excerpt} ???????????????????????????????Scaffolding?????????????????????class BookController { def scaffold = Book }Grails??? {excerpt:hidden=true}Start Grails {excerpt}
{excerpt:hidden=true}To start your Grails app run the following target {excerpt} Grails???????????????????????????????{excerpt:hidden=true}This will startup in instance of the Jetty servlet engine running on port 8080. In order to start in on a different port like e.g. 9090 use {{grails -Dserver.port=9090 run-app}}. To access the list of books open up a browser and type: {excerpt} ??????????Jetty???????????8080??????????9090????????????????? {{grails -Dserver.port=9090 run-app}} ? books?????????????????????????????????grails run-app{excerpt:hidden=true}Or, as the "list" closure is the default action for the BookController you can type: {excerpt} ???"list"???????BookController??????????????????????http://localhost:8080/my-project/book/listhttp://localhost:8080/my-project/book



