使用SpringMVC後,感覺註解方式甚是好用,於是想着自定義註解來實現session檢查,用到了SpringMVC的攔截器。
1.自定義註解。
package com.xxx.order;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SessionCheck {
boolean check() default true;
}
2.攔截器。
package com.raycloud.order;
import com.raycloud.order.common.Constant;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 描述:
*
* @author chenpeng
* @date 2015/6/11 15:16
*/
public class SessionInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod) handler;
SessionCheck sessionCheck = handlerMethod.getMethod().getAnnotation(SessionCheck.class);
if (sessionCheck != null && sessionCheck.check()) {
System.out.println("你遇到了:@sessionCheck");
response.getOutputStream().print("{\"message\":\"session invalid\",\"result\":700}");
return false;
} else {
System.out.println("未受到攔截");
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
3.攔截器配置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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.0.xsd
">
<context:component-scan base-package="com.raycloud"/>
<mvc:annotation-driven/>
<mvc:interceptors>
<bean class="com.raycloud.order.SessionInterceptor" />
</mvc:interceptors>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
上面兩個還好寫,這個配置是找了不少時間,具體的可以看這裏========================》
使用方法:
這樣就能實現自定義註解方式的攔截了哈,攔截成功後會打印出自定義的信息。
控制檯信息輸出:
總結:
難點在於SpringMVC的配置了,遇到過各種問題:
1.配置好了攔截器,但攔截器沒有起到任何效果
如果你用的是DefaultAnnotationHandlerMapping,而且攔截器路徑什麼的都是正確的,那麼你把<mvc:annotation-driven/>去掉試試
2.攔截器可以用,但是無法自定義註解
如果用的DefaultAnnotationHandlerMapping,需要在後面再引入<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
3.自定義註解ok了,攔截頁面跳轉也ok了,但是攔截json數據會出現406錯誤~那麼你就改成我這種配置把~我就是這麼改過來的。