SpringBoot实战学习(三) SpringMVC基本配置

1.目录结构

SpringBoot实战学习(三) SpringMVC基本配置_第1张图片

2.编写

1.拦截器配置

package com.wen.springmvc4.interceptor;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 拦截器配置
 * @author EGWri
 *
 */
public class DemoInterceptor extends HandlerInterceptorAdapter{//继承HandlerInterceptorAdapter类实现自定义拦截器

    //重写preHandle方法,在请求发生前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        long startTime=System.currentTimeMillis();
        request.setAttribute("startTime", startTime);
        System.out.println("preHandle");
        return true;
    }

    //重写postHandle方法,在请求发生后执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        long startTime=(long) request.getAttribute("startTime");
        request.removeAttribute("startTime");
        long endTime=System.currentTimeMillis();
        System.out.println("本次请求处理时间为时间:"+new Long(endTime-startTime)+"ms");
        request.setAttribute("handlingTime", endTime-startTime);
    }

}

2.添加静态资源
在src/main/resource下建立assests/js目录。弄一个jquery.js进去

3.配置

package com.wen.springmvc4;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.wen.springmvc4.interceptor.DemoInterceptor;



/**
 * Spring MVC配置
 * @author EGWri
 *
 */
@Configuration//声明当前类是一个配置类
@EnableWebMvc//开启默认配置,开启SpringMVC支持(无此句重写WebMvcConfigurerAdapter方法无效)
@ComponentScan("com.wen.springmvc4")//自动扫描包名下所有使用@Service、@Component、@Repository和@Controller的类,并注册为Bean
//继承WebMvcConfigurerAdapter类,重写其方法可对SpringMVC进行配置
public class MyMvcConfig extends WebMvcConfigurerAdapter{

    @Bean
    public InternalResourceViewResolver viewResolver(){
        //SpringMVC渲染核心,映射路径和实际页面的位置
        InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/classes/views/");//表示路径前缀(运行时目录结构)
        viewResolver.setSuffix(".jsp");//表示路径后缀,假设 ViewName 为 hello,则完整路径为 /WEB-INF/page/hello.jsp
        viewResolver.setViewClass(JstlView.class);//viewClass: 表示要解析的视图类型
        return viewResolver;
        }

        /**静态目录**/
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
        }   //addResourceHandler对外暴露的路径,addResourceLocations文件放置的目录
        /**静态目录**/

        /**拦截器  **/
        @Bean//配置拦截器的Bean
        public DemoInterceptor demoInterceptor(){
            return new DemoInterceptor();
        }

        @Override//重写addInterceptors方法,注册拦截器
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(demoInterceptor());
        }
        /**拦截器  **/
}

3.运行

将程序部署到Tomcat,运行,访问http://localhost:8080/SpringMVC/index ,看控制台打印消息
SpringBoot实战学习(三) SpringMVC基本配置_第2张图片
可知请求发生前执行preHandle方法,完成后执行postHndle方法

你可能感兴趣的:(SpringBoot)