0

I am stuck in grails url mapping ,my requirement is to generate dynamic url based on some text entered in Database. Let say "category" is a field in data base ,and I enter "Mobile", then the url for the mobile should be www.abc.com/mobile.Please help me on this .

thanks,

1 Answers1

1

URL mappings support embedded variables.

So you can define url mappings for your category controller like this

"/$category" (controller:"category", action="index")

Make sure you put this mapping at the top of other url mappings / remove the default urlmappings.

The above url mappings will map to all the urls like domain.com/mobile, domain.com/laptops etc. And the name of the variable will be available in params.

So in your controller you can get the name of the category like this

class Category {

  def index() {
   String categoryName = params.category //this is embedded variable in urlmappings
 }
}

Refer docs for more details.

Sudhir N
  • 4,008
  • 1
  • 22
  • 32
  • @ShashikantSharma Glad to know, read more in the url mappings section in grails docs. You can accept the answer btw – Sudhir N Apr 20 '15 at 09:34