Spring Boot crawl Pit Journey--mywebappconfigurer (Apply context configuration) (3)

Source: Internet
Author: User

Apply Context Configuration:

package com.zm.blog.config;
Import org.springframework.context.annotation.Configuration;
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 Com.zm.blog.config.interceptor.Interceptor; @Configuration public class Mywebappconfigurer extends Webmvcconfigureradapter {@Override public void addinterceptors (I Nterceptorregistry registry) {//Multiple interceptors make up an interceptor chain//Addpathpatterns for adding interception rules//Excludepathpatterns User exclusion interception registry
		. Addinterceptor (New Interceptor ()). Addpathpatterns ("/**");
	Super.addinterceptors (registry); }//Static resource mapping @Override public void Addresourcehandlers (Resourcehandlerregistry registry) {REGISTRY.ADDRESOURC
        Ehandler ("/**"). Addresourcelocations ("classpath:/uib/hui/");
    Super.addresourcehandlers (registry); }
}

1. Registering a custom Interceptor

Package com.zm.blog.config.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 Interceptor implements Handlerinterceptor {@Override public void aftercompletion (HttpServletRequest reque St, httpservletresponse response, Object obj, Exception ex) throws Exception {} @Override public void Posthandle (HTT Pservletrequest request, HttpServletResponse response, Object obj, Modelandview mv) throws Exception {Long startTime =
		(Long) Request.getattribute ("StartTime");
		Request.removeattribute ("StartTime");
		Long endTime = System.currenttimemillis ();
		String Uri=request.getrequesturi ();
	System.out.println ("+uri+" Request processing time is: "+ new Long (Endtime-starttime) +" MS "); } @Override public Boolean prehandle (HttpServletRequest request, httpservletresponse response, Object obj) throws Excep tion {LongStartTime = System.currenttimemillis ();
		Request.setattribute ("StartTime", startTime);
	return true;
 }

}

2. Registering Static resources

@Override public
    void Addresourcehandlers (Resourcehandlerregistry registry) {
        Registry.addresourcehandler ( "/**"). Addresourcelocations ("classpath:/uib/hui/");
        Super.addresourcehandlers (registry);
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.