當前位置: 首頁>>代碼示例>>Java>>正文


Java RequestContextUtils.getLocaleResolver方法代碼示例

本文整理匯總了Java中org.springframework.web.servlet.support.RequestContextUtils.getLocaleResolver方法的典型用法代碼示例。如果您正苦於以下問題:Java RequestContextUtils.getLocaleResolver方法的具體用法?Java RequestContextUtils.getLocaleResolver怎麽用?Java RequestContextUtils.getLocaleResolver使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.web.servlet.support.RequestContextUtils的用法示例。


在下文中一共展示了RequestContextUtils.getLocaleResolver方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: preHandle

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
	if (request.getUserPrincipal() != null) {
		String codiIdioma = request.getParameter(this.paramName);
		
		if (codiIdioma != null) {
			personaService.savePrefIdioma(codiIdioma);
			LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
			if (localeResolver == null)
				throw new IllegalStateException  ("No LocaleResolver found: not in a DispatcherServlet request?");
			LocaleEditor localeEditor = new LocaleEditor();
			localeEditor.setAsText(codiIdioma);
			localeResolver.setLocale(request, response, (Locale)localeEditor.getValue());
		}
	}
	return true;
}
 
開發者ID:GovernIB,項目名稱:helium,代碼行數:17,代碼來源:IdiomaInterceptor.java

示例2: preHandle

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
    if (localeResolver == null) {
        return true;
    }

    try {
        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for (Cookie cookie : cookies){
                if (Objects.equal("lang", cookie.getName())){
                    LocaleContextHolder.setLocale(new Locale(cookie.getValue()));
                }
            }
        }
    } catch (Exception e) {
        Logs.error("occur errors when resolve locale: {}", Throwables.getStackTraceAsString(e));
    }

    return true;
}
 
開發者ID:ihaolin,項目名稱:diablo,代碼行數:24,代碼來源:LocaleInterceptor.java

示例3: preHandle

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
    String newLocale = request.getParameter(getParamName());
    if(newLocale != null) {
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        if(localeResolver == null) {
            throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
        }

        Locale locale = StringUtils.parseLocaleString(newLocale);
        localeResolver.setLocale(request, response, locale);
        localeHolder.setLocale(locale);
    }

    return true;
}
 
開發者ID:Javakit,項目名稱:Javakit,代碼行數:17,代碼來源:LocaleChangeInterceptorWrapper.java

示例4: preHandle

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request,
		HttpServletResponse response, Object handler)
		throws ServletException {
	LocaleResolver localeResolver = RequestContextUtils
			.getLocaleResolver(request);
	if (localeResolver == null) {
		throw new IllegalStateException(
				"No LocaleResolver found: not in a DispatcherServlet request?");
	}
//	CmsSite site = CmsUtils.getSite(request);
	CmsSite site=CmsThreadVariable.getSite();
	String newLocale = site.getLocaleAdmin();
	LocaleEditor localeEditor = new LocaleEditor();
	localeEditor.setAsText(newLocale);
	localeResolver.setLocale(request, response, (Locale) localeEditor
			.getValue());
	// Proceed in any case.
	return true;
}
 
開發者ID:huanzhou,項目名稱:jeecms6,代碼行數:21,代碼來源:AdminLocaleInterceptor.java

示例5: preHandle

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request,
		HttpServletResponse response, Object handler)
		throws ServletException {
	LocaleResolver localeResolver = RequestContextUtils
			.getLocaleResolver(request);
	if (localeResolver == null) {
		throw new IllegalStateException(
				"No LocaleResolver found: not in a DispatcherServlet request?");
	}
	CmsSite site = CmsUtils.getSite(request);
	String newLocale = site.getLocaleFront();
	LocaleEditor localeEditor = new LocaleEditor();
	localeEditor.setAsText(newLocale);
	localeResolver.setLocale(request, response, (Locale) localeEditor
			.getValue());
	// Proceed in any case.
	return true;
}
 
開發者ID:huanzhou,項目名稱:jeecms6,代碼行數:20,代碼來源:FrontLocaleInterceptor.java

示例6: getMessage

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
/**
 * 獲得國際化信息
 * 
 * @param request
 *            HttpServletRequest
 * @param code
 *            國際化代碼
 * @param args
 *            替換參數
 * @return
 * @see org.springframework.context.MessageSource#getMessage(String,
 *      Object[], Locale)
 */
public static String getMessage(HttpServletRequest request, String code,
		Object... args) {
	WebApplicationContext messageSource = RequestContextUtils.getWebApplicationContext(request);
	if (messageSource == null) {
		throw new IllegalStateException("WebApplicationContext not found!");
	}
	LocaleResolver localeResolver = RequestContextUtils
			.getLocaleResolver(request);
	Locale locale;
	if (localeResolver != null) {
		locale = localeResolver.resolveLocale(request);
	} else {
		locale = request.getLocale();
	}
	return messageSource.getMessage(code, args, locale);
}
 
開發者ID:huanzhou,項目名稱:jeecms6,代碼行數:30,代碼來源:MessageResolver.java

示例7: preHandle

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws Exception {
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);

	// Check if it's our case
	if (localeResolver == null) {
		throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
	}
	if (!(localeResolver instanceof CookieLocaleResolver)) {
		return true;
	}

	// Check if locale not in cookie.
	// If so, then force it to store in cookie
	CookieLocaleResolver cookieLocaleResolver = (CookieLocaleResolver) localeResolver;
	Cookie cookie = WebUtils.getCookie(request, cookieLocaleResolver.getCookieName());
	if (cookie == null) {
		cookieLocaleResolver.setLocale(request, response, localeResolver.resolveLocale(request));
	}

	return true;
}
 
