程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> SpringMVCʵÏÖcontrollerÖлñÈ¡sessionµÄʵÀý´úÂë

SpringMVCʵÏÖcontrollerÖлñÈ¡sessionµÄʵÀý´úÂë

編輯:關於JAVA

SpringMVCʵÏÖcontrollerÖлñÈ¡sessionµÄʵÀý´úÂë。本站提示廣大學習愛好者:(SpringMVCʵÏÖcontrollerÖлñÈ¡sessionµÄʵÀý´úÂë)文章只能為提供參考,不一定能成為您想要的結果。以下是SpringMVCʵÏÖcontrollerÖлñÈ¡sessionµÄʵÀý´úÂë正文


ƽʱʹÓÃ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¸ºÔð£¬àÅ£¬Õâ¸öºÃ¡£

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íû¶Ô´ó¼ÒµÄѧϰÓÐËù°ïÖú£¬Ò²Ï£Íû´ó¼Ò¶à¶àÖ§³Ö½Å±¾Ö®¼Ò¡£

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved