spring-base.xml中配置:
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<import resource="spring-db.xml"></import>
<mvc:interceptors>
<mvc:interceptor>
<mvc:exclude-mapping path="/**/*.do" />
<bean class="com.kd.winery.Interceptor.UserInterceptor"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:exclude-mapping path="/**/*.action" />
<bean class="com.kd.winery.Interceptor.AdminInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
其中配置了两个拦截器,为了方便,用户前台我的请求都是.do结尾,管理员后台的请求都是.action结尾,目的是想实现用户输入前台的地址跳转到用户登录,输入后台的地址跳转到管理员登陆页
然后在com.kd.winery.Interceptor.AdminInterceptor的文件结构下配置两个拦截器:
其中前台拦截:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.kd.demo.domain.User;
import com.kd.demo.util.SessionUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.kd.winery.domain.Emp;
public class UserInterceptor implements HandlerInterceptor {
public final String DEFAULT="/user/toLogin";//默认登录页面
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String servletPath = request.getServletPath();
System.out.println(servletPath);
Emp emp = (Emp) request.getSession().getAttribute("emp");
if (emp == null) {
System.out.println("转发到:"+request.getContextPath()+DEFAULT);
response.sendRedirect(request.getContextPath()+DEFAULT);
return false;
}
return true;
}
}