springmvc⾥使⽤注解进⾏配置
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.hod.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 = MethodAnnotation(Interceptor.class);
if (interceptor == null)
return false;
if (!interceptor.name().Class().getSimpleName()) && !interceptor.name().Class().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);
/**
* 运⾏结果mvc的controller
*
* @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;
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配置⽂件中配置:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="interceptor.LoginCheck"></bean>
</mvc:interceptor>
</mvc:interceptors>
配置为拦截所有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的⼀样好⽤了?

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。