wuheng 2 年之前
父節點
當前提交
dce48fb22b

+ 16 - 16
day04/src/main/java/com/lovecoding/mvc/AnnoApplicataion.java

@@ -4,19 +4,19 @@ import com.lovecoding.mvc.config.SpringConfig;
 import com.lovecoding.mvc.config.SpringMvcConfig;
 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
 
-public class AnnoApplicataion extends AbstractAnnotationConfigDispatcherServletInitializer {
-    @Override
-    protected Class<?>[] getRootConfigClasses() {
-        return new Class[]{SpringConfig.class};
-    }
-
-    @Override
-    protected Class<?>[] getServletConfigClasses() {
-        return new Class[]{SpringMvcConfig.class};
-    }
-
-    @Override
-    protected String[] getServletMappings() {
-        return new String[]{"/"};
-    }
-}
+//public class AnnoApplicataion extends AbstractAnnotationConfigDispatcherServletInitializer {
+//    @Override
+//    protected Class<?>[] getRootConfigClasses() {
+//        return new Class[]{SpringConfig.class};
+//    }
+//
+//    @Override
+//    protected Class<?>[] getServletConfigClasses() {
+//        return new Class[]{SpringMvcConfig.class};
+//    }
+//
+//    @Override
+//    protected String[] getServletMappings() {
+//        return new String[]{"/"};
+//    }
+//}

+ 25 - 26
day04/src/main/java/com/lovecoding/mvc/Application.java

@@ -12,32 +12,31 @@ import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRegistration;
 
-//public class Application implements WebApplicationInitializer {
-//
-//    @Override
-//    public void onStartup(ServletContext servletContext) throws ServletException {
-//        //System.out.println( "我被调用了!!!" );
-//
-////        XmlWebApplicationContext applicationContext = new XmlWebApplicationContext();
-////        applicationContext.setConfigLocation("classpath:spring.xml");
-//
-//
-//
+public class Application implements WebApplicationInitializer {
+
+    @Override
+    public void onStartup(ServletContext servletContext) throws ServletException {
+
+        XmlWebApplicationContext applicationContext = new XmlWebApplicationContext();
+        applicationContext.setConfigLocation("classpath:spring.xml");
+
+
+
 //        AnnotationConfigWebApplicationContext applicationContext =
 //                new AnnotationConfigWebApplicationContext();
 //        applicationContext.register( SpringConfig.class );
-//
-//        //配置 MVC 配置档
-//        //配置 Mybatis配置档
-//
-//        DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
-//
-//        ServletRegistration.Dynamic dispatcher =
-//                servletContext.addServlet("dispatcher", dispatcherServlet);
-//
-//        dispatcher.setLoadOnStartup(1);
-//        dispatcher.addMapping("/");
-//
-//    }
-//
-//}
+
+        //配置 MVC 配置档
+        //配置 Mybatis配置档
+
+        DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
+
+        ServletRegistration.Dynamic dispatcher =
+                servletContext.addServlet("dispatcher", dispatcherServlet);
+
+        dispatcher.setLoadOnStartup(1);
+        dispatcher.addMapping("/");
+
+    }
+
+}

+ 53 - 0
day04/src/main/java/com/lovecoding/mvc/config/MyInterceptor.java

@@ -0,0 +1,53 @@
+package com.lovecoding.mvc.config;
+
+import org.springframework.lang.Nullable;
+import org.springframework.web.servlet.HandlerInterceptor;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class MyInterceptor implements HandlerInterceptor {
+
+    /**
+     * preHandle 是我们调用 控制器 函数之前 被执行
+     * @param request
+     * @param response
+     * @param handler
+     * @return
+     * @throws Exception
+     */
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
+            throws Exception {
+        System.out.println( "=========》preHandle 被调用" );
+        return true;
+    }
+
+    /**
+     * postHandle 是在 控制器执行完毕, 页面渲染之前 被执行
+     * @param request
+     * @param response
+     * @param handler
+     * @param modelAndView
+     * @throws Exception
+     */
+    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
+                            @Nullable ModelAndView modelAndView) throws Exception {
+
+        System.out.println( "=========》postHandle 被调用" );
+    }
+
+    /**
+     * afterCompletion 是视图渲染完成被执行
+     * @param request
+     * @param response
+     * @param handler
+     * @param ex
+     * @throws Exception
+     */
+    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
+                                 @Nullable Exception ex) throws Exception {
+        System.out.println( "=========》afterCompletion 被调用" );
+    }
+
+}

+ 4 - 0
day04/src/main/java/com/lovecoding/mvc/config/SpringConfig.java

@@ -11,4 +11,8 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 @ComponentScan("com.lovecoding.mvc")
 @PropertySource("classpath:db.properties")
 public class SpringConfig {
+
+
+
+
 }

+ 16 - 1
day04/src/main/java/com/lovecoding/mvc/config/SpringMvcConfig.java

@@ -3,9 +3,24 @@ package com.lovecoding.mvc.config;
 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.WebMvcConfigurer;
 
 @Configuration
 @ComponentScan("com.lovecoding.mvc")
 @EnableWebMvc
-public class SpringMvcConfig {
+public class SpringMvcConfig implements WebMvcConfigurer {
+
+//    public void addInterceptors(InterceptorRegistry registry) {
+//
+//        //如此配置 全局可用 当前框架所有MVC请求都被 拦截器过滤了
+//        //registry.addInterceptor( new MyInterceptor() );
+//
+//        //拦截指定URL 请求, 并排除其他URL路径
+//        registry.addInterceptor( new MyInterceptor() )
+//                .addPathPatterns("/mybatis")
+//                .excludePathPatterns("/login");
+//
+//    }
+
 }

+ 10 - 1
day04/src/main/resources/spring.xml

@@ -2,8 +2,17 @@
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
-       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
+       xmlns:mvc="http://www.springframework.org/schema/mvc"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
 
     <context:component-scan base-package="com.lovecoding.mvc" />
 
+    <mvc:interceptors>
+        <mvc:interceptor>
+            <mvc:mapping path="/mybatis"/>
+            <mvc:exclude-mapping path="/test"/>
+            <bean class="com.lovecoding.mvc.config.MyInterceptor" />
+        </mvc:interceptor>
+    </mvc:interceptors>
+
 </beans>