Java 类javax.servlet.jsp.jstl.core.Config 实例源码

项目:lams    文件:SessionListener.java   
/** HttpSessionListener interface */
   @Override
   public void sessionCreated(HttpSessionEvent sessionEvent) {
if (sessionEvent == null) {
    return;
}
HttpSession session = sessionEvent.getSession();
session.setMaxInactiveInterval(SessionListener.timeout);

//set server default locale for STURTS and JSTL. This value should be overwrite
//LocaleFilter class. But this part code can cope with login.jsp Locale.
if (session != null) {
    String defaults[] = LanguageUtil.getDefaultLangCountry();
    Locale preferredLocale = new Locale(defaults[0] == null ? "" : defaults[0],
        defaults[1] == null ? "" : defaults[1]);
    session.setAttribute(LocaleFilter.PREFERRED_LOCALE_KEY, preferredLocale);
    Config.set(session, Config.FMT_LOCALE, preferredLocale);
}
   }
项目:spring4-understanding    文件:JstlUtils.java   
/**
 * Checks JSTL's "javax.servlet.jsp.jstl.fmt.localizationContext"
 * context-param and creates a corresponding child message source,
 * with the provided Spring-defined MessageSource as parent.
 * @param servletContext the ServletContext we're running in
 * (to check JSTL-related context-params in {@code web.xml})
 * @param messageSource the MessageSource to expose, typically
 * the ApplicationContext of the current DispatcherServlet
 * @return the MessageSource to expose to JSTL; first checking the
 * JSTL-defined bundle, then the Spring-defined MessageSource
 * @see org.springframework.context.ApplicationContext
 */
public static MessageSource getJstlAwareMessageSource(
        ServletContext servletContext, MessageSource messageSource) {

    if (servletContext != null) {
        String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
        if (jstlInitParam != null) {
            // Create a ResourceBundleMessageSource for the specified resource bundle
            // basename in the JSTL context-param in web.xml, wiring it with the given
            // Spring-defined MessageSource as parent.
            ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
            jstlBundleWrapper.setBasename(jstlInitParam);
            jstlBundleWrapper.setParentMessageSource(messageSource);
            return jstlBundleWrapper;
        }
    }
    return messageSource;
}
项目:Portofino    文件:I18nUtils.java   
public static void setupTextProvider(ServletContext servletContext, ServletRequest request) {
    Locale locale = request.getLocale();
    ResourceBundleManager resourceBundleManager =
            (ResourceBundleManager) servletContext.getAttribute(BaseModule.RESOURCE_BUNDLE_MANAGER);
    ResourceBundle portofinoResourceBundle = resourceBundleManager.getBundle(locale);

    LocalizationContext localizationContext =
            new LocalizationContext(portofinoResourceBundle, locale);
    request.setAttribute(Config.FMT_LOCALIZATION_CONTEXT + ".request", localizationContext);

    //Setup Elements I18n
    ResourceBundle elementsResourceBundle =
            ResourceBundle.getBundle(SimpleTextProvider.DEFAULT_MESSAGE_RESOURCE, locale);

    TextProvider textProvider =
            new MultipleTextProvider(
                    portofinoResourceBundle, elementsResourceBundle);
    ElementsThreadLocals.setTextProvider(textProvider);
}
项目:class-guard    文件:JstlUtils.java   
/**
 * Checks JSTL's "javax.servlet.jsp.jstl.fmt.localizationContext"
 * context-param and creates a corresponding child message source,
 * with the provided Spring-defined MessageSource as parent.
 * @param servletContext the ServletContext we're running in
 * (to check JSTL-related context-params in {@code web.xml})
 * @param messageSource the MessageSource to expose, typically
 * the ApplicationContext of the current DispatcherServlet
 * @return the MessageSource to expose to JSTL; first checking the
 * JSTL-defined bundle, then the Spring-defined MessageSource
 * @see org.springframework.context.ApplicationContext
 */
