Grails 1.3.7 picks wrong view for controller - grails

I have controller named LoginController with action auth:
class LoginController {
def auth = {
render view: 'auth'
}
}
So I go to the URL http://localhost:8080/myapp/login/auth, and want to see my view '/login/auth.gsp'. But I get the following error:
type Status report
message /myapp/WEB-INF/grails-app/views/jobSearch/auth.jsp
description The requested resource (/myapp/WEB-INF/grails-app/views/jobSearch/auth.jsp) is not available.
Seems that grails resolves wrong view name for some reason. Do you know, what's going on?
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints { // apply constraints here }
}
"/"(view:"/index") "500"(view:'/error')
}
}

Agree with #omarello, something looks like its mapping the login controller URL to 'jobsearch'. The error message relating to JSP is what happens when it can't find a matching gsp page and tries to fallback to JSP.

Related

Sending Stripe Webhooks to Controller in Grails

I am trying to capture a test web hook from Stripe on Grails(2.5.1). I setup a line in my URLmappings which when going directly to the URL on a browser on my local machine running ngrok executes the controller method ok. I cannot seem to get the Stripe payload to the controller however? Thanks!
//UrlMappings.groovy
class UrlMappings {
static mappings = {
"/stripe-demo" (controller: 'charge', action: 'respond')
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
//Controller
package stripe.demo
class ChargeController {
def respond(String payload){
//capture the payload sent by Stripe and do something, also respond with 200.
}
}
The stripe webhook data comes in request.JSON so databinding it to a String like that won't work. Here's an example:
class ChargeController {
def webHook() {
if(request.JSON) {
// do something with it, eg:
switch(request.JSON.type) { }
}
render '' // sends nothing back but a http 200
}
}
And then set the webhook to https://example.com/charge/webHook (note, it must be accessable to stripe so localhost wont work)

Grails: On 404 error need to know name of controller and action that caused it

In the URL mapping file, i have connected 404 error with ErrorController's notFound action.
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"500"(controller: "errorPage", action: "internalError")
"403"(controller: "errorPage", action: "forbidden")
"404"(controller: "errorPage", action: "notFound")
}
If any controller does not makes render call and a default view does not exists for that action, it invokes the notFound action of ErrorPageController. Now, I want to know in this notFound action which action was called due to which the 404 exception was invoked. How can that be known? For example, in case of 500, the internalError action get request.exception through which we know what happened that invoked internalError. Same needs to be done for 404 error.
I've tried this controller:
package test
class ErrorPageController {
def grailsUrlMappingsHolder
def grailsApplication
def notFound() {
// retrieve uri without context (if any)
def uri = request.forwardURI - "/${grailsApplication.metadata['app.name']}"
// try to parse uri agains defined urlMappings to get parameters
def parameters = grailsUrlMappingsHolder.match(uri)?.parameters ?: [:]
[previousAction : parameters.action, previousController: parameters.controller]
}
}
I have assumed that in your path the context is your appName, you may have to adjust it accordingly.
You could probably use grails wildcard filters like mentioned http://grails.org/doc/2.2.1/ref/Plug-ins/filters.html
Then print the things you need like controllername and action here. The filter get called before every controller / action execution.

Grails app index page URL

I am trying to shorten the URL to get to my grails app. Currently the shortest I can get it to is
http://myserver:8080/helloWorld/helloWorld/
HelloWorld is the controller name and the app name. Can I make it shorter somehow so its only
http://myserver:8080/helloWorld/
I have URL mappings set to
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
You can make HelloWorldController the default URL by changing
"/"(view:"/index")
to
"/"(controller: 'helloWorld')
This uses the default action in that controller (likely index()); if you want a different action, do this:
"/"(controller: 'helloWorld', action: 'theOtherName')
If you only have one controller, there's no need to have it in the URL. You could use the following mapping:
static mappings = {
"/$action?/$id?"(controller:'helloWorld')
"500"(view:'/error')
}
In this case, http://myserver:8080/helloWorld/ will go to HelloWorldController.index() instead of serving up the index.gsp view.
The leading helloWorld is also optional. Add these lines to your Config.groovy to use the root context:
grails.app.context = "/"
grails.serverURL = "http://myserver:8080"
Combining both of these will allow you app be accessed through http://myserver:8080/.

Grails UrlMapping route to fixed controller and action

I made the following rule in my UrlMapping file and now all my controllers are matching to the ("/$username") mapping, not the first one ("/$controller/$action?/$id?").
The idea here was to list all public items from an user using a short url. It works but it breaks all others controllers.
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/$username" {
controller = 'user'
action = 'publicItens'
}
"/"(controller:'usuario', action: 'index' )
"500"(view:'/error')
}
How can I map it correctly?
solved!
I just wrote some code in the UrlMappings to create rules automatically for each controller in the application. Using this approach when the user types /appname/controllerName then the rule created automatically is considered in place of the "$/username" rule.
The critical point is that the use of ApplicationHolder which is deprecated. That can fixed writing your own ApplicationHolder.
static mappings = {
//creates one mapping rule for each controller in the application
ApplicationHolder.application.controllerClasses*.logicalPropertyName.each { cName ->
"/$cName" {
controller = cName
}
}
"/$controller/$action?/$id?"{
}
"/$username" {
controller = 'usuario'
action = 'itensPublicos'
}
"/"(controller:'usuario', action: 'index' )
"500"(view:'/error')
}
Just add /users/$username to the URL mapping. This is the simplest way how to achieve your goals.
"/users/$username" {
controller = 'user'
action = 'publicItens'
}
You could probably also exclude the user and usario controllers in the first url mapping constraints(notEqual)
http://www.grails.org/doc/latest/ref/Constraints/notEqual.html

Setting up the filter without controllers

I have a running tomcat at localost.
I wish to write a grails filter such that when ever user goes to localhost/filter/ intercept the call and do some processing. I created a filter which is inside conf folder
class TestFilters {
def filters = {
filter(uri:'/filter') {
before = {
}
after = {
}
afterView = {
}
}
}
}
after setting this up when I go to localhost/filter/ I get only 404 error.
Where I'm making the mistake?
Thanks in advance
If you have no FilterController the url localhost/filter has no ressource to show - so you get a 404 error. You have to adapt your UrlMappings so that localhost/filter is a valid url of application.
Add the following to UrlMappings.groovy:
"/filter" (controller: "yourController", action:"yourAction")
Now - the url localhost/filter points to yourAction in yourController and the filter should be applied.

Resources