其实真正开始web开发是从django开始的。以前玩过asp,php,jsp,但是却都放下了。一者是工作的缘故。ruby on rails框架是一个非常不错的web敏捷开发框架,但是我在python下面找到django是一个非常类似的框架,而且用了一下也很喜欢。现在转到groovy了,发现grails框架同样是一套非常强大的MVC框架,所以,花一点时间却研究一下是值得的。从使用上来讲,觉得于django非常相似,看来,以后在这个平台上面还是要有所作为的。
1.安装(转自)
- the latest Grails
- Extract the archive into an appropriate location; typically C:\grails on Windows or ~/grails on Unix
- Create a GRAILS_HOME environment variable that points to the path
where you extracted the archive (eg C:\grails on Windows or ~/grails on
Unix)
- If you have not set the JAVA_HOME environment variable yet, create
JAVA_HOME environment variable that points to the path where you have
installed Java
- Append a reference to the "bin" directory within the Grails
directory to your PATH variable (eg %GRAILS_HOME%\bin on Windows or
$GRAILS_HOME/bin on Unix). Note that, for Windows, both PATH and
GRAILS_HOME must be defined at the same environment variable level (eg.
'System variables') rather than across environment variable levels (eg.
PATH under 'System variables' and GRAILS_HOME under 'User variables')
- Type "grails" at the command line, if a help message is displayed you are ready to !
- If you get an error message, try to
chmod +x
the grails
script inside the bin
directory.
2.第一个入门的例子()
Quick Start
The following makes it simple to start a grails project. There's also a that follows these steps for creating a small app.
There is a on this Quick Start guide.
Create a Grails project
Once you have Grails you can use the built-in target for creating new projects:
The target will prompt you for the name of your project and create the project structure below:
%PROJECT_HOME%
+ grails-app
+ conf ---> location of configuration artifacts
+ hibernate ---> optional hibernate config
+ spring ---> optional spring config
+ controllers ---> location of controller artifacts
+ domain ---> location of domain classes
+ i18n ---> location of message bundles for i18n
+ services ---> location of services
+ taglib ---> location of tag libraries
+ util ---> location of special utility classes
+ views ---> location of views
+ layouts ---> location of layouts
+ lib
+ scripts ---> scripts
+ src
+ groovy ---> optional; location for Groovy source files
(of types other than those in grails-app/*)
+ java ---> optional; location for Java source files
+ test ---> generated test classes
+ web-app
+ WEB-INF
Configure a Data Source (Optional)
The
"create-app" target created a Grails data source artifact for you in
the "<..>/grails-app/conf" directory called DataSource.groovy
with closures for each of the standard environments: Development,
TestData, and Production. All the examples that follow operate on the development environment. For more information on environments see .
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:
dataSource {
pooled = false
driverClassName = "org.hsqldb.jdbcDriver"
username = "sa"
password = ""
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop','update'
url = "jdbc:hsqldb:mem:devDB"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:hsqldb:mem:testDb"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:hsqldb:file:prodDb;shutdown=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. Properties set in the dataSource node are
inherited by the children.
Create a Domain Class
Make sure you are in the root directory of your project (for argument sake "my-project") by typing
cd my-projectgrails create-domain-class
The target will prompt you for the name of your domain class.
Responding to the command with "book" will create Book.groovy in
grails-appdomain under your project. You can edit it with your favorite
text editor or
Note: When naming your domain classes,
stay away from names that are keywords in the database server you are
using for persistence (for example, on MySQL, "Group" is a keyword;
Alternatives could be something like "UserGroup").
Note: Grails doesn't seem to like names
like MYCar or MYtruck (more than one capital letter at the beginning of
the name). Names like MyCar or MyTruck, however, seem to work fine.
Classes with names that have more than one capital letter in the
beginning give 404 pages even if views for the class exist or
scaffolding is set. If you are having problems with views not showing
up and your names have more than one capital letter in the beginning of
the name, try changing your names to the "Grails-friendly" format and
try again.
A domain class is a persistent artifact and all properties are by default persisted to the database (Go the the section on (Grails Object Relational Mapping) for more info):
class Book {
String title
String author
}
At this point you may want to create some test data. An easy way to do
this is to build and save the domain objects in the "init" closure of
the Grails application bootstrap class found in
"<..>/grails-app/conf/BootStrap.groovy" (Note: with version
0.5.6, and perhaps others, it seems this file has been changed to
ApplicationBootStrap.groovy with the class declaration also changed to
"class ApplicationBootStrap"):
class BootStrap { def init = { servletContext ->
// Create some test data
new Book(author:"Stephen King",title:"The Shining").save()
new Book(author:"James Patterson",title:"Along Came a Spider").save()
}
def destroy = {
}
}
(Note that you can build and save domain objects here the same as in controllers or other parts of Grails applications; see for more on domain objects.)
Create a controller
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.
Run
the "grails create-controller" target and type in the name of the
controller. In our example we type "Book" which generates a controller
called {{grails-app/controllers/BookController.groovy}}. Open up this
controller and change it as follows to use dynamic which dynamically generates your application at runtime:
class BookController {
def scaffold = Book
}
Make sure that you typed "Book" with a capital B.
NOTE: You will need to remove or comment out the "def index = { } " from the generated file for the scaffolding to work.
Alternatively,
you could also have run "grails generate-all", which creates all the
scaffolding for you, and left the generated controller alone, instead
of replacing it with the default scaffolding. It might be worth
learning from.
Start Grails
To start your Grails app run the following target
This will startup an 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:
Or, as the "list" closure is the default action for the BookController you can type:
{tip:title=Speedy
Grails}
Grails can seem very slow in development mode, with pages taking more
than a second to load. If this applies to you, then try increasing the
maximum heap size by setting the {{JAVA_OPTS}} environment variable to
something like this: '-Xmx512m' - this will set the maximum heap size
to 512Mb when running Grails and should result in a noticeable
improvement in response times.
{tip}
阅读(2162) | 评论(0) | 转发(0) |