如何从Grails控制器返回404 / 50x状态码? [英] How can I return a 404/50x status code from a Grails Controller?

查看:213
本文介绍了如何从Grails控制器返回404 / 50x状态码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器需要在某些条件下返回404页面和状态代码。在Grails中我似乎找不到如何做到这一点。一个同事推荐这个方法:

  response.sendError(HttpServletResponse.SC_NOT_FOUND)
/ pre>

它的工作完全正常,但它不是很像Grails。我知道Rails的渲染方法采用状态参数,但Grails的渲染没有这样的功能。有没有什么我错过的,最好的方法是完成这个?

解决方案

使用自己的语句设置响应状态够好了。它看起来不太丑陋,很简单:

  response.status = 404; 

我自己已经使用了这个,已经看到别人这样做了。由于它只是一个设置者,您还可以在设置状态后进行其他操作。无论您最后设置的哪个状态将是HttpServletResponse实际将响应发送回客户端时使用的状态。


I have a controller that needs to return a 404 page and status code on certain conditions. I can't seem to find out how to do this in Grails. A coworker recommended this method:

response.sendError(HttpServletResponse.SC_NOT_FOUND)

which works perfectly fine, but it's not very Grails-like. I know Rails' render method take a status argument, but Grails' render has no such functionality. Is there something I'm missing, what's the best way to accomplish this?

解决方案

Setting the response status with its own statement is good enough. It doesn't look too ugly and is pretty straightforward:

response.status = 404;

I've used this successfully myself and have seen others do it this way too. Since it's just a setter, you can also do other stuff after setting the status. Whichever status you set last will be what the HttpServletResponse uses when it actually sends the response back to the client.

这篇关于如何从Grails控制器返回404 / 50x状态码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