SpringMVC实现controller中获取session的实例代码

所属分类: 软件编程 / java 阅读数: 25
收藏 0 赞 0 分享

平时使用springMVC,在方法中访问session中经常很自然地调用Servlet API。用起来非常直观方便,一直没有多考虑什么。

比如这样:

@RequestMapping(value = "/logout")
public String logout(HttpSession session) {
 session.removeAttribute("user");
 return "/login";
} 

但毕竟这样对Servlet API产生了依赖,感觉不够pojo。

于是我试着解决这个问题。

我打算用一个注解,名字就叫"sessionScope",Target可以是一个Method,也可以是Parameter。

也就是说:

 import java.lang.annotation.Documented;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 @Target({ ElementType.PARAMETER,ElementType.METHOD })
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 public @interface SessionScope {
  String value();
 }

然后我要注册一个ArgumentResolver,专门解决被注解的东东,把他们统统替换成session里的东西。

代码如下:

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

public class SessionScopeMethodArgumentResolver implements
  HandlerMethodArgumentResolver {

 @Override
 public boolean supportsParameter(MethodParameter parameter) {
  //让方法和参数,两种target通过
  if(parameter.hasParameterAnnotation(SessionScope.class))return true;
  else if (parameter.getMethodAnnotation(SessionScope.class) != null)return true;
  return false;
 }

 @Override 
 public Object resolveArgument(MethodParameter parameter,
   ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
   WebDataBinderFactory binderFactory) throws Exception {
  String annoVal = null;

  if(parameter.getParameterAnnotation(SessionScope.class)!=null){
   logger.debug("param anno val::::"+parameter.getParameterAnnotation(SessionScope.class).value());
   annoVal = parameter.getParameterAnnotation(SessionScope.class).value();
  }else if(parameter.getMethodAnnotation(SessionScope.class)!=null){
   logger.debug("method anno val::::"+parameter.getMethodAnnotation(SessionScope.class).value());
   annoVal = parameter.getMethodAnnotation(SessionScope.class)!=null?
          StringUtils.defaultString(parameter.getMethodAnnotation(SessionScope.class).value())
            :StringUtils.EMPTY;
  }
                                
  if (webRequest.getAttribute(annoVal,RequestAttributes.SCOPE_SESSION) != null){
   return webRequest.getAttribute(annoVal,RequestAttributes.SCOPE_SESSION);
  }
  else
   return null;
 }
                                      
 final Logger logger = LoggerFactory.getLogger(SessionScopeMethodArgumentResolver.class);
}

supportParameter判断对象是否被注解,被注解则进行resolve。

resolve时获取注解值,注解值为session key,用webRequest从session scope中取值。

另外需要将此配置放到org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter的customArgumentResolvers列表中,可以使用bean标签配置,也可以直接使用mvc标签。

即:

namespace为:xmlns:mvc="http://www.springframework.org/schema/mvc"

schema location为:http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

<mvc:annotation-driven>
 <mvc:argument-resolvers>
  <bean class="pac.common.SessionScopeMethodArgumentResolver" />
 </mvc:argument-resolvers>
</mvc:annotation-driven>
<mvc:default-servlet-handler />

话说mvc:annotation-driven和mvc:default-servlet-handler的顺序不能颠倒,该不会只有我出现这种情况吧- -..

现在可以在controller中使用了,比如:

@RequestMapping(value = "/index")
@SessionScope("currentUser")
public ModelAndView index(User currentUser) {
 ModelAndView mav;
 if (currentUser==null || currentUser.getId()==null)
  mav = new ModelAndView("/login");
 else {
  mav = new ModelAndView("/index");
 }
 return mav;
}

或者在参数上注解:

@RequestMapping(value = "/welcome")
public String welcome(@SessionScope("currentUser")User currentUser) {
 return "/main";
}

至于更新session,目前只是用@sessionAttributes配合ModelMap的方式。

或者我可以不用这样做,直接集成Apache Shiro,在controller中直接getSubject()。

把用户信息完全让shiro负责,嗯,这个好。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Collections工具类_动力节点Java学院整理

Collections工具类提供了大量针对Collection/Map的操作。这篇文章主要介绍了Collections工具类_动力节点Java学院整理,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringMVC集成Swagger实例代码

本篇文章主要介绍了SpringMVC集成Swagger实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

十大常见Java String问题_动力节点Java学院整理

本文介绍Java中关于String最常见的10个问题,需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(13) 微信JSSDK中Config配置

这篇文章主要为大家详细介绍了Java微信公众平台开发第十三步,微信JSSDK中Config配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java实现一个达达租车系统的步骤详解

这篇文章主要给大家介绍了利用Java实现一个达达租车系统的步骤,文中给出了详细的实现思路和示例代码,并在文末给出了完整的源码供大家学习下载,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

Java微信公众平台开发(14) 微信web开发者工具使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十四步,微信web开发者工具的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Spring Boot整合RabbitMQ实例(Topic模式)

Topic Exchange 转发消息主要是根据通配符。接下来通过本文给大家分享Spring Boot整合RabbitMQ实例(Topic模式),需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(15) 微信JSSDK的使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十五步,微信JSSDK的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java多线程的同步方法实例代码

这篇文章主要介绍了 java多线程的同步方法实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合RabbitMQ实例详解(Fanout模式)

这篇文章主要介绍了spring boot整合RabbitMQ的实例讲解(Fanout模式),非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多