使用过滤器取消页面请求时,Grails会引发404错误 [英] Grails throws a 404 error when canceling a page request with a filter
问题描述
class MyFilters {
def filters = {
before = {
render(view:/ test)
return false
}
}
}
这对于我使用控制器来处理请求的页面很有效,显示test.gsp的内容而不是我请求的页面。但是,当我尝试访问直接映射到GSP文件的页面时,出现404错误。
将渲染更改为 render test
会产生相同的结果,就像注释掉它一样,只留下 return false
。
Grails是一个MVC框架。如果您想直接将URL映射到GSP(不通过控制器和操作进行重定向),则需要在 UrlMappings.groovy
中对grails进行解释。在那里你可以定义你的快捷方式。例如:
static mappings = {
/ $ viewName(view:/ index){
约束{
viewName([一些约束])
}
}
}
这将在不经过控制器的情况下渲染 views / index.gsp
。如果您没有为这些URL定义控制器映射(或至少一个视图映射),则不能使用grails过滤器:
如果您确实想拦截所有请求,您可以像这样为您的grails应用程序添加一个servlet过滤器:
import javax.servlet。*
import org.springframework.web.context.support.WebApplicationContextUtils;
$ b $ class TestFilter implements Filter {
def applicationContext
void init(FilterConfig config)throws ServletException {
applicationContext = WebApplicationContextUtils.getWebApplicationContext( config.servletContext)
}
void destroy(){
}
void doFilter(ServletRequest请求,ServletResponse响应,FilterChain链)throws IOException, ServletException {
System.out.println(this filter has called called);
$ p在这里你可以根据你的重定向或渲染 applicationcontext
和当前请求
。
您需要将此过滤器添加到 web.xml
中。在如何做到这一点,看看:我如何在我的grails应用程序中使用servlet?
I have a filter:
class MyFilters {
def filters = {
before = {
render(view: "/test")
return false
}
}
}
This works great on pages where I'm using a controller to handle the request, showing the contents of test.gsp instead of the page I requested. However, when I try to access a page that maps directly to a GSP file, I get a 404 error.
Changing the render to simply render "test"
produces the same results, as does commenting it out and just leaving the return false
.
解决方案 Grails is a MVC framework. If you want to map an URL directly to a GSP (without redirection through a controller and action) you need to explain this to grails within your UrlMappings.groovy
. There you can define your "shortcuts". E.g.:
static mappings = {
"/$viewName"(view:"/index") {
constraints {
viewName([some constraints])
}
}
}
Which will render views/index.gsp
without going through a controller. If you do NOT define a controller mapping (or at least a view mapping) for those URLs, you canNOT use grails filters:
If you really want to intercept ALL requests, you can add a servlet filter to your grails application like this:
import javax.servlet.*
import org.springframework.web.context.support.WebApplicationContextUtils;
class TestFilter implements Filter {
def applicationContext
void init(FilterConfig config) throws ServletException {
applicationContext = WebApplicationContextUtils.getWebApplicationContext(config.servletContext)
}
void destroy() {
}
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("this filter has been called");
}
}
In here you can do your redirections or renderings based on the applicationcontext
and the current request
.
You need to add this filter to your web.xml
. On how to do this, have a look at: How do i use a servlet in my grails app?
这篇关于使用过滤器取消页面请求时,Grails会引发404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!