wuheng %!s(int64=2) %!d(string=hai) anos
pai
achega
f2d81c5d4b

+ 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 - 28
day04/src/main/java/com/lovecoding/mvc/Application.java

@@ -12,31 +12,28 @@ 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 {
-
-        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("/");
-
-    }
-
-}
+//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("/");
+//
+//    }
+//
+//}

+ 3 - 2
day04/src/main/java/com/lovecoding/mvc/MybatisTestController.java

@@ -19,11 +19,12 @@ public class MybatisTestController {
 
     @GetMapping("/mybatis")
     @ResponseBody
-    public String test(){
+    public Book test(){
 
         Book book = bookService.get(1L);
         System.out.println( book );
-        return "OK";
+        return book;
+
     }
 
 }

+ 19 - 1
day04/src/main/java/com/lovecoding/mvc/TestController.java

@@ -1,15 +1,33 @@
 package com.lovecoding.mvc;
 
 import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.filter.HttpPutFormContentFilter;
+import org.springframework.web.filter.reactive.HiddenHttpMethodFilter;
+import org.springframework.web.servlet.ModelAndView;
 
 @Controller
 public class TestController {
 
-
     @GetMapping("/test")
     public String test(){
+        int n = 1 / 0;
+        //Long i = null;
+        //System.out.println( i.longValue() );
+
         return "/test.jsp";
     }
 
+//    @ExceptionHandler({
+//            ArithmeticException.class
+//    })
+//    public ModelAndView err( Exception e ){
+//        ModelAndView modelAndView = new ModelAndView();
+//        modelAndView.addObject("ex", e.getMessage());
+//        modelAndView.setViewName("/anno.jsp");
+//        return modelAndView;
+//    }
+
+
 }

+ 20 - 0
day04/src/main/java/com/lovecoding/mvc/config/MyException.java

@@ -0,0 +1,20 @@
+package com.lovecoding.mvc.config;
+
+import org.springframework.web.servlet.HandlerExceptionResolver;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class MyException implements HandlerExceptionResolver {
+
+    @Override
+    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
+
+        ModelAndView modelAndView = new ModelAndView();
+        modelAndView.setViewName("/ex.jsp");
+        modelAndView.addObject( "ex", ex.getMessage() );
+
+        return modelAndView;
+    }
+}

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

@@ -1,10 +1,9 @@
 package com.lovecoding.mvc.config;
 
-import org.springframework.context.annotation.ComponentScan;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.Import;
-import org.springframework.context.annotation.PropertySource;
+import org.springframework.context.annotation.*;
+import org.springframework.web.servlet.HandlerExceptionResolver;
 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
 
 @Configuration
 @Import( {JdbcConfig.class, MybatisConfig.class}  )
@@ -13,6 +12,18 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 public class SpringConfig {
 
 
+//    @Bean
+//    public HandlerExceptionResolver getMyException(){
+//        MyException myException = new MyException();
+//        return myException;
+//    }
+
+//    @Bean
+//    public HandlerExceptionResolver getSimpleMappingExceptionResolver(){
+//        SimpleMappingExceptionResolver ex = new SimpleMappingExceptionResolver();
+//        ex.setDefaultErrorView("/ari.jsp");
+//        return ex;
+//    }
 
 
 }

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

@@ -7,6 +7,16 @@
 
     <context:component-scan base-package="com.lovecoding.mvc" />
 
+    <bean class="com.lovecoding.mvc.config.MyException" />
+
+<!--    <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">-->
+<!--        <property name="exceptionMappings">-->
+<!--            <props>-->
+<!--                <prop key="java.lang.ArithmeticException">/ari.jsp</prop>-->
+<!--            </props>-->
+<!--        </property>-->
+<!--    </bean>-->
+
     <mvc:interceptors>
         <mvc:interceptor>
             <mvc:mapping path="/mybatis"/>

+ 17 - 0
day04/src/main/webapp/anno.jsp

@@ -0,0 +1,17 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: lc
+  Date: 2023-04-02
+  Time: 上午 10:55
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<html>
+<head>
+    <title>Title</title>
+</head>
+<body>
+<h1>注解处理异常信息</h1>
+<h3>${ex}</h3>
+</body>
+</html>

+ 22 - 0
day04/src/main/webapp/ari.jsp

@@ -0,0 +1,22 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: lc
+  Date: 2023-04-02
+  Time: 上午 10:39
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<html>
+<head>
+    <title>Title</title>
+</head>
+<body>
+
+        <h1>系统发生运算异常!!!</h1>
+        <h3>${exception}</h3>
+        <%
+            System.out.println( request.getAttribute("exception") );
+        %>
+
+</body>
+</html>

+ 17 - 0
day04/src/main/webapp/ex.jsp

@@ -0,0 +1,17 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: lc
+  Date: 2023-04-02
+  Time: 上午 10:51
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<html>
+<head>
+    <title>Title</title>
+</head>
+<body>
+    <h1> 自定义的异常处理页面 </h1>
+    <h3>${ex}</h3>
+</body>
+</html>

+ 7 - 0
pom.xml

@@ -93,6 +93,13 @@
             <version>2.4</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+            <version>2.13.3</version>
+        </dependency>
+
+
     </dependencies>
 
 </project>