Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9186437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:31:39+00:00 2026-06-17T19:31:39+00:00

I have a filter that is currently defined to run after the view is

  • 0

I have a filter that is currently defined to run after the view is rendered:

class MyFilter {
  def filters = {
    doStuff(controller: '*', action: '*') {
      before = {...}
      after = {...} 
      afterView = { 
        // code I want to run when EVERYTHING is set and done }
        Holder.setCurrentData(null)
    }
  }
}

This should work, but I noticed that <g:message /> tags (and possibly others; this is the one what interests me at this point) are executed after the afterView filter.

This is a problem because I use this filter to keep track of some information for the current execution in a ThreadLocal, and I want to make sure I clean up after myself when the request is done. I don’t want to use the request/session object to shuffle the data along, because then I have to pass it to all the calls I make; as it is, I have a Holder class that I can query for the value in the ThreadLocal.

I need the information from that ThreadLocal in my custom MessageSource. That’s how I noticed that <g:message /> is called after the afterView filter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:31:40+00:00Added an answer on June 17, 2026 at 7:31 pm

    You can use a servlet filter:

    package com.mycompany
    
    import java.io.IOException;
    
    import javax.servlet.FilterChain;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    import org.springframework.web.filter.GenericFilterBean;
    
    public class MyFilter extends GenericFilterBean {
    
       public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
                throws IOException, ServletException {
    
          try {
             chain.doFilter(req, res);
          }
          finally {
             Holder.setCurrentData(null);   
          }
       }
    }
    

    Run grails install-templates if you haven’t already and then you can register it in src/templates/war/web.xml like this:

    <filter>
       <filter-name>myFilter</filter-name>
       <filter-class>com.mycompany.MyFilter</filter-class>
    </filter>
    
    <filter-mapping>
       <filter-name>myFilter</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    I’ve extended GenericFilterBean for convenience but you could also just implement the javax.servlet.Filter interface directly. It could also be written in Groovy, but I tend to write filters in Java since they’re called for every request, and the small overhead that Groovy adds can add up here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Filter Servlet that filters request from a Servlet. I do not
I have a view that I'm trying to filter with something similar to DISTINCT
I have an ISAPI filter that I am using to do URL rewriting for
I have a control that has a Filter property that expects a function that
Say you have a few DropDownLists that filter data: Car Make: [Ford,Chevrolet,etc] Car Year:
I have been fruitlessly trying for several hours to make a function that filter
I have an application that can filter a datagridview based on date using a
I have an algorithm in C++ that uses Kalman Filter. Somewhere in the code
I have a command (cmd1) that greps through a log file to filter out
I have a WPF ListView that I am trying to filter within a BackgroundWorker.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.