SpringMVC基于session存储注解的值 (全局使用)

用户网站 有一个中英文切换的地方,会把切换后的language的属性值l, 存入session 

这个值language在多个控制器参数里面会被调用,现在将其声明为注解 利用拦截器

再切换语言的时候 修改language的值

利用aop思想 一处修改多处直接拿到该值 不需要每次都在从session中获得该值 稍微方便一下下。。

 

1.注解声明



@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Language {
String value() default "language";
}

 

2 . 拦截器

public class LanguageValueInterceptor implements HandlerMethodArgumentResolver {


@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterAnnotation(Language.class) != null;
}


@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
Language parameterAnnotation = parameter.getParameterAnnotation(Language.class);
if (parameterAnnotation != null) {
String attributeKey = parameterAnnotation.value();
HttpServletRequest nativeRequest = (HttpServletRequest) webRequest.getNativeRequest();
return nativeRequest.getSession().getAttribute(attributeKey);//现在是从session中获取
}
return null;
}


}

 

3 拦截器配置文件

 

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:flex="http://www.springframework.org/schema/flex"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd    //注意这里是3.1  3.0会报错
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">
<context:component-scan base-package="com.bimatrix.revit.controller" />
<!-- <mvc:annotation-driven conversion-service="conversionService" /> -->
<mvc:annotation-driven conversion-service="conversionService">
<mvc:argument-resolvers>
<bean class="com.bimatrix.revit.interceptor.LanguageValueInterceptor" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
<mvc:default-servlet-handler />

 

4.中英文切换的地方 写入session

@RequestMapping(value = "changeLanguage", method = RequestMethod.GET)
public String changeLanguage(HttpServletRequest request, HttpServletResponse response,String locale,
@Language String language) {
List<RevitNewEntity> hotRevitList = revitNewEntityService.getHotRevit();
request.setAttribute("hotRevitList", hotRevitList);
List<RevitAddInShowDto> nowDownList = revitNewEntityService.getDownLoadNowRevit();
request.setAttribute("nowDownList", nowDownList);
System.out.println("0 当前language :"+language );
System.out.println("1 当前lan :"+request.getSession().getAttribute("language") );

String langType = locale;
if(langType==null||langType.equals("")){
return "/index";
}else{
if (langType.equals("zh")) {
Locale locale1 = new Locale("zh", "CN");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale1);
request.getSession().setAttribute("language", "zh");
} else if (langType.equals("en")) {
Locale locale1 = new Locale("en", "US");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale1);
request.getSession().setAttribute("language", "en");
} else {
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, LocaleContextHolder.getLocale());
}
}
System.out.println("2 当前lan :"+request.getSession().getAttribute("language") );
return "/index";
}

切换输出效果 

 

0 当前language :null
1 当前lan :null
2 当前lan :zh
0 当前language :zh
1 当前lan :zh
2 当前lan :zh

 

说明language  已经写入session

这样其他地方 控制器 就可以直接获得 注解的值了。。

@RequestMapping(value = "list", method = RequestMethod.GET)
public ModelAndView list(SearchRevitVO vo, Integer sortType , HttpServletRequest request,@Language String language) throws Exception {

 

 

5. jsp页面的直接使用 实现数据库级别的网站国际化,对于小网站可以对该表增加英文字段 减少开发周期

 

<c:if test="${sessionScope.language eq 'en' }">   
  ${tag.nameEn}
</c:if>
 <c:if test="${sessionScope.language eq 'zh' || sessionScope.language eq null}">   //null是初始化时候防止显示为空
 ${tag.name}
  </c:if>

这么看 其实第四步可能没有必要 第五步才是需求使用的 只需要存到session也可以 

 

 

 

 

赠送视频如下:

 

或者一套redis视频

 

不定期送视频 

1700G it视频  等你来!

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值