1

I have a controller where I'm trying to set defaults based on the url - but have all of the requests going to one controller. Trying to extend the answer in : URLMapping to direct all request to a single controller/action

I did this in URLMappings.groovy

"/**"(controller:"lab", action:"index", params:[labName:action])

Where I was hoping I could add the original action name to the parameters, but this doesn't seem to do anything.

Any way I could have all the requests going to that controller mapped to one action, and see what the original action name would be?

Community
  • 1
  • 1
BZ.
  • 1,928
  • 2
  • 17
  • 26

1 Answers1

2

Action name is decided based on the url mapping not by the requested url. As you are using a single action, you will always get the action name as index. Based on your requirement below are some of the options that you can choose:

  • Use requested url and http method to find the right controller and action. Not recommended.
  • Use filter for setting default data
  • Use filter to redirect to the default controller after saving the original controller and action in request attributes. Not recommended as it will cause multiple redirects
  • Extend your controllers with the default controller and do the data setting in interceptor.
Sandeep Poonia
  • 2,158
  • 3
  • 16
  • 29
  • Any way to access the original URL if I map all requests to one controller? – BZ. Feb 23 '16 at 04:31
  • There are different methods in HttpServletRequest to get the orig url: getPathInfo(), getServletPath(), getRequestURI(), getRequestURL(). You can look into their documentation to find out which one you need. – Sandeep Poonia Feb 23 '16 at 04:49
  • I ended up doing this in urlMappings: "/lab/$labname"(controller: "lab", action: "index") which gave me the lab name as a parameter. Thanks :) – BZ. Feb 23 '16 at 05:26