public static MessageSource getJstlAwareMessageSource(
        ServletContext servletContext, MessageSource messageSource) {

    if (servletContext != null) {
        String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
        if (jstlInitParam != null) {
            // Create a ResourceBundleMessageSource for the specified resource bundle
            // basename in the JSTL context-param in web.xml, wiring it with the given
            // Spring-defined MessageSource as parent.
            ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
            jstlBundleWrapper.setBasename(jstlInitParam);
            jstlBundleWrapper.setParentMessageSource(messageSource);
            return jstlBundleWrapper;
        }
    }
    return messageSource;
}
项目:manydesigns.cn    文件:I18nFilter.java   
@Override
public void doFilter(
        ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    //I18n
    Locale locale = request.getLocale();
    ResourceBundleManager resourceBundleManager =
            (ResourceBundleManager) servletContext.getAttribute(BaseModule.RESOURCE_BUNDLE_MANAGER);
    ResourceBundle portofinoResourceBundle = resourceBundleManager.getBundle(locale);

    LocalizationContext localizationContext =
            new LocalizationContext(portofinoResourceBundle, locale);
    request.setAttribute(Config.FMT_LOCALIZATION_CONTEXT + ".request", localizationContext);

    //Setup Elements I18n
    ResourceBundle elementsResourceBundle =
            ResourceBundle.getBundle(SimpleTextProvider.DEFAULT_MESSAGE_RESOURCE, locale);

    TextProvider textProvider =
            new MultipleTextProvider(
                    portofinoResourceBundle, elementsResourceBundle);
    ElementsThreadLocals.setTextProvider(textProvider);
    filterChain.doFilter(request, response);
}
项目:spacewalk    文件:LocalizedEnvironmentFilter.java   
private void setTimeZone(Context ctx, User user, HttpServletRequest request) {
    RhnTimeZone tz = null;
    if (user != null) {
        tz = user.getTimeZone();
        LOG.debug("Set timezone from the user.");
    }
    if (tz == null) {
        // Use the Appserver timezone if the User doesn't have one.
        String olsonName = TimeZone.getDefault().getID();
        if (LOG.isDebugEnabled()) {
            LOG.debug("olson name [" + olsonName + "]");
        }
        tz = UserManager.getTimeZone(olsonName);
        // if we're still null set it to a default
        if (tz == null) {
            tz = new RhnTimeZone();
            tz.setOlsonName(olsonName);

            LOG.debug("timezone still null");
        }
    }
    ctx.setTimezone(tz.getTimeZone());
    // Set the timezone on the request so that fmt:formatDate
    // can find it
    Config.set(request, Config.FMT_TIME_ZONE, tz.getTimeZone());
}
项目:stripes-stuff    文件:JstlBundleInterceptor.java   
/**
    * Sets the Stripes error and field resource bundles in the request, if their names have been configured.
    */
@Override
protected void setOtherResourceBundles(HttpServletRequest request)
{
    Locale locale = request.getLocale();

       if (errorBundleName != null)
       {
           ResourceBundle errorBundle = getLocalizationBundleFactory().getErrorMessageBundle(locale);
           Config.set(request, errorBundleName, new LocalizationContext(errorBundle, locale));
           LOGGER.debug("Loaded Stripes error message bundle ", errorBundle, " as ", errorBundleName);
       }

       if (fieldBundleName != null)
       {
           ResourceBundle fieldBundle = getLocalizationBundleFactory().getFormFieldBundle(locale);
           Config.set(request, fieldBundleName, new LocalizationContext(fieldBundle, locale));
           LOGGER.debug("Loaded Stripes field name bundle ", fieldBundle, " as ", fieldBundleName);
       }
}
项目:vraptor4    文件:JstlLocalization.java   
/**
 * Looks up a configuration variable in the request, session and application scopes. If none is found, return by
 * {@link ServletContext#getInitParameter(String)} method.
 */
