grails create-app de.vogella.grails.guestbook
To 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 the generate-*
commands such as generate-all, which will generate a controller (and its unit test) and the associated views:
grails generate-all Book
2.Run your application
grails run-app
3.Set Port for application
You can specify a different port by using the
server.port
argument:grails -Dserver.port=8090 run-app
4.Stop the server
Stop the server via "Ctrl + C". We need the command line to create more elements.
5.Create your domain modelgrails create-domain-class de.vogella.grails.guestbook.Feedback
grails create-domain-class de.vogella.grails.guestbook.User
grails create-domain-class de.vogella.grails.guestbook.Comment
6.Create your controller
grails generate-controller de.vogella.grails.guestbook.Feedback
grails generate-controller de.vogella.grails.guestbook.User
grails generate-controller de.vogella.grails.guestbook.Comment
7.Create your views
grails generate-views de.vogella.grails.guestbook.Feedback
grails generate-views de.vogella.grails.guestbook.User
grails generate-views de.vogella.grails.guestbook.Comment
8.Run in production
Note: By default the data maintained in the web application are not stored. If you entries should be saved to the database after server shutdown use the following command to start it.
grails prod run-app
9.Create a war archive
grails war
grails dev war
10.Testing your application
grails test-app
11.Create your services
grails create-service Geocoder
II. Grails Advance
1.Creating a Date TagLib
It's time to take a stab at making the lastUpdated date look a little more user-friendly.
The best place to put reusable snippets of code is in a custom TagLib. Type grails create-tag-lib Date.
Add the code in Listing 11 to grails-app/taglib/DateTagLib.groovy:
import java.text.SimpleDateFormat
class DateTagLib {
def longDate = {attrs, body ->
//parse the incoming date
// def b = attrs.body ?: body()
def b = attrs.date ?: body().toString()
if (b == "") {
return
}
print b
def d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(b)
print d
//if no format attribute is supplied, use this
def pattern = attrs["format"] ?: "EEEE, MMM d, yyyy"
out << new SimpleDateFormat(pattern).format(d)
}
}
Now wrap the lastUpdated
field in grails-app/views/entry/list.gsp
in your newly created <g:longDate>
tag
<g:each in="${entryInstanceList}" status="i" var="entryInstance">
<div class="entry">
<span class="entry-date"><g:longDate>${entryInstance.lastUpdated}</g:longDate></span>
<h2><g:link action="show" id="${entryInstance.id}">${entryInstance.title}</g:link></h2>
<p>${entryInstance.summary}</p>
</div>
</g:each>
Không có nhận xét nào:
Đăng nhận xét