springmvc本身不能像struts2那样使用注解拦截action,这在使用中非常不方便,我们来扩展下springmvc,增加一个类,就可以实现注解拦截器.
首先把下面这个类拷贝进去:
package interceptor;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public abstract class BaseInterceptor extends HandlerInterceptorAdapter {
public String success;
public String failed;
public boolean isMyHandler(Object handler) {
if (!(handler instanceof HandlerMethod))
return false;
HandlerMethod handlerMethod = (HandlerMethod) handler;
Interceptor interceptor = handlerMethod.getMethodAnnotation(Interceptor.class);
if (interceptor == null)
return false;
if (!interceptor.name().equals(this.getClass().getSimpleName()) && !interceptor.name().equals(this.getClass().getName()))
return false;
success = interceptor.success();
failed = interceptor.failed();
return true;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (isMyHandler(handler)) {
return runHandler(response, runHandler(request, response));
}
return super.preHandle(request, response, handler);
}
public abstract boolean runHandler(HttpServletRequest request, HttpServletResponse response);
/**
* 运行结果
*
*@param response
*@param isInterceptor
* 是否拦截,true拦截,跳转向failed指向页面,false:不拦截,跳转向success指向页面;
* success和failed为空时不做任何操作
*@return
*@throws Exception
*/
public boolean runHandler(HttpServletResponse response, boolean isInterceptor) throws Exception {
if (!isInterceptor) {
if (!success.equals("")) {
response.sendRedirect(success);
return false;
}
} else {
if (!failed.equals("")) {
response.sendRedirect(failed);
return false;
}
}
return true;
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public static @interface Interceptor {
public String name();
public String success() default "";
public String failed() default "";
}
}
以上代码仅支持spring3.1.6以上版本,低一些的版本赶快升级吧.说真的,如果不能注解拦截器,开发时候像是登录验证/权限认证太费事了.
自己写一个拦截器,继承与上面的类,如下:
package interceptor;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.UserServiceImpl;
public class LoginCheck extends BaseInterceptor {
@Resource
private UserServiceImpl userService;
@Override
public boolean runHandler(HttpServletRequest request, HttpServletResponse response) {
//.....判断是否登陆,判断是否有权限等逻辑代码
return false;
}
}
在springmvc配置文件中配置拦截器:
拦截器配置为拦截所有controller.
在controller方法上写
@Interceptor(name = "LoginCheck", failed = "/user/login")
如:
@Interceptor(name = "LoginCheck", failed = "/user/login")
@RequestMapping("/user/del/{id}")
public void del(@PathVariable int id, HttpServletResponse response) throws IOException {
userService.del(id);
response.sendRedirect("/users/1");
}
这样在访问del方法的时候,就会先运行logincheck拦截器,如果拦截器拦截了,就会跳转到user/login页面,是不是和struts2的拦截器一样好用了?