private Object findByKey(String key) {
    Object value = Config.get(request, key);
    if (value != null) {
        return value;
    }

    value = Config.get(request.getSession(createNewSession()), key);
    if (value != null) {
        return value;
    }

    value = Config.get(request.getServletContext(), key);
    if (value != null) {
        return value;
    }

    return request.getServletContext().getInitParameter(key);
}
项目:ironsites    文件:I18nHelperTag.java   
public int doEndTag() throws JspException {
    try {
        init();

        ValueMap valueMap = request.getResource().adaptTo(ValueMap.class);
        I18nResourceBundle bundle = new I18nResourceBundle(valueMap,
                getResourceBundle(getBaseName(), request));

        // make this resource bundle & i18n available as ValueMap for TEI
        pageContext.setAttribute(getMessagesName(),
                bundle.adaptTo(ValueMap.class), 
                PageContext.PAGE_SCOPE);
        pageContext.setAttribute(getI18nName(), 
                new I18n(bundle), 
                PageContext.PAGE_SCOPE);

        // make this resource bundle available for fmt functions
        Config.set(pageContext, "javax.servlet.jsp.jstl.fmt.localizationContext", new LocalizationContext(bundle, getLocale(request)), 1);
    } catch(Exception e) {
        LOG.error(e.getMessage());
        throw new JspException(e);
    }
    return EVAL_PAGE;
}
项目:spring4-understanding    文件:JstlUtils.java   
/**
 * Exposes JSTL-specific request attributes specifying locale
 * and resource bundle for JSTL's formatting and message tags,
 * using Spring's locale and MessageSource.
 * @param request the current HTTP request
 * @param messageSource the MessageSource to expose,
 * typically the current ApplicationContext (may be {@code null})
 * @see #exposeLocalizationContext(RequestContext)
 */
public static void exposeLocalizationContext(HttpServletRequest request, MessageSource messageSource) {
    Locale jstlLocale = RequestContextUtils.getLocale(request);
    Config.set(request, Config.FMT_LOCALE, jstlLocale);
    TimeZone timeZone = RequestContextUtils.getTimeZone(request);
    if (timeZone != null) {
        Config.set(request, Config.FMT_TIME_ZONE, timeZone);
    }
    if (messageSource != null) {
        LocalizationContext jstlContext = new SpringLocalizationContext(messageSource, request);
        Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
    }
}
项目:spring4-understanding    文件:JstlUtils.java   
/**
 * Exposes JSTL-specific request attributes specifying locale
 * and resource bundle for JSTL's formatting and message tags,
 * using Spring's locale and MessageSource.
 * @param requestContext the context for the current HTTP request,
 * including the ApplicationContext to expose as MessageSource
 */
