简体   繁体   English

Grails:映射到响应代码,无法更改http状态代码

[英]Grails: Mapping to response codes, impossible to change http status code

"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. 在将响应发送到客户端之前,pageNotFound操作中对response.status的任何更改都将还原为404。 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. 当我检测到资源已被删除时,我希望能够将其更改为410;而当资源被永久移动时,我希望能够将其更改为301。

If that's not working try this in your error controller: 如果这不起作用,请在错误控制器中尝试以下操作:

class ErrorController {

    def notFound = {
        redirect( action: 'gone')
      }

    def gone= {
        response.sendError(410, "Gone")
    }
}

尝试通过response.setHeader()手动设置标题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM