Grails
  1. Grails
  2. GRAILS-6555

Make ModelAndView available in after filter

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Controllers
    • Labels:
      None

      Description

      Since controller interceptors have largely been replaced by filters, it would be nice if the ModelAndView object available in an interceptor was available in the after filter

        Activity

        Hide
        Jeff Scott Brown added a comment -

        Is the model enough or do you have uses cases where you really need the view as well?

        Show
        Jeff Scott Brown added a comment - Is the model enough or do you have uses cases where you really need the view as well?
        Hide
        Jean-Luc Geering added a comment - - edited

        hi,

        I'm trying to replace an afterInterceptor I have on all my controllers:

        def afterInterceptor = { model, modelAndView ->
        if (modelAndView) {
        if (sometest())

        { modelAndView.viewName = '/prefix_a' + modelAndView.viewName; }

        else

        { modelAndView.viewName = '/prefix_b' + modelAndView.viewName; }

        }
        }

        and for that I do need the view.

        Maybe there is a better way to change the view for all controllers based on some custom logic?

        cheers
        JL

        Show
        Jean-Luc Geering added a comment - - edited hi, I'm trying to replace an afterInterceptor I have on all my controllers: def afterInterceptor = { model, modelAndView -> if (modelAndView) { if (sometest()) { modelAndView.viewName = '/prefix_a' + modelAndView.viewName; } else { modelAndView.viewName = '/prefix_b' + modelAndView.viewName; } } } and for that I do need the view. Maybe there is a better way to change the view for all controllers based on some custom logic? cheers JL
        Hide
        Dave Oxtoby added a comment -

        I have an issue where i need to load resources for my views through a filter.

        To do this I need to know what view is being rendered by the controller which may not be the same as the action name, i.e there has been a render command.

        Having the modelandview object available would really help in this case.

        Show
        Dave Oxtoby added a comment - I have an issue where i need to load resources for my views through a filter. To do this I need to know what view is being rendered by the controller which may not be the same as the action name, i.e there has been a render command. Having the modelandview object available would really help in this case.
        Hide
        Alessandro Mecca added a comment -

        In my case I have a filter that keeps a history of the last two requests in the session. If the controller returns a "redirect:previousPage" view the filter will get the previous page from the session and redirect there. This is the code how it would look like if a modelAndView was available.

        before = {
        	// only record GETs (I'll never redirect back to a post)
        	if ("GET".equals(request.method))
        	{
        		// strip the context path from the url, to keep it uniform with context-less url
        		String relativeUrl = request.forwardURI.substring(request.contextPath.length())
        		String queryString = request.queryString == null ? "" : "?" + request.queryString
        		String actualRequest = relativeUrl + queryString;
        		RequestHistory history = (RequestHistory) session.getAttribute(SESSION_KEY)
        		[...] omitted check for history == null
        		// check that I'm not just requesting the same page again (this would unnecessarily replace the previous request in case of a refresh)
        		if (!actualRequest == history.lastRequest)
        		{
        			history.beforeLastRequest = history.lastRequest
        			history.lastRequest = actualRequest
        		}
        	}
        	return true;
        }
        after = { modelAndView ->
        	if (modelAndView != null && "redirect:previousRequest".equals(modelAndView.getViewName()))
        	{
        		RequestHistory history = (RequestHistory) session.getAttribute(SESSION_KEY);
        		modelAndView.setViewName("redirect:" + history.lastRequest)
        	}
        }
        
        Show
        Alessandro Mecca added a comment - In my case I have a filter that keeps a history of the last two requests in the session. If the controller returns a "redirect:previousPage" view the filter will get the previous page from the session and redirect there. This is the code how it would look like if a modelAndView was available. before = { // only record GETs (I'll never redirect back to a post) if ( "GET" .equals(request.method)) { // strip the context path from the url, to keep it uniform with context-less url String relativeUrl = request.forwardURI.substring(request.contextPath.length()) String queryString = request.queryString == null ? "" : " ?" + request.queryString String actualRequest = relativeUrl + queryString; RequestHistory history = (RequestHistory) session.getAttribute(SESSION_KEY) [...] omitted check for history == null // check that I'm not just requesting the same page again ( this would unnecessarily replace the previous request in case of a refresh) if (!actualRequest == history.lastRequest) { history.beforeLastRequest = history.lastRequest history.lastRequest = actualRequest } } return true ; } after = { modelAndView -> if (modelAndView != null && "redirect:previousRequest" .equals(modelAndView.getViewName())) { RequestHistory history = (RequestHistory) session.getAttribute(SESSION_KEY); modelAndView.setViewName( "redirect:" + history.lastRequest) } }
        Hide
        Fedor Belov added a comment -

        Pls check this thread - http://grails.1312388.n4.nabble.com/Change-view-name-in-filter-td1373045.html
        I've got the same problem - I can create interceptor but I want it to all my controllers... It's strange that filters still doesn't allow it to do

        Pls add this feature

        Show
        Fedor Belov added a comment - Pls check this thread - http://grails.1312388.n4.nabble.com/Change-view-name-in-filter-td1373045.html I've got the same problem - I can create interceptor but I want it to all my controllers... It's strange that filters still doesn't allow it to do Pls add this feature

          People

          • Assignee:
            Unassigned
            Reporter:
            jstewart
          • Votes:
            11 Vote for this issue
            Watchers:
            11 Start watching this issue

            Dates

            • Created:
              Updated:
              Last Reviewed:

              Development