public static void exposeLocalizationContext(RequestContext requestContext) {
    Config.set(requestContext.getRequest(), Config.FMT_LOCALE, requestContext.getLocale());
    TimeZone timeZone = requestContext.getTimeZone();
    if (timeZone != null) {
        Config.set(requestContext.getRequest(), Config.FMT_TIME_ZONE, timeZone);
    }
    MessageSource messageSource = getJstlAwareMessageSource(
            requestContext.getServletContext(), requestContext.getMessageSource());
    LocalizationContext jstlContext = new SpringLocalizationContext(messageSource, requestContext.getRequest());
    Config.set(requestContext.getRequest(), Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
}
项目:spring4-understanding    文件:JstlUtils.java   
@Override
public ResourceBundle getResourceBundle() {
    HttpSession session = this.request.getSession(false);
    if (session != null) {
        Object lcObject = Config.get(session, Config.FMT_LOCALIZATION_CONTEXT);
        if (lcObject instanceof LocalizationContext) {
            ResourceBundle lcBundle = ((LocalizationContext) lcObject).getResourceBundle();
            return new MessageSourceResourceBundle(this.messageSource, getLocale(), lcBundle);
        }
    }
    return new MessageSourceResourceBundle(this.messageSource, getLocale());
}
项目:spring4-understanding    文件:JstlUtils.java   
@Override
public Locale getLocale() {
    HttpSession session = this.request.getSession(false);
    if (session != null) {
        Object localeObject = Config.get(session, Config.FMT_LOCALE);
        if (localeObject instanceof Locale) {
            return (Locale) localeObject;
        }
    }
    return RequestContextUtils.getLocale(this.request);
}
项目:spring4-understanding    文件:RequestContext.java   
public static Locale getJstlLocale(HttpServletRequest request, ServletContext servletContext) {
    Object localeObject = Config.get(request, Config.FMT_LOCALE);
    if (localeObject == null) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            localeObject = Config.get(session, Config.FMT_LOCALE);
        }
        if (localeObject == null && servletContext != null) {
            localeObject = Config.get(servletContext, Config.FMT_LOCALE);
        }
    }
    return (localeObject instanceof Locale ? (Locale) localeObject : null);
}
项目:spring4-understanding    文件:RequestContext.java   
public static TimeZone getJstlTimeZone(HttpServletRequest request, ServletContext servletContext) {
    Object timeZoneObject = Config.get(request, Config.FMT_TIME_ZONE);
    if (timeZoneObject == null) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            timeZoneObject = Config.get(session, Config.FMT_TIME_ZONE);
        }
        if (timeZoneObject == null && servletContext != null) {
            timeZoneObject = Config.get(servletContext, Config.FMT_TIME_ZONE);
        }
    }
    return (timeZoneObject instanceof TimeZone ? (TimeZone) timeZoneObject : null);
}
项目:spring4-understanding    文件:ViewResolverTests.java   
@Test
public void testInternalResourceViewResolverWithJstl() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.addMessage("code1", locale, "messageX");
    wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    vr.setViewClass(JstlView.class);
    vr.setApplicationContext(wac);

    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());

    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());

    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    HttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
    Map model = new HashMap();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, request, response);

    assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
    assertTrue("Correct rc attribute", request.getAttribute("rc") == null);

    assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
    LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
    assertEquals("messageX", lc.getResourceBundle().getString("code1"));
}
项目:spring4-understanding    文件:ViewResolverTests.java   
@Test
public void testInternalResourceViewResolverWithJstlAndContextParam() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

    MockServletContext sc = new MockServletContext();
    sc.addInitParameter(Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/context/WEB-INF/context-messages");
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.addMessage("code1", locale, "messageX");
    wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    vr.setViewClass(JstlView.class);
    vr.setApplicationContext(wac);

    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());

    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());

    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    HttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
    Map model = new HashMap();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, request, response);

    assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
    assertTrue("Correct rc attribute", request.getAttribute("rc") == null);

    assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
    LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
    assertEquals("message1", lc.getResourceBundle().getString("code1"));
    assertEquals("message2", lc.getResourceBundle().getString("code2"));
}
项目:zefiro    文件:LocaleUtil.java   
/**
 * Setta a session scope la locale passata con chiave di attributo coerente a JSTL
 * @param pLocale
 * @param pHttpSession
 */
public static void setLocale(Locale pLocale, HttpSession pHttpSession) {
    if (pHttpSession==null || pLocale==null) throw new JBrickException(JBrickException.FATAL);
    mLog.debug("ENTER setLocale(Session) con locale ",pLocale.toString());
    Config.set(pHttpSession, Config.FMT_LOCALE, pLocale);
    mLog.debug("FINISH setLocale(Session)");
}
项目:zefiro    文件:LocaleUtil.java   
/**
 * Ritorna la Locale attualmente valida per quella sessione.
 * Viene ipotizzata la solita coerenza con JSTL.
 * Ritorna null nel caso la sessione non abbia una locale settata. 
 * 
 * @param httpSession
 */
