Saturday 18 October 2014

My Grails Trail : Use Case 0.3

Continuing my encounters with Grails in whatever limited time I am able to garner. Wrote a small use case and implemented it.

Use Case
* Menubar should be a common resource called from various screens
* Menubar should have a menu item "About Us". This should have three sub-menu items: "Company", "Mission", "Contact Information".
* "Company", "Mission", and "Contact Information" are static pages and do not need any model data.
Design
Grails templates encapsulate fragments of view functionality and can be incorporated in your pages using the tag.

There may be several pages in your application that require this small piece of content. If you save the fragments in views/_sidebar.gsp (note the underscore), you can reference it in your GSP pages or layouts with this tag:
<g:render template = "/sidebar"/>

For creating static pages, an excellent approach given by Scott Burch is to create a staticViewController and do a convenient url mapping so that it just renders the view passed in as the second parameter from the browser. This solution is given in this post -> http://grails.1312388.n4.nabble.com/Is-it-possible-to-serve-a-GSP-without-controller-intervention-td1384440.html

Implementation
-- Created a new file views/layouts/common1.gsp from views/layouts/main.gsp
-- Created a new file views/_menubar.gsp with only the html for the menubar.
-- Called views/_menubar.gsp from layouts/common1.gsp
-- views/home/index.gsp uses common1.gsp for layout.

-- Commands
--> grails create-controller staticView
| Created file grails-app/controllers/raygate/StaticViewController.groovy
| Created file grails-app/views/staticView
| Created file test/unit/raygate/StaticViewControllerSpec.groovy

views/staticView
-----------------------
-- created three files : company.gsp, contactinfo.gsp, mission.gsp from home/index.gsp

conf/UrlMappings.groovy
-----------------------------------
"/staticView/$view" {
controller = "staticView"
action = "view"
}

controllers/raygate/StaticViewController.groovy
--------------------------------------------------------------
def view = {
render(view:params.view)
}


Refactoring
Why should views/home/index.gsp have the links to css and js resource files? These are required for the menubar. Hence they are moved to the file views/layout/common1.gsp

No comments:

Post a Comment