SpringMvc 拦截器的使用

1.目录工程



2.配置拦截器:

<!-- 以下配置将拦截特有的URL请求  拦截user下面所有的路劲-->
	<mvc:interceptors>
    	<mvc:interceptor>
    		<mvc:mapping path="/user/**"/>
    		<bean class="com.qunar.interceptor.MyInterceptor" />
    	</mvc:interceptor>
    </mvc:interceptors> 

3.拦截器实现类

package com.qunar.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MyInterceptor implements HandlerInterceptor {

	/**
	 * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。该方法将在整个请求完成之后,
	 * 也就是DispatcherServlet渲染了视图执行,
	 * 这个方法的主要作用是用于清理资源的,当然这个方法也只能在当前这个Interceptor的preHandle方法的返回值为true时才会执行。
	 */
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("===========HandlerInterceptor1 afterCompletion");
	}

	/**
	 * 
	 * 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。postHandle是进行处理器拦截用的,
	 * 它的执行时间是在处理器进行处理之
	 * 后,也就是在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行
	 * ,也就是说在这个方法中你可以对ModelAndView进行操
	 * 作。这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor拦截器该方法反而会后调用,
	 */
	public void postHandle(HttpServletRequest request,
			HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println("===========HandlerInterceptor1 postHandle");

	}

	/**
	 * 
	 * preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用,
	 * SpringMVC中的Interceptor拦截器是链式的,可以同时存在
	 * 多个Interceptor,然后SpringMVC会根据声明的前后顺序一个接一个的执行
	 * ,而且所有的Interceptor中的preHandle方法都会在
	 * Controller方法调用之前调用。SpringMVC的这种Interceptor链式结构也是可以进行中断的
	 * ,这种中断方式是令preHandle的返 回值为false,当preHandle的返回值为false的时候整个请求就结束了。
	 * 
	 */
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object arg2) throws Exception {
		System.out.println("===========HandlerInterceptor1 preHandle");
		System.out.println("******************"
				+ request.getSession().getAttribute("password"));
		if (request.getSession().getAttribute("password") != null) {
			return true;
		} else {
			response.sendRedirect(request.getContextPath() + "/index.jsp");
			return false;
		}

	}

}

LoginController.java

package com.qunar.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/login")
public class LoginController {
	@RequestMapping(value="/",method=RequestMethod.GET)
	public String login(){
		return "login/login";
	}
	@RequestMapping(value="/",method=RequestMethod.POST)
	public String login(HttpServletRequest request,String username,String password){
		if("suhao".equals(username)&&"123".equals(password)){
			System.out.println("loing success");
			HttpSession session = request.getSession();
			session.setAttribute("password", password);
		}
		return "login/welcome";
	}
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request){
		request.getSession().invalidate();
		return "redirect:/index.jsp";
	}
}

UserController.java

package com.qunar.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qunar.dao.IUserDAO;
import com.qunar.vo.User;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private IUserDAO userMapper;

	@RequestMapping("/")
	public String index() {
		return "login";
	}
	
	@RequestMapping(value="/json",method=RequestMethod.GET)
	@ResponseBody
	public List<User> getUserJson(){
		List<User> users = null;
		try {
			users = userMapper. selectUsers();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return users;
	}
}

weclome.jsp

<body>
	welcome to my blog 
	<br/>
	<a href="logout">注销</a>
	<a href="../user/">用户</a>
</body>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值