SpringMVC拦截器

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;

package com.kd.winery.Interceptor;


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;
}


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值