SpringBoot拦截器和视图控制器

SpringBoot拦截器

1、定义拦截器(与SpringMVC的定义方式一致)

package com.mingde.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 {
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object arg2) throws Exception {
		System.out.println("MyInterceptor01--->preHandle()执行控制器之前调用此方法....");
		//返回true则放行,返回false则将其拦截住
		return true;
	}
	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse resp, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println("MyInterceptor01--->postHandle()执行控制器之后且在渲染视图前调用此方法....");
	}
	@Override
	public void afterCompletion(HttpServletRequest req, HttpServletResponse resp, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("MyInterceptor01--->afterCompletion()执行控制器之后且在完成渲染视图后调用此方法....");
	}
}

2、注册拦截器

package com.mingde.configurer;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.mingde.interceptor.MyInterceptor;

@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
	//重写添加拦截器的方法(添加调用的拦截器,并指定拦截的目标)
	@Override
	public void addInterceptors(InterceptorRegistry registry) { //注册拦截器
		//代表指定拦截所有路径的资源.并且排除掉xxx(不拦截xxx)
		//registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("xxx");
		//addPathPatterns("/**")代表指定拦截所有路径的资源
		registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
		super.addInterceptors(registry);
	}
}



SpringBoot添加视图控制器

在上面的类中补习addview方法即可
package com.mingde.configurer;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.mingde.interceptor.MyInterceptor;

@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
	//重写添加拦截器的方法(添加调用的拦截器,并指定拦截的目标)
	@Override
	public void addInterceptors(InterceptorRegistry registry) { //注册拦截器
		//代表指定拦截所有路径的资源.并且排除掉xxx(不拦截xxx)
		//registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("xxx");
		//addPathPatterns("/**")代表指定拦截所有路径的资源
		registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
		super.addInterceptors(registry);
	}
	
	//加载静态资源
	/*@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// TODO Auto-generated method stub
		super.addResourceHandlers(registry);
	}*/
	
	//重写添加视图控制器(不用写controller就可以跳转至指定的jsp,比如:http://localhost:90/tologin)
	//此时就跳转到/WEB-INF/jsp/list.jsp页面
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("tologin").setViewName("list");
		super.addViewControllers(registry);
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值