public static Locale getLocale(HttpSession pHttpSession) {
    if (pHttpSession==null) throw new JBrickException(JBrickException.FATAL);
    mLog.debug("ENTER getLocale(Session)");
    Locale lLocale = (Locale) Config.get(pHttpSession, Config.FMT_LOCALE);
    if (lLocale != null) 
        mLog.debug("EXIT getLocale(Session) returning ",lLocale.toString());
    else mLog.debug("EXIT getLocale(Session) returning null");
    return lLocale;
}
项目:feilong-spring    文件:JsonView.java   
@Override
protected void renderMergedOutputModel(Map<String, Object> model,HttpServletRequest request,HttpServletResponse response)
                throws Exception{

    String filterString = (String) model.get(JSON_FILTER_STR);
    model.remove(JSON_FILTER_STR);

    // 这个key 有问题
    // see com.jumbo.brandstore.web.view.CommonTilesView.renderMergedOutputModel(Map<String, Object>, HttpServletRequest,
    // HttpServletResponse)
    model.remove(Config.FMT_LOCALIZATION_CONTEXT + ".request");

    String writeStr = "";
    if (prefixJson){
        writeStr = "{} && ";
        response.getWriter().write(writeStr);

        if (LOGGER.isDebugEnabled()){
            LOGGER.debug(writeStr);
        }
    }
    if (model.size() > 0){
        if (filterString == null){
            filterString = "**";
        }

        //TODO
        // writeStr = JsonUtil.format(model, filterString);
        writeStr = JsonUtil.format(model);
    }else{
        //writeStr = new JSONObject().toString(4);
        writeStr = "{}";
    }

    response.getWriter().write(writeStr);

    if (LOGGER.isDebugEnabled()){
        LOGGER.debug("{} \t filterString:[{}] \n{}", RequestUtil.getRequestFullURL(request, UTF8), filterString, writeStr);
    }
}
项目:ldadmin    文件:LocaleFilterTest.java   
public void testJstlLocaleIsSet() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("locale", "es");

    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(new MockHttpSession(null));

    filter.doFilter(request, response, new MockFilterChain());

    assertNotNull(Config.get(request.getSession(), Config.FMT_LOCALE));
}
项目:mamute    文件:MamuteLocalization.java   
@Override @Produces
public ResourceBundle getBundle(Locale locale) {
    Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, null);
    ResourceBundle customBundle = super.getBundle(locale);
    Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, "mamute-messages");
    ResourceBundle mamuteBundle = super.getBundle(locale);

    return new MamuteResourceBundle(customBundle, mamuteBundle);
}
项目:Openfire    文件:LocaleFilter.java   
/**
 * Ssets the locale context-wide based on a call to {@link JiveGlobals#getLocale()}.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    final String pathInfo = ((HttpServletRequest)request).getPathInfo();

    if (pathInfo == null) {
        // Note, putting the locale in the application at this point is a little overkill
        // (ie, every user who hits this filter will do this). Eventually, it might make
        // sense to just set the locale in the user's session and if that's done we might
        // want to honor a preference to get the user's locale based on request headers.
        // For now, this is just a convenient place to set the locale globally.
        Config.set(context, Config.FMT_LOCALE, JiveGlobals.getLocale());
    }
    else {
        try {
            String[] parts = pathInfo.split("/");
            String pluginName = parts[1];
            ResourceBundle bundle = LocaleUtils.getPluginResourceBundle(pluginName);
            LocalizationContext ctx = new LocalizationContext(bundle, JiveGlobals.getLocale());
            Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, ctx);
        }
        catch (Exception e) {
            // Note, putting the locale in the application at this point is a little overkill
            // (ie, every user who hits this filter will do this). Eventually, it might make
            // sense to just set the locale in the user's session and if that's done we might
            // want to honor a preference to get the user's locale based on request headers.
            // For now, this is just a convenient place to set the locale globally.
            Config.set(context, Config.FMT_LOCALE, JiveGlobals.getLocale());
        }
    }
    // Move along:
    chain.doFilter(request, response);
}
项目:class-guard    文件:JstlUtils.java   
/**
 * Exposes JSTL-specific request attributes specifying locale
 * and resource bundle for JSTL's formatting and message tags,
 * using Spring's locale and MessageSource.
 * @param requestContext the context for the current HTTP request,
 * including the ApplicationContext to expose as MessageSource
 */
