Browse Source

修改攔截器 intercept

zxy 2 years ago
parent
commit
a8992e8d07

+ 0 - 10
springMvc/src/main/java/com/config/SpringConfig.java

@@ -1,10 +0,0 @@
1
-package com.config;
2
-
3
-import org.springframework.context.annotation.ComponentScan;
4
-import org.springframework.context.annotation.Configuration;
5
-
6
-@Configuration
7
-@ComponentScan({"com"})
8
-public class SpringConfig {
9
-
10
-}

+ 5 - 0
springMvc/src/main/java/com/controller/UserController.java

@@ -1,5 +1,7 @@
1 1
 package com.controller;
2 2
 
3
+import org.aopalliance.intercept.Interceptor;
4
+import org.springframework.beans.factory.annotation.Autowired;
3 5
 import org.springframework.stereotype.Controller;
4 6
 import org.springframework.web.bind.annotation.RequestMapping;
5 7
 
@@ -9,6 +11,8 @@ import java.io.IOException;
9 11
 
10 12
 @Controller
11 13
 public class UserController {
14
+    @Autowired
15
+    String getS;
12 16
     @RequestMapping("/quick")
13 17
     public String quick() {
14 18
         System.out.println("userControllerRunning1...");
@@ -19,6 +23,7 @@ public class UserController {
19 23
     @RequestMapping("/save")
20 24
     public void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
21 25
         // 直接返回字符串
26
+        System.out.println("1213");
22 27
         response.getWriter().println("save");
23 28
 //        return "forward:success.jsp";
24 29
 

+ 17 - 0
springMvc/src/main/java/com/interceptor/InterceptorConfig.java

@@ -0,0 +1,17 @@
1
+package com.interceptor;
2
+
3
+import org.springframework.context.annotation.Bean;
4
+import org.springframework.context.annotation.Configuration;
5
+import org.springframework.web.servlet.handler.MappedInterceptor;
6
+
7
+@Configuration
8
+public class InterceptorConfig {
9
+    @Bean
10
+    public MappedInterceptor myInterceptor() {
11
+        return new MappedInterceptor(new String[]{"/**"},null,new MyInterceptor());
12
+    }
13
+    @Bean
14
+    public MappedInterceptor myInterceptor2() {
15
+        return new MappedInterceptor(new String[]{"/save"},null,new MyInterceptor());
16
+    }
17
+}

+ 20 - 0
springMvc/src/main/java/com/interceptor/MyInterceptor.java

@@ -0,0 +1,20 @@
1
+package com.interceptor;
2
+
3
+import org.springframework.stereotype.Component;
4
+import org.springframework.web.servlet.HandlerInterceptor;
5
+import org.springframework.web.servlet.ModelAndView;
6
+
7
+import javax.servlet.http.HttpServletRequest;
8
+import javax.servlet.http.HttpServletResponse;
9
+public class MyInterceptor implements HandlerInterceptor {
10
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
11
+        System.out.println("pre");
12
+        return true;
13
+    }
14
+    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
15
+        System.out.println("chulihou");
16
+    }
17
+    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
18
+        System.out.println("clean");
19
+    }
20
+}

+ 20 - 0
springMvc/src/main/java/com/interceptor/MyInterceptor2.java

@@ -0,0 +1,20 @@
1
+package com.interceptor;
2
+
3
+import org.springframework.stereotype.Component;
4
+import org.springframework.web.servlet.HandlerInterceptor;
5
+import org.springframework.web.servlet.ModelAndView;
6
+
7
+import javax.servlet.http.HttpServletRequest;
8
+import javax.servlet.http.HttpServletResponse;
9
+public class MyInterceptor2 implements HandlerInterceptor {
10
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
11
+        System.out.println("pre2");
12
+        return true;
13
+    }
14
+    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
15
+        System.out.println("chulihou2");
16
+    }
17
+    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
18
+        System.out.println("clean2");
19
+    }
20
+}

+ 0 - 2
springMvc/src/main/java/com/listener/ContextLoadListener.java

@@ -1,8 +1,6 @@
1 1
 package com.listener;
