`
yonglin4605
  • 浏览: 187223 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ThreadLocale

阅读更多

import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.LocaleResolver;

public class WebContext
{

    private WebContext()
    {
    }

    public static WebContext getWebContext()
    {
        WebContext context = (WebContext)webContext.get();
        if(context == null)
        {
            context = new WebContext();
            webContext.set(context);
        }
        return context;
    }

    public static void clear()
    {
        webContext.set(null);
    }

    public HttpServletRequest getRequest()
    {
        return request;
    }

    public void setRequest(HttpServletRequest request)
    {
        this.request = request;
    }

    public HttpServletResponse getResponse()
    {
        return response;
    }

    public void setResponse(HttpServletResponse response)
    {
        this.response = response;
    }

    public LocaleResolver getLocaleResolver()
    {
        return localeResolver;
    }

    public void setLocaleResolver(LocaleResolver localeResolver)
    {
        this.localeResolver = localeResolver;
    }

    public Locale getLocale()
    {
        if(localeResolver != null && request != null)
            return localeResolver.resolveLocale(request);
        else
            return Internationalization.getInstance().getDefaultLocale();
    }

    private static final ThreadLocal webContext = new ThreadLocal();
    private HttpServletRequest request;
    private HttpServletResponse response;
    private LocaleResolver localeResolver;

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics