@RequestMapping(value = "/logout")
public String logout(HttpSession session) {
session.removeAttribute("user");
return "/login";
}
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();
}
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);
}
<mvc:annotation-driven> <mvc:argument-resolvers> <bean class="pac.common.SessionScopeMethodArgumentResolver" /> </mvc:argument-resolvers> </mvc:annotation-driven> <mvc:default-servlet-handler />
@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";
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有