開發者ID:skarpushin,項目名稱:summerb,代碼行數:24,代碼來源:ForceLocaleToCoockieInterceptor.java

示例8: preHandle

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws Exception {
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);

	// Check if it's our case
	if (localeResolver == null) {
		throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
	}
	if (!(localeResolver instanceof LocaleResolverUserBasedImpl)) {
		return true;
	}

	// Check if locale not in cookie.
	// If so, then force it to store in cookie
	LocaleResolverUserBasedImpl cookieLocaleResolver = (LocaleResolverUserBasedImpl) localeResolver;
	Locale localeFromCookie = cookieLocaleResolver.resolveLocaleFromCookie(request);
	Locale localeFromUser = cookieLocaleResolver.resolveLocale(request);
	if (localeFromUser != null && !localeFromUser.equals(localeFromCookie)) {
		cookieLocaleResolver.setLocale(request, response, localeFromUser);
	}

	return true;
}
 
開發者ID:skarpushin,項目名稱:summerb,代碼行數:25,代碼來源:ForceLocaleToCoockieUserAwareInterceptor.java

示例9: getLocale

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
/**
 * Get locale for curent request
 * 
 * @return
 */
public static Locale getLocale() {
	// Plan A: Try to get it from locale context holder
	Locale locale = LocaleContextHolder.getLocale();
	if (locale != null) {
		return locale;
	}

	// Plan B: Try to get it from request
	HttpServletRequest request = get();
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
	if (localeResolver == null) {
		log.warn("Cannot get locale resolver. Will fallback to: " + defaultLocale);
		return defaultLocale;
	}
	locale = localeResolver.resolveLocale(request);
	if (locale != null) {
		return locale;
	}

	// fallback
	log.warn("Cannot resolve locale. Will fallback to: " + defaultLocale);
	return defaultLocale;
}
 
開發者ID:skarpushin,項目名稱:summerb,代碼行數:29,代碼來源:CurrentRequestUtils.java

示例10: preHandle

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    Object newLocale = request.getAttribute(LOCALE_ATTRIBUTE_NAME);
    if (newLocale != null) {
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        if (localeResolver == null) {
            throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
        }
        localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale.toString()));
    }
    return true;
}
 
開發者ID:xtianus,項目名稱:yadaframework,代碼行數:13,代碼來源:YadaLocalePathChangeInterceptor.java

示例11: getLocaleResolver

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
private static LocaleResolver getLocaleResolver(HttpServletRequest request) {
    final LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);

    if (localeResolver == null) {
        throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
    }

    return localeResolver;
}
 
開發者ID:suomenriistakeskus,項目名稱:oma-riista-web,代碼行數:10,代碼來源:LanguageChangeController.java

示例12: changeLang

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
@RequestMapping(value = "/changeLang", method = RequestMethod.POST)
@ResponseBody
public String changeLang(HttpServletRequest request, HttpServletResponse response, @RequestParam String lang){
    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
    if ("zh".equals(lang)) {
        // request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("zh", "CN"));
        localeResolver.setLocale(request, response, new Locale("zh", "CN"));
    } else if("en".equals(lang)){
        // request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("en", "US"));
        localeResolver.setLocale(request, response, new Locale("en", "US"));
    }
    return "lang:" + LocaleContextHolder.getLocale().getLanguage();
}
 
開發者ID:javahongxi,項目名稱:whatsmars,代碼行數:14,代碼來源:SampleController.java

示例13: preHandle

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws ServletException {

	String newLocale = request.getParameter(getParamName());
	if (newLocale != null) {
		if (checkHttpMethod(request.getMethod())) {
			LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
			if (localeResolver == null) {
				throw new IllegalStateException(
						"No LocaleResolver found: not in a DispatcherServlet request?");
			}
			try {
				localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
			}
			catch (IllegalArgumentException ex) {
				if (isIgnoreInvalidLocale()) {
					logger.debug("Ignoring invalid locale value [" + newLocale + "]: " + ex.getMessage());
				}
				else {
					throw ex;
				}
			}
		}
	}
	// Proceed in any case.
	return true;
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:29,代碼來源:LocaleChangeInterceptor.java

示例14: handleRequest

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
@Override
@SuppressWarnings("deprecation")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	if (!(RequestContextUtils.getWebApplicationContext(request) instanceof SimpleWebApplicationContext)) {
		throw new ServletException("Incorrect WebApplicationContext");
	}
	if (!(RequestContextUtils.getLocaleResolver(request) instanceof AcceptHeaderLocaleResolver)) {
		throw new ServletException("Incorrect LocaleResolver");
	}
	if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) {
		throw new ServletException("Incorrect Locale");
	}
	return null;
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:16,代碼來源:SimpleWebApplicationContext.java

示例15: postHandle

import org.springframework.web.servlet.support.RequestContextUtils; //導入方法依賴的package包/類
@Override
public void postHandle(HttpServletRequest request,
		HttpServletResponse response, Object handler,
		ModelAndView modelAndView) throws Exception {
	LocaleResolver localeResolver = RequestContextUtils
			.getLocaleResolver(request);
	if (localeResolver == null) {
		throw new IllegalStateException(
				"No LocaleResolver found: not in a DispatcherServlet request?");
	}
	if (modelAndView != null) {
		modelAndView.getModelMap().addAttribute(LOCALE,
				localeResolver.resolveLocale(request).toString());
	}
}
 
開發者ID:huanzhou,項目名稱:jeecms6,代碼行數:16,代碼來源:AdminLocaleInterceptor.java


注:本文中的org.springframework.web.servlet.support.RequestContextUtils.getLocaleResolver方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。