user1437891
user1437891

Reputation: 112

Run queries on Groovy/Grails startup

I am currently learning Grails/Groovy (I am very new) and trying to set a up eCommerce type of a site. I want the first page that the user sees to be a list of all of our merchandise categories. Every category has items. If I hard code the categories into the home page, I am able to click on each link (each category) and go to my controller and query each item for that specific category. But I can't figure out how to query for the categories on start-up as it doesn't appear that any controller runs for my index.gsp.

What is the best way to go about querying for my categories on start-up, and then possibly every time I come back to my index.gsp page? Below is the code I have used to hard-code the categories:

<li><g:link id="2" controller="category" action="show">Category 1</g:link></li>
<li><g:link id="3" controller="category" action="show">Category 2</g:link></li>

Once I click these links, my controller works fine and the items for that category are listed properly.

Thanks for the help.

Upvotes: 0

Views: 227

Answers (2)

uchamp
uchamp

Reputation: 2512

Although the UrlMappings.groovy config change should be the preferred solution but, if you don't want to do that then you can also issue a redirect in your index.gsp. So your index file will look like:

<% response.sendRedirect("${request.contextPath}/category/index") %>

Upvotes: 2

Jeff Storey
Jeff Storey

Reputation: 57202

The default index.gsp does not map to a controller (as you figured out). In the URLMappings.groovy, you can change the default URL "/" to map to whatever controller you want. That controller can query the database and show the categories (so your default controller might be the CategoriesController for example).

The docs for URL Mappings are http://grails.org/doc/2.2.x/ref/Plug-ins/URL%20mappings.html

Upvotes: 1

Related Questions