public static void exposeLocalizationContext(RequestContext requestContext) {
    Config.set(requestContext.getRequest(), Config.FMT_LOCALE, requestContext.getLocale());
    MessageSource messageSource = getJstlAwareMessageSource(
            requestContext.getServletContext(), requestContext.getMessageSource());
    LocalizationContext jstlContext = new SpringLocalizationContext(messageSource, requestContext.getRequest());
    Config.set(requestContext.getRequest(), Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
}
项目:class-guard    文件:JstlUtils.java   
@Override
public ResourceBundle getResourceBundle() {
    HttpSession session = this.request.getSession(false);
    if (session != null) {
        Object lcObject = Config.get(session, Config.FMT_LOCALIZATION_CONTEXT);
        if (lcObject instanceof LocalizationContext) {
            ResourceBundle lcBundle = ((LocalizationContext) lcObject).getResourceBundle();
            return new MessageSourceResourceBundle(this.messageSource, getLocale(), lcBundle);
        }
    }
    return new MessageSourceResourceBundle(this.messageSource, getLocale());
}
项目:class-guard    文件:JstlUtils.java   
@Override
public Locale getLocale() {
    HttpSession session = this.request.getSession(false);
    if (session != null) {
        Object localeObject = Config.get(session, Config.FMT_LOCALE);
        if (localeObject instanceof Locale) {
            return (Locale) localeObject;
        }
    }
    return RequestContextUtils.getLocale(this.request);
}
项目:class-guard    文件:RequestContext.java   
public static Locale getJstlLocale(HttpServletRequest request, ServletContext servletContext) {
    Object localeObject = Config.get(request, Config.FMT_LOCALE);
    if (localeObject == null) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            localeObject = Config.get(session, Config.FMT_LOCALE);
        }
        if (localeObject == null && servletContext != null) {
            localeObject = Config.get(servletContext, Config.FMT_LOCALE);
        }
    }
    return (localeObject instanceof Locale ? (Locale) localeObject : null);
}
项目:class-guard    文件:ViewResolverTests.java   
@Test
public void testInternalResourceViewResolverWithJstl() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.addMessage("code1", locale, "messageX");
    wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    vr.setViewClass(JstlView.class);
    vr.setApplicationContext(wac);

    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());

    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());

    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    HttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
    Map model = new HashMap();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, request, response);

    assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
    assertTrue("Correct rc attribute", request.getAttribute("rc") == null);

    assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
    LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
    assertEquals("messageX", lc.getResourceBundle().getString("code1"));
}
项目:class-guard    文件:ViewResolverTests.java   
@Test
public void testInternalResourceViewResolverWithJstlAndContextParam() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

    MockServletContext sc = new MockServletContext();
    sc.addInitParameter(Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/context/WEB-INF/context-messages");
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.addMessage("code1", locale, "messageX");
    wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    vr.setViewClass(JstlView.class);
    vr.setApplicationContext(wac);

    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());

    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());

    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    HttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
    Map model = new HashMap();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, request, response);

    assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
    assertTrue("Correct rc attribute", request.getAttribute("rc") == null);

    assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
    LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
    assertEquals("message1", lc.getResourceBundle().getString("code1"));
    assertEquals("message2", lc.getResourceBundle().getString("code2"));
}
项目:class-guard    文件:TilesViewTests.java   
@Test
public void tilesJstlView() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

    StaticWebApplicationContext wac = prepareWebApplicationContext();

    InternalResourceViewResolver irvr = new InternalResourceViewResolver();
    irvr.setApplicationContext(wac);
    irvr.setViewClass(TilesJstlView.class);
    View view = irvr.resolveViewName("testTile", new Locale("nl", ""));

    MockHttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
    wac.addMessage("code1", locale, "messageX");
    view.render(new HashMap<String, Object>(), request, response);

    assertEquals("/WEB-INF/jsp/layout.jsp", response.getForwardedUrl());
    ComponentContext cc = (ComponentContext) request.getAttribute(ComponentConstants.COMPONENT_CONTEXT);
    assertNotNull(cc);
    PathAttribute attr = (PathAttribute) cc.getAttribute("content");
    assertEquals("/WEB-INF/jsp/content.jsp", attr.getValue());

    assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
    LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
    assertEquals("messageX", lc.getResourceBundle().getString("code1"));
}
项目:class-guard    文件:TilesViewTests.java   
@Test
public void tilesJstlViewWithContextParam() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

    StaticWebApplicationContext wac = prepareWebApplicationContext();
    ((MockServletContext) wac.getServletContext()).addInitParameter(
            Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/servlet/view/tiles/context-messages");

    InternalResourceViewResolver irvr = new InternalResourceViewResolver();
    irvr.setApplicationContext(wac);
    irvr.setViewClass(TilesJstlView.class);
    View view = irvr.resolveViewName("testTile", new Locale("nl", ""));

    MockHttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
    MockHttpServletResponse response = new MockHttpServletResponse();
    wac.addMessage("code1", locale, "messageX");
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));

    view.render(new HashMap<String, Object>(), request, response);
    assertEquals("/WEB-INF/jsp/layout.jsp", response.getForwardedUrl());
    ComponentContext cc = (ComponentContext) request.getAttribute(ComponentConstants.COMPONENT_CONTEXT);
    assertNotNull(cc);
    PathAttribute attr = (PathAttribute) cc.getAttribute("content");
    assertEquals("/WEB-INF/jsp/content.jsp", attr.getValue());

    LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
    assertEquals("message1", lc.getResourceBundle().getString("code1"));
    assertEquals("message2", lc.getResourceBundle().getString("code2"));
}
项目:musicrecital    文件:LocaleFilterTest.java   
public void testJstlLocaleIsSet() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("locale", "es");

    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(new MockHttpSession(null));

    filter.doFilter(request, response, new MockFilterChain());

    assertNotNull(Config.get(request.getSession(), Config.FMT_LOCALE));
}
项目:g3server    文件:LocaleFilter.java   
/**
 * Ssets the locale context-wide based on a call to {@link JiveGlobals#getLocale()}.
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    final String pathInfo = ((HttpServletRequest)request).getPathInfo();

    if (pathInfo == null) {
        // Note, putting the locale in the application at this point is a little overkill
        // (ie, every user who hits this filter will do this). Eventually, it might make
        // sense to just set the locale in the user's session and if that's done we might
        // want to honor a preference to get the user's locale based on request headers.
        // For now, this is just a convenient place to set the locale globally.
        Config.set(context, Config.FMT_LOCALE, JiveGlobals.getLocale());
    }
    else {
        try {
            String[] parts = pathInfo.split("/");
            String pluginName = parts[1];
            ResourceBundle bundle = LocaleUtils.getPluginResourceBundle(pluginName);
            LocalizationContext ctx = new LocalizationContext(bundle, JiveGlobals.getLocale());
            Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, ctx);
        }
        catch (Exception e) {
            // Note, putting the locale in the application at this point is a little overkill
            // (ie, every user who hits this filter will do this). Eventually, it might make
            // sense to just set the locale in the user's session and if that's done we might
            // want to honor a preference to get the user's locale based on request headers.
            // For now, this is just a convenient place to set the locale globally.
            Config.set(context, Config.FMT_LOCALE, JiveGlobals.getLocale());
        }
    }
    // Move along:
    chain.doFilter(request, response);
}
项目:displaytag    文件:I18nJstlAdapter.java   
/**
 * @see LocaleResolver#resolveLocale(PageContext)
 */
