Grails is an MVC framework and has models,controllers and views to cleanly separate concerns.
So let's create our first controller for our Sparrow.let's assume we want a page to say hello for everyone who access our app.The requirement is simple,it's just print a "hello world "(Yes,always hello world).It's easy to do this in Grails.We need a controllers,type following command:
>>cd sparrow
>>grails create-controller
Grails will ask you what's controller name.pls type hello
After a minute,Grails will create a HelloController.groovy in path /sparrow/grails-app/controllers/
then open it and find the index method.Note every methods in this groovy file are action .the index method is the default behavior when you navigate following url:http://127.0.0.1:8080/sparrow/hello/
We will modify this method like this :
def index = {
render "Hello World!"
}
When you navigate http://127.0.0.1:8080/sparrow/hello/
you'll see a page with "Hello World!" string.Note this file is a standard groovy file.index method is a default method as I said at above.the render also a keyword.We can assume it like Response object's write method in Java servelt.
If you want define other action in hello controller.just define a new method in the file HelloController.groovy.for example.I want to show a good bye information.Just add following method :
def saygoodbye={
render "Good Bye!"
}
then navigate to following url: http://127.0.0.1:8080/sparrow/hello/saygoodbye.
Note:in this url,
the http://127.0.0.1:8080/ is domain and port.
the saprrow is context path.
the hello is controler name.
the saygoodbye is action name.
In future section we'll discuss it detail.
Sunday, March 9, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment