Framework: Spring, Hibernate. O/S: Windows
I am trying to implement hibernate’s Custom message interpolator following the direction of this Link.
When implementing the below class, it gives an error “Cannot make a static reference to the non-static type Locale”.
public class ClientLocaleThreadLocal<Locale> {
private static ThreadLocal tLocal = new ThreadLocal();
public static void set(Locale locale) {
tLocal.set(locale);
}
public static Locale get() {
return tLocal.get();
}
public static void remove() {
tLocal.remove();
}
}
As I do not know generics enough, not sure how < Locale > is being used by TimeFilter class below and the purpose of definition in the above class.
public class TimerFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
try {
ClientLocaleThreadLocal.set(req.getLocale());
filterChain.doFilter(req, res);
}finally {
ClientLocaleThreadLocal.remove();
}
}
public void init(FilterConfig arg0) throws ServletException {
}
}
Will doing the following be okay?
-
Change static method/field in ClientLocaleThreadLocal to non-static method/fields
-
In TimeFilter, set locale by instantiating new object as below.
new ClientLocaleThreadLocal().set(req.getLocale())
Thanks for your help in advance
declares a generic class
ClientLocaleThreadLocalwith a type parameter calledLocale. Since the ClientLocaleThreadLocal always contains a Locale, there is no need for a type parameter here.A ThreadLocal in contrast is a generic type, and has as type parameter the type of object it holds. In your case, this is
Locale. Your code should therefore read:As for what a ThreadLocal is, read its Javadoc or google its name.
Whether
res.getLocale()is the “client locale” is something we can’t know, since “client locale” is a little vague.