Grails throws a 404 error when canceling a page request with a filter

梦想的初衷 提交于 2019-12-23 17:23:13

问题


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.


回答1:


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?




回答2:


It seems to me that your app is doing the right thing. It is showing a 404 because /views/test.gsp does not exist. When I change the code to the following, it works for me.

class MyFilters {
    def filters = {
        StackOverflowTestFilter (controller:'*') {
            before = {
                render("Hello World!")
                // also fine: render(controller:"mycontroller", action:"myaction")
                return false
            }
        }
    }
}

Also, are you aware, that having return false will always cancel the rest of the flow? Return false only if the filter notices some violation of whatever you want to filter.

Hope this helps!




回答3:


You can set the http status code easily with the status argument of the Grails render method

I have not tested this with filters but with beforeIntecerptors in Grails controllers it works.

class MyFilters {
    def filters = {
        before = {
            render(view: "/test", status:200)
            return false
        }
    }
}


来源:https://stackoverflow.com/questions/9885483/grails-throws-a-404-error-when-canceling-a-page-request-with-a-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!