@Configuration public class MyWebMvcConfig implements WebMvcConfigurer { //注册了新的访问路径 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/mytestfilder/**").addResourceLocations("classpath:/mytestfilder/"); } @Override public void addViewControllers(ViewControllerRegistry registry) { //注册重定向 registry.addRedirectViewController("访问路径","页面路径"); //请求转发 registry.addViewController("访问路径").setViewName("/index"); } //注册格式化 @Override public void addFormatters(FormatterRegistry registry) { registry.addFormatter(new Formatter<Date>() { @Override public String print(Date date, Locale locale) { return null; } /* * s 为页面上传过来的转换的时间 * */ @Override public Date parse(String s, Locale locale) throws ParseException { System.out.println(s); SimpleDateFormat slf = new SimpleDateFormat("yyyy-MM-dd"); return slf.parse(s); } }); } //注册拦截器 @Override public void addInterceptors(InterceptorRegistry registry) { //创建拦截器对象 MyHandlerInterceptor mhi = new MyHandlerInterceptor(); //注册 List<String> patterm = new ArrayList<>(); patterm.add("/**"); List<String> exq = new ArrayList<>(); exq.add("/user/login.do"); exq.add("/resources/**"); registry.addInterceptor(mhi).addPathPatterns(patterm).excludePathPatterns(exq); } }
//创建了拦截器 搭配mywebmvc...class中的使用 @Configuration public class MyHandlerInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("pre"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("post"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("after"); } }
原文地址:https://www.cnblogs.com/xiaozhang666/p/12034316.html
时间: 2024-11-08 23:40:26