2 Getting Started - Reference DocumentationAuthors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith Version: 2.3.3 Table of Contents2 Getting Started2.1 Installation RequirementsBefore installing Grails you will need as a minimum a Java Development Kit (JDK) installed version 1.6 or above. Download the appropriate JDK for your operating system, run the installer, and then set up an environment variable calledJAVA_HOME pointing to the location of this installation. If you're unsure how to do this, we recommend the video installation guides from :
These will show you how to install Grails too, not just the JDK. A JDK is required in your Grails development environment. A JRE is not sufficient. On some platforms (for example OS X) the Java installation is automatically detected. However in many cases you will want to manually configure the location of Java. For example: export JAVA_HOME=/Library/Java/Home export PATH="$PATH:$JAVA_HOME/bin" if you're using bash or another variant of the Bourne Shell. 2.2 Downloading and InstallingThe first step to getting up and running with Grails is to install the distribution. To do so follow these steps:
If Grails is working correctly you should now be able to type
2.3 Creating an ApplicationTo create a Grails application you first need to familiarize yourself with the usage of thegrails command which is used in the following manner:grails [command name] Run create-app to create an application:
This will create a new directory inside the current one that contains the project. Navigate to this directory in your console: cd helloworld 2.4 A Hello World ExampleLet's now take the new project and turn it into the classic "Hello world!" example. First, change into the "helloworld" directory you just created and start the Grails interactive console:
You should see a prompt that looks like this: What we want is a simple page that just prints the message "Hello World!" to the browser. In Grails, whenever you want a new page you just create a new controller action for it. Since we don't yet have a controller, let's create one now with the create-controller command:
Don't forget that in the interactive console, we have auto-completion on command names. So you can type "cre" and then press <tab> to get a list of all The above command will create a new controller in the We now have a controller so let's add an action to generate the "Hello World!" page. The code looks like this: package helloworld The action is simply a method. In this particular case, it calls a special method provided by Grails to render the page. Job done. To see your application in action, you just need to start up a server with another command called run-app:
This will start an embedded server on port 8080 that hosts your application. You should now be able to access your application at the URL http://localhost:8080/helloworld/ - try it!
If you see the error "Server failed to start for port 8080: Address already in use", then it means another server is running on that port. You can easily work around this by running your server on a different port using The result will look something like this: This is the Grails intro page which is rendered by the One final thing: a controller can contain many actions, each of which corresponds to a different page (ignoring AJAX at this point). Each page is accessible via a unique URL that is composed from the controller name and the action name: /<appname>/<controller>/<action>. This means you can access the Hello World page via /helloworld/hello/index, where 'hello' is the controller name (remove the 'Controller' suffix from the class name and lower-case the first letter) and 'index' is the action name. But you can also access the page via the same URL without the action name: this is because 'index' is the default action . See the end of the controllers and actions section of the user guide to find out more on default actions.
2.5 Using Interactive ModeGrails 2.0 features an interactive mode which makes command execution faster since the JVM doesn't have to be restarted for each command. To use interactive mode simple type 'grails' from the root of any projects and use TAB completion to get a list of available commands. See the screenshot below for an example:For more information on the capabilities of interactive mode refer to the section on Interactive Mode in the user guide. 2.6 Getting Set Up in an IDEIntelliJ IDEAIntelliJ IDEA and the JetGroovy plugin offer good support for Groovy and Grails developers. Refer to the section on Groovy and Grails support on the JetBrains website for a feature overview. IntelliJ IDEA comes in two flavours; the open source "Community Edition" and the commercial "Ultimate Edition". Both offers support for Groovy, but only Ultimate Edition offers Grails support. With Ultimate Edition, there is no need to use the You can still use Community Edition for Grails development, but you will miss out on all the Grails specific features like automatic classpath management, GSP editor and quick access to Grails commands. To integrate Grails with Community Edition run the following command to generate appropriate project files: grails integrate-with --intellij EclipseWe recommend that users of Eclipse looking to develop Grails application take a look at Groovy/Grails Tool Suite, which offers built in support for Grails including automatic classpath management, a GSP editor and quick access to Grails commands. See the STS Integration page for an overview. NetBeansNetBeans provides a Groovy/Grails plugin that automatically recognizes Grails projects and provides the ability to run Grails applications in the IDE, code completion and integration with the Glassfish server. For an overview of features see the NetBeans Integration guide on the Grails website which was written by the NetBeans team. TextMateSince Grails' focus is on simplicity it is often possible to utilize more simple editors and TextMate on the Mac has an excellent Groovy/Grails bundle available from the TextMate bundles SVN. To integrate Grails with TextMate run the following command to generate appropriate project files: grails integrate-with --textmate Alternatively TextMate can easily open any project with its command line integration by issuing the following command from the root of your project: mate . 2.7 Convention over ConfigurationGrails uses "convention over configuration" to configure itself. This typically means that the name and location of files is used instead of explicit configuration, hence you need to familiarize yourself with the directory structure provided by Grails.Here is a breakdown and links to the relevant sections:
2.8 Running an ApplicationGrails applications can be run with the built in Tomcat server using the run-app command which will load a server on port 8080 by default:grails run-app You can specify a different port by using the grails -Dserver.port=8090 run-app Note that it is better to start up the application in interactive mode since a container restart is much quicker: $ grails grails> run-app | Server running. Browse to http://localhost:8080/helloworld | Application loaded in interactive mode. Type 'stop-app' to shutdown. | Downloading: plugins-list.xml grails> stop-app | Stopping Grails server grails> run-app | Server running. Browse to http://localhost:8080/helloworld | Application loaded in interactive mode. Type 'stop-app' to shutdown. | Downloading: plugins-list.xml More information on the run-app command can be found in the reference guide. 2.9 Testing an ApplicationThecreate-* commands in Grails automatically create unit or integration tests for you within the test/unit or test/integration directory. It is of course up to you to populate these tests with valid test logic, information on which can be found in the section on Testing.To execute tests you run the test-app command as follows: grails test-app 2.10 Deploying an ApplicationGrails applications are deployed as Web Application Archives (WAR files), and Grails includes the war command for performing this task:grails war This will produce a WAR file under the Unlike most scripts which default to the grails dev war NEVER deploy Grails using the run-app command as this command sets Grails up for auto-reloading at runtime which has a severe performance and scalability implications When deploying Grails you should always run your containers JVM with the -server -Xmx512M -XX:MaxPermSize=256m 2.11 Supported Java EE ContainersGrails runs on any container that supports Servlet 2.5 and above and is known to work on the following specific container products:
It's required to set "-Xverify:none" in "Application servers > server > Process Definition > Java Virtual Machine > Generic JVM arguments" for older versions of WebSphere. This is no longer needed for WebSphere version 8 or newer. Some containers have bugs however, which in most cases can be worked around. A list of known deployment issues can be found on the Grails wiki. 2.12 Creating ArtefactsGrails ships with a few convenience targets such as create-controller, create-domain-class and so on that will create Controllers and different artefact types for you.These are just for your convenience and you can just as easily use an IDE or your favourite text editor.For example to create the basis of an application you typically need a domain model: grails create-app helloworld cd helloworld grails create-domain-class book This will result in the creation of a domain class at package helloworld There are many such To decrease the amount of time it takes to run Grails scripts, use the interactive mode. 2.13 Generating an ApplicationTo get started quickly with Grails it is often useful to use a feature called Scaffolding to generate the skeleton of an application. To do this use one of thegenerate-* commands such as generate-all, which will generate a controller (and its unit test) and the associated views:grails generate-all helloworld.Book |
|
来自: 昵称13039494 > 《每天进步一点点》