Parcourir la source

添加了 WebMvcConfigurer 但是不行?爲什麼

zxy il y a 2 ans
Parent
commit
3073c67a73

+ 37 - 0
springMvc/src/main/java/com/config/WebMvcConfig.java

@@ -0,0 +1,37 @@
1
+package com.config;
2
+
3
+import com.converter.DateConverter;
4
+import com.interceptor.MyInterceptor;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.context.annotation.Configuration;
7
+import org.springframework.core.convert.converter.Converter;
8
+import org.springframework.format.FormatterRegistry;
9
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
10
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
11
+
12
+import java.text.ParseException;
13
+import java.text.SimpleDateFormat;
14
+import java.util.Date;
15
+
16
+@Configuration
17
+public class WebMvcConfig implements WebMvcConfigurer {
18
+    @Override
19
+    public void addFormatters(FormatterRegistry registry) {
20
+        registry.addConverter(new Converter<String, Date>() {
21
+            public Date convert(String s) {
22
+                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
23
+                Date date = null;
24
+                try {
25
+                    date = simpleDateFormat.parse(s);
26
+                } catch (ParseException e) {
27
+                    e.printStackTrace();
28
+                }
29
+                return date;
30
+            }
31
+        });
32
+    }
33
+    @Override
34
+    public void addInterceptors(InterceptorRegistry registry) {
35
+//        WebMvcConfigurer.super.addInterceptors(registry);
36
+    }
37
+}

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

@@ -1,13 +1,17 @@
1 1
 package com.controller;
2 2
 
3
+import com.interceptor.MyInterceptor;
4
+import com.listener.WebApplicationContextUtils;
3 5
 import com.pojo.Student;
4 6
 import org.aopalliance.intercept.Interceptor;
5 7
 import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.context.ApplicationContext;
6 9
 import org.springframework.stereotype.Controller;
7 10
 import org.springframework.web.bind.annotation.RequestMapping;
8 11
 import org.springframework.web.bind.annotation.ResponseBody;
9 12
 import org.springframework.web.servlet.ModelAndView;
10 13
 
14
+import javax.servlet.ServletContext;
11 15
 import javax.servlet.http.HttpServletRequest;
12 16
 import javax.servlet.http.HttpServletResponse;
13 17
 import java.io.IOException;
@@ -29,6 +33,7 @@ public class UserController {
29 33
     @RequestMapping("/save")
30 34
     public void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
31 35
         // 直接返回字符串
36
+        ServletContext servletContext = request.getServletContext();
32 37
         response.getWriter().println("save");
33 38
 
34 39
     }

+ 12 - 0
springMvc/src/main/java/testCom/SpringConfig2.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 SpringConfig2 {
8
+    @Bean
9
+    public String getS () {
10
+        return "s1";
11
+    }
12
+}

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

@@ -36,6 +36,7 @@
36 36
 <!--        </property>-->
37 37
 
38 38
 <!--    </bean>-->
39
+<!--            <mvc:annotation-driven></mvc:annotation-driven>-->
39 40
         <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
40 41
         <!--  开放静态资源的访问-->
41 42
         <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>