2 2
 
3
-import com.config.SpringConfig;
4 3
 import org.springframework.context.ApplicationContext;
5
-import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6 4
 import org.springframework.context.support.ClassPathXmlApplicationContext;
7 5
 
8 6
 import javax.servlet.ServletContext;

+ 3 - 2
springMvc/src/main/java/com/web/UserServlet.java

@@ -1,11 +1,10 @@
1 1
 package com.web;
2 2
 
3
-import com.config.SpringConfig;
4 3
 import com.service.UserService;
5
-import org.springframework.context.ApplicationContext;
6 4
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
7 5
 import org.springframework.web.context.WebApplicationContext;
8 6
 import org.springframework.web.context.support.WebApplicationContextUtils;
7
+import testCom.SpringConfig;
9 8
 
10 9
 import javax.servlet.ServletContext;
11 10
 import javax.servlet.ServletException;
@@ -23,6 +22,8 @@ public class UserServlet extends HttpServlet {
23 22
 //        ApplicationContext app = (ApplicationContext)servletContext.getAttribute("app");
24 23
 //        ApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
25 24
         WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
25
+        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
26
+        Object ssFilter = annotationConfigApplicationContext.getBean("ssFilter");
26 27
         UserService bean = webApplicationContext.getBean(UserService.class);
27 28
         bean.saveUser();
28 29
         // Hello

+ 12 - 0
springMvc/src/main/java/testCom/SpringConfig.java

@@ -0,0 +1,12 @@
1
+package testCom;
2
+
3
+import org.springframework.context.annotation.Bean;
4
+import org.springframework.context.annotation.Configuration;
5
+
6
+@Configuration
7
+public class SpringConfig {
8
+    @Bean
9
+    public String getS () {
10
+        return "s";
11
+    }
12
+}

+ 12 - 1
springMvc/src/main/resources/Spring-mvc.xml

@@ -3,7 +3,12 @@
3 3
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 4
        xmlns:context="http://www.springframework.org/schema/context"
5 5
        xmlns:mvc="http://www.springframework.org/schema/mvc"
6
-       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">
6
+       xsi:schemaLocation="http://www.springframework.org/schema/beans
7
+       http://www.springframework.org/schema/beans/spring-beans.xsd
8
+       http://www.springframework.org/schema/context
9
+       https://www.springframework.org/schema/context/spring-context.xsd
10
+       http://www.springframework.org/schema/mvc
11
+       http://www.springframework.org/schema/mvc/spring-mvc.xsd">
7 12
 <!--    controller组件扫描-->
8 13
 <!--    <context:component-scan base-package="com.controller"></context:component-scan>-->
9 14
 <!--    只扫描 com下的@Controller注解-->
@@ -17,4 +22,10 @@
17 22
 <!--        <property name="prefix" value="/template/"></property>-->
18 23
 <!--        <property name="suffix" value=".jsp"></property>-->
19 24
 <!--    </bean>-->
25
+<!--       <mvc:interceptors>-->
26
+<!--           <mvc:interceptor>-->
27
+<!--               <mvc:mapping path="/save"/>-->
28
+<!--               <bean class="com.interceptor.MyInterceptor2"></bean>-->
29
+<!--           </mvc:interceptor>-->
30
+<!--       </mvc:interceptors>-->
20 31
 </beans>

+ 1 - 0
springMvc/src/main/resources/context.xml

@@ -20,4 +20,5 @@
20 20
     <!--    </bean>-->
21 21
     <!--    组件扫描-->
22 22
     <context:component-scan base-package="com"></context:component-scan>
23
+    <context:component-scan base-package="testCom"></context:component-scan>
23 24
 </beans>

+ 1 - 0
springMvc/src/main/webapp/WEB-INF/web.xml

@@ -41,6 +41,7 @@
41 41
       <param-value>classpath:Spring-mvc.xml</param-value>
42 42
     </init-param>
43 43
     <load-on-startup>1</load-on-startup>
44
+    
44 45
   </servlet>
45 46
   <servlet-mapping>
46 47
     <servlet-name>dispatcherServlet</servlet-name>