@Override
public Locale resolveLocale(PageContext pageContext)
{
    Locale locale = (Locale) Config.find(pageContext, Config.FMT_LOCALE);
    if (locale == null)
    {
        locale = pageContext.getRequest().getLocale();
    }
    return locale;
}
项目:openfire    文件:LocaleFilter.java   
/**
 * Ssets the locale context-wide based on a call to {@link JiveGlobals#getLocale()}.
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    final String pathInfo = ((HttpServletRequest)request).getPathInfo();

    if (pathInfo == null) {
        // Note, putting the locale in the application at this point is a little overkill
        // (ie, every user who hits this filter will do this). Eventually, it might make
        // sense to just set the locale in the user's session and if that's done we might
        // want to honor a preference to get the user's locale based on request headers.
        // For now, this is just a convenient place to set the locale globally.
        Config.set(context, Config.FMT_LOCALE, JiveGlobals.getLocale());
    }
    else {
        try {
            String[] parts = pathInfo.split("/");
            String pluginName = parts[1];
            ResourceBundle bundle = LocaleUtils.getPluginResourceBundle(pluginName);
            LocalizationContext ctx = new LocalizationContext(bundle, JiveGlobals.getLocale());
            Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, ctx);
        }
        catch (Exception e) {
            // Note, putting the locale in the application at this point is a little overkill
            // (ie, every user who hits this filter will do this). Eventually, it might make
            // sense to just set the locale in the user's session and if that's done we might
            // want to honor a preference to get the user's locale based on request headers.
            // For now, this is just a convenient place to set the locale globally.
            Config.set(context, Config.FMT_LOCALE, JiveGlobals.getLocale());
        }
    }
    // Move along:
    chain.doFilter(request, response);
}
项目:hurraa    文件:ChangeLocaleController.java   
@Path("/change-location/{lenguageName}/{country}")
public void changeLocale(String lenguageName , String country , Result result, HttpSession session , HttpServletRequest request ){
    Locale locale = new Locale( lenguageName , country );
    Config.set( session, Config.FMT_FALLBACK_LOCALE, locale);
    Config.set (session, Config.FMT_LOCALE, locale);
    redirectBack(result , request);
}
项目:stripes-stuff    文件:JstlBundleInterceptor.java   
@Override
protected void findResourceBundleFactory(Configuration configuration)
        throws Exception
{
    if (configuration.getServletContext().getInitParameter(Config.FMT_LOCALIZATION_CONTEXT) == null)
    {
        super.findResourceBundleFactory(configuration);
    }
    else
    {
        LOGGER.debug("Skipping ResourceBundleFactory initialization, as there is already a default JSTL resource " +
                     "bundle configured via the config parameter \"{}\".", Config.FMT_LOCALIZATION_CONTEXT);
    }
}
项目:stripes-stuff    文件:JstlBundleInterceptor.java   
/**
    * Sets the message resource bundle in the request using the JSTL's mechanism.
    */
