"404" (controller: 'error', action: 'pageNotFound')
Any changes to response.status inside the pageNotFound action is reverted back to 404 before the response is sent to the client. Is it possible to work around this some way? I would like to be able to change it to 410 when I detect that the resource has been deleted or 301 when it's moved permanently.
If that's not working try this in your error controller:
class ErrorController {
def notFound = {
redirect( action: 'gone')
}
def gone= {
response.sendError(410, "Gone")
}
}
Try setting the header yourself manually by response.setHeader()
Related
My Grails 3.2.3 app is running on port 9000 and I'm doing a GET request to the URL /api/mail/getUnreadCount but I'm getting a 404 error to that URL.
My URLMapping.groovy having:
get "/api/$controller(.$format)?"(action: "index")
get "/api/$controller/$id(.$format)?"(action: "show")
"/api/$controller/$action?/$id?(.$format)?" {
constraints {
// apply constraints here
}
}
Also help me, how we can enable Grails URL mapping logs?
Update 1
class MailController {
static allowedMethods = [index: "GET", getUnreadCount: "GET"]
.....
.......
def getUnreadCount() {
User currentUserInstance = springSecurityService.getCurrentUser()
List counts = userService.getUnreadCounts(currentUserInstance)
respond(counts)
}
}
It is expected that you have getUnreadCount.gsp in views/mail directory to get the output from respond
.. and for the code you provided, if you dont have any view, you should get Error 500 not 404.
In your code, put
formats: ['xml', 'json']
or explicitely mention view
view: "showCount"
or try render method instead
Update
Btw, please remove
get "/api/$controller/$id(.$format)?"(action: "show")
When you hit /api/mail/getUnreadCount, this mapping causes $id = getUnreadCount, and you don't have any action show in your controller, which generates 404 error.
I'm using grails 2.3.7 ; my objective is to forward (not redirect !) all requests that are not ajax to a view 'index.gsp'
I've created a filter :
import javax.servlet.http.HttpServletResponse
class SinglePageFilters {
def filters = {
isNotAjax(uri: '/**') {
before = {
if (!request.xhr) {
render(status:HttpServletResponse.SC_OK, view: 'index')
return false;
}
}
}
}
}
This filter works, but the status code of the response is always 404, never 200. It's seems I can't change de the response status code from the filter.
Has someone any hint to solve this problem ?
When I redirect to another action in the same controller the 'request' is null.
def updateEmployee() {
println "updateEmployee(): request =" + request.JSON
redirect(action: "createEmployee", params: params)
}
def createEmployee() {
def renderStatus = 500;
System.out.println "createEmployee() : request= " + request.JSON;
the updateEmployee prints all the request data, but creteEmployee prints it as null ([:])
How to redirect the 'request' (I mean the POST data ) ?
You cannot redirect POST request. Redirect mean a new GET request, so all previous data from previous request will be lost.
If you need to call another action without actual redirect use forward: http://grails.org/doc/latest/ref/Controllers/forward.html
I have a strange problem with handling 404 HTTP response in grails 1.3.6 (the same wrong behavior was in 1.3.5). It sometimes works but most of the time it doesn't. I think it is a grails bug but haven't found any bug in grails' jira..
whenever I request for bad URL I receive default Tomcat 404 page.
My Configuration/UrlMappings.groovy looks like:
class UrlMappings {
static mappings = {
"404" {
controller = 'customError'
action = 'index'
code = 404
}
"500" {
controller = 'customError'
action = 'index'
code = 500
}
"/"(controller: "home", action: "index")
"/$controller/$action?/$id?"{
constraints {
// id has to be a number
id(matches: /\d+/)
}
}
}
}
Doesn anybody know how to solve it?:-)
Best,
Mateo
I think the problem is you are using braces { } instead of using parenthesis ( ). This is how it should be listed for your example of using a customError controller.
static mappings = {
"404"(controller: "customError", action: "index")
"500"(controller: "customError", action: "index")
...
}
Please see [6.4.4 in the Grails documentation][1] for more information.
[1]: http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.4.4 Mapping to Response Codes
Try it without the space. There was a bug in older versions of Grails where a space would cause the error mappings not to be picked up due to a bug in some regex somewhere:
static mappings = {
"404"{
controller = 'customError'
action = 'index'
code = 404
}
"500"{
controller = 'customError'
action = 'index'
code = 500
}
I implemented a controller that handles HTTP error codes:
class ErrorController {
// 500
def internalserver = {
}
// 504
def timeout = {
}
// 404
def notfound = {
// just testing the values
log.debug "params: ${params}"
log.debug "response: ${response}"
log.debug "url: ${response.redirectURL}"
log.debug "object: ${response.content}"
}
// 403
def forbidden = {
}
}
Note that i already updated the UrlMappings too.
"500"(controller:'error', action:'internalserver')
"504"(controller:'error', action:'timeout')
"404"(controller:'error', action:'notfound')
"403"(controller:'error', action:'forbidden')
Is there a way to retrieve details inside each action?
i.e. for 404, the URL that was requested. for 500, the exception message or something.
looks like by simply referring to:
grails-app/views/error.gsp
will reveal all needed information.