Java Code Examples for org.springframework.web.context.request.ServletRequestAttributes#getAttribute()

The following examples show how to use org.springframework.web.context.request.ServletRequestAttributes#getAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageResourceExtension.java    From Spring-Boot-I18n-Pro with MIT License 6 votes vote down vote up
@Override
protected String resolveCodeWithoutArguments(String code, Locale locale) {
    // 获取request中设置的指定国际化文件名
    ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    final String i18File = (String) attr.getAttribute(I18N_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
    if (!StringUtils.isEmpty(i18File)) {
        //获取在basenameSet中匹配的国际化文件名
        String basename = getBasenameSet().stream()
                .filter(name -> StringUtils.endsWithIgnoreCase(name, i18File))
                .findFirst().orElse(null);
        if (!StringUtils.isEmpty(basename)) {
            //得到指定的国际化文件资源
            ResourceBundle bundle = getResourceBundle(basename, locale);
            if (bundle != null) {
                return getStringOrNull(bundle, code);
            }
        }
    }
    //如果指定i18文件夹中没有该国际化字段,返回null会在ParentMessageSource中查找
    return null;
}
 
Example 2
Source File: RequestHolder.java    From mogu_blog_v2 with Apache License 2.0 5 votes vote down vote up
/**
 * 获取session的Attribute
 *
 * @param name session的key
 * @return Object
 */
public static Object getSession(String name) {
    log.debug("getSession -- Thread id :{}, name : {}", Thread.currentThread().getId(), Thread.currentThread().getName());
    ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
    if (null == servletRequestAttributes) {
        return null;
    }
    return servletRequestAttributes.getAttribute(name, RequestAttributes.SCOPE_SESSION);
}
 
Example 3
Source File: BaseServerController.java    From Jpom with MIT License 5 votes vote down vote up
public static UserModel getUserModel() {
    ServletRequestAttributes servletRequestAttributes = tryGetRequestAttributes();
    if (servletRequestAttributes == null) {
        return null;
    }
    return (UserModel) servletRequestAttributes.getAttribute(LoginInterceptor.SESSION_NAME, RequestAttributes.SCOPE_SESSION);
}
 
Example 4
Source File: RequestHolder.java    From springboot-shiro with MIT License 5 votes vote down vote up
/**
 * 获取session的Attribute
 *
 * @param name session的key
 * @return Object
 */
public static Object getSession(String name) {
    log.debug("getSession -- Thread id :{}, name : {}", Thread.currentThread().getId(), Thread.currentThread().getName());
    ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
    if (null == servletRequestAttributes) {
        return null;
    }
    return servletRequestAttributes.getAttribute(name, RequestAttributes.SCOPE_SESSION);
}
 
Example 5
Source File: RequestHolder.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取session的Attribute
 *
 * @param name session的key
 * @return Object
 */
public static Object getSession(String name) {
    log.debug("getSession -- Thread id :{}, name : {}", Thread.currentThread().getId(), Thread.currentThread().getName());
    ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
    if (null == servletRequestAttributes) {
        return null;
    }
    return servletRequestAttributes.getAttribute(name, RequestAttributes.SCOPE_SESSION);
}