@Override
   protected void setMessageResourceBundle(HttpServletRequest request, ResourceBundle bundle)
{
    Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, new LocalizationContext(bundle, request.getLocale()));
       LOGGER.debug("Enabled JSTL localization using: ", bundle);
       LOGGER.debug("Loaded resource bundle ", bundle, " as default bundle");
   }
项目:stripes-stuff    文件:JstlLocalizationBundleFactory.java   
/**
 * Invoked directly after instantiation to allow the configured component to perform
 * one time initialization.  Components are expected to fail loudly if they are not
 * going to be in a valid state after initialization.
 *
 * @param configuration the Configuration object being used by Stripes
 * @throws Exception should be thrown if the component cannot be configured well enough to use.
 */
public void init(Configuration configuration)
        throws Exception
{
    bundleName = configuration.getServletContext().getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
    if (bundleName == null)
    {
        throw new StripesRuntimeException("JSTL has no resource bundle configured. Please set the context " +
                                          "parameter " + Config.FMT_LOCALIZATION_CONTEXT + " to the name of the " +
                                          "ResourceBundle to use.");
    }
}
项目:dandelion-datatables    文件:JstlLocaleResolver.java   
@Override
public Locale resolveLocale(HttpServletRequest request) {

   Object locale = Config.get(request, Config.FMT_LOCALE);
   if (locale == null) {
      HttpSession session = request.getSession(false);
      if (session != null) {
         locale = Config.get(session, Config.FMT_LOCALE);
      }
   }

   return (locale instanceof Locale ? (Locale) locale : null);
}