UrlMapping causing method not to render different view than default - grails

class SearchController {
def list = {
List<Product> productsList = productRepository.findProductBySearchPhrase(params.searchPhrase)
render(view: "/product/list", model: [products: productsList])
}
}
class UrlMappings {
"/$controller/$action?/$id?" {
constraints {}
}
"/search" {
controller = "search"
view = "list"
constraints {}
}
}
1) This URL works properly, rendering GSP from /views/product/list directory.
myapp.com/search/list?searchPhrase=underware
2) This URL doesn't do the work, rendering /views/search/list.
myapp.com/search?searchPhrase=underware
Any ideas?

May be you want to replace 'view' with 'action' in the search URL Mapping.

Related

UrlMapping to Index is not working

I am trying to get my Index which in my case is just localhost:8080 to go to a different page. This code is not working and I'm just going to the index page instead of rendering "this worked"
My UrlMappings file looks like this:
package appworld
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}
"/" {
controller = "Home"
action = "isUserLoggedIn"
}
"500"(view:'/error')
"404"(view:'/notFound')
}
}
The method in HomeController.groovy looks like:
def isUserLoggedIn() {
println("We made it from index")
render "this worked"
}
It looks like your syntax is off. Try:
"/"(controller:"home", action:"isUserLoggedIn")

Add custom headers to response after "render as JSON"

I am trying to add a custom HTTP header to the response of a certain set of controllers using Grails Filters:
class ApiFilters {
def filters = {
xwingFilter(uri:'/api/**') {
after = {
response.setHeader('X-Wing', 'foo')
}
}
}
}
If the controller renders a view or text, the header gets added to the response.
If the controller uses "render as JSON" the header is not added. It looks like the JSON converter is closing the output stream and sending it right away. Is that right?
I could use Grails interceptors but I'd have to replicate it in more than one controller.
Any ideas on how can I achieve this?
You can do the translation from model to JSON in the filter instead of the action:
controller:
class myController {
def myAction() {
[aThing: 1, anotherThing: [a: 1, b: 2]]
}
}
filter:
class ApiFilters {
def filters = {
xwingFilter(uri:'/api/**') {
after = { model ->
response.setHeader('X-Wing', 'foo')
render model as JSON
return false // prevent normal view from rendering
}
}
}
}

Grails 2.2.0 URLMappings: Any way to use same URL with Different Verb

I have the following:
"/api/users"(controller: "user") {
action = [GET:"list"]
}
Doing a call to http://localhost:8080/platform/users I get a list of users back. Then I added this:
"/api/users"(controller: "user") {
action = [POST:"save"]
}
And now I get a 404 and it is not hitting either method in UserController. I'd like to be able to use the same URL with the verb controlling which action. Am I doing this wrong or does Grails not support this?
From the Grails docs: URL Mappings
static mappings = {
"/product/$id"(controller:"product") {
action = [GET:"show", PUT:"update", DELETE:"delete", POST:"save"]
}
}
For your case:
"/api/users"(controller: "user") {
action = [GET:"list",POST:"save"]
}
Check your userController to see if there is allowedMethods defined accordingly like this:
class UserController {
static allowedMethods = [save: "POST", list: "GET"]
def list() {
.....
}
def save() {
.....
}
}

Grails URL Mapping question

I'm using Grails 1.2.1. I want to set up this mapping ...
http://localhost:8080/context-path/mediaproxy
So I added this to my URLMappings.groovy file ...
class UrlMappings {
static mappings = {
‰name mediaproxy: "/mediaproxy" {
controller = "SocialMediaCacheProxy"
action = "index"
}
"/"(view:"/index")
"500"(view:'/error')
}
}
However, I'm getting a 404 when I visit the above URL. Here is how I set up my controller
class SocialMediaCacheProxyController {
def index = {
if (params.dumpAll != null) {
} else if (params.url != null) {
doCacheTransport(params, response);
} // if
}
...
}
Any ideas what I'm doing wrong? Thanks, - Dave
There are is some weird character in front of your named mapping (‰) and your controller name should be lowercase on the first character so that it points to SocialMediaCacheProxyController.
If you don't need a named mapping the following mapping would do the trick for you:
class UrlMappings {
static mappings = {
"/mediaproxy"(controller:"socialMediaCacheProxy", action:"index")
"/"(view:"/index")
"500"(view:'/error')
}
}
It might be some problem with your question formatting but I would expect the url mapping to look like this:
class UrlMappings {
static mappings = {
"/mediaproxy" {
controller = "SocialMediaCacheProxy"
action = "index"
}
"/"(view:"/index")
"500"(view:'/error')
}
}

Best practices for grails index page

What is the right way to populate the model for the index page in a grails app? There is no IndexController by default, is there some other mechanism for getting lists of this and that into the model?
I won't claim that this is the right way, but it is one way to start things off. It doesn't take much to have a controller be the default. Add a mapping to UrlMappings.groovy:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"500"(view:'/error')
"/"
{
controller = "quote"
}
}
}
Then add an index action to the now default controller:
class QuoteController {
def index = {
...
}
}
If what you want to load is already part of another action simply redirect:
def index = {
redirect(action: random)
}
Or to really get some reuse going, put the logic in a service:
class QuoteController {
def quoteService
def index = {
redirect(action: random)
}
def random = {
def randomQuote = quoteService.getRandomQuote()
[ quote : randomQuote ]
}
}
I couldn't get Ed T's example above to work. Perhaps Grails has changed since then?
After some experimentation and some rummaging on the net, I ended up with this in UrlMappings.groovy:
"/"(controller: 'home', action: 'index')
My HomeController looks like this:
class HomeController {
def index = {
def quotes = = latest(Quote.list(), 5)
["quotes": quotes, "totalQuotes": Quote.count()]
}
}
And in views/home, I have an index.gsp file. That makes the index.gsp file in views unnecessary, so I removed it.
The good answer: If you need to populate a model for the index page, it's time to change from using a straight index.gsp to an index controller.
The evil answer: If you create a filter whose controller is '*', it'll get executed even for static pages.
In grails 1.3.6 for just adding
"/index.gsp"(uri:"/")
to UrlMappings.groovy worked fine for me. It has the same effect as adding a new controller and mappings like described before.
Below is my complete UrlMappings.groovy:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
"/index.gsp"(uri:"/")
}
}

Resources