Browse Source

引入 springMVC

zhang 2 years ago
parent
commit
ab2e78ba56

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

@@ -0,0 +1,13 @@
1
+package com.controller;
2
+
3
+import org.springframework.stereotype.Controller;
4
+import org.springframework.web.bind.annotation.RequestMapping;
5
+
6
+@Controller
7
+public class UserController {
8
+    @RequestMapping("/quick")
9
+    public String save() {
10
+        System.out.println("userControllerRunning1...");
11
+        return "success.jsp";
12
+    }
13
+}

+ 7 - 6
springMvc/src/main/java/com/listener/ContextLoadListener.java

@@ -1,25 +1,26 @@
1 1
 package com.listener;
2 2
 
3 3
 import com.config.SpringConfig;
4
+import org.springframework.context.ApplicationContext;
4 5
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6
+import org.springframework.context.support.ClassPathXmlApplicationContext;
5 7
 
6 8
 import javax.servlet.ServletContext;
7 9
 import javax.servlet.ServletContextEvent;
8 10
 import javax.servlet.ServletContextListener;
9 11
 
10 12
 public class ContextLoadListener implements ServletContextListener {
13
+    @Override
11 14
     public void contextInitialized(ServletContextEvent sce) {
12 15
 //        ServletContextListener.super.contextInitialized(sce);
13 16
         ServletContext servletContext = sce.getServletContext();
14 17
         // 获取web.xml中默认设置参数
15
-        String initParameter = servletContext.getInitParameter("testName");
16
-        System.out.println(initParameter);
17
-        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
18
-        servletContext.setAttribute("app",annotationConfigApplicationContext);
18
+        String contextPath = servletContext.getInitParameter("contextConfigLocation");
19
+        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(contextPath);
20
+        servletContext.setAttribute("app",applicationContext);
19 21
         System.out.println("servletContextLoader");
20 22
     }
21
-
23
+    @Override
22 24
     public void contextDestroyed(ServletContextEvent sce) {
23
-//        ServletContextListener.super.contextDestroyed(sce);
24 25
     }
25 26
 }

+ 11 - 0
springMvc/src/main/java/com/listener/WebApplicationContextUtils.java

@@ -0,0 +1,11 @@
1
+package com.listener;
2
+
3
+import org.springframework.context.ApplicationContext;
4
+
5
+import javax.servlet.ServletContext;
6
+
7
+public class WebApplicationContextUtils {
8
+    public static ApplicationContext getWebApplicationContext(ServletContext servletContext) {
9
+        return (ApplicationContext) servletContext.getAttribute("app");
10
+    }
11
+}

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

@@ -4,6 +4,8 @@ import com.config.SpringConfig;
4 4
 import com.service.UserService;
5 5
 import org.springframework.context.ApplicationContext;
6 6
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
7
+import org.springframework.web.context.WebApplicationContext;
8
+import org.springframework.web.context.support.WebApplicationContextUtils;
7 9
 
8 10
 import javax.servlet.ServletContext;
9 11
 import javax.servlet.ServletException;
@@ -18,8 +20,10 @@ public class UserServlet extends HttpServlet {
18 20
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
19 21
         //super.doGet(req, resp);
20 22
         ServletContext servletContext = req.getServletContext();
21
-        ApplicationContext app = (ApplicationContext)servletContext.getAttribute("app");
22
-        UserService bean = app.getBean(UserService.class);
23
+//        ApplicationContext app = (ApplicationContext)servletContext.getAttribute("app");
24
+//        ApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
25
+        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
26
+        UserService bean = webApplicationContext.getBean(UserService.class);
23 27
         bean.saveUser();
24 28
         // Hello
25 29
         PrintWriter out = resp.getWriter();

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

@@ -0,0 +1,8 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<beans xmlns="http://www.springframework.org/schema/beans"
3
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+       xmlns:context="http://www.springframework.org/schema/context"
5
+       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
+<!--    controller组件扫描-->
7
+    <context:component-scan base-package="com.controller"></context:component-scan>
8
+</beans>

+ 19 - 4
springMvc/src/main/webapp/WEB-INF/web.xml

@@ -26,11 +26,27 @@
26 26
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27 27
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
28 28
   <listener>
29
-    <listener-class>com.listener.ContextLoadListener</listener-class>
29
+<!--    <listener-class>com.listener.ContextLoadListener</listener-class>-->
30
+    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
30 31
   </listener>
32
+<!--  配置SpringMVC前端控制器-->
33
+  <servlet>
34
+    <servlet-name>dispatcherServlet</servlet-name>
35
+    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
36
+    <init-param>
37
+      <param-name>contextConfigLocation</param-name>
38
+      <param-value>classpath:Spring-mvc.xml</param-value>
39
+    </init-param>
40
+    <load-on-startup>1</load-on-startup>
41
+  </servlet>
42
+  <servlet-mapping>
43
+    <servlet-name>dispatcherServlet</servlet-name>
44
+    <url-pattern>/</url-pattern>
45
+  </servlet-mapping>
46
+<!--  全局初始化参数-->
31 47
   <context-param>
32
-    <param-name>testName</param-name>
33
-    <param-value>test1</param-value>
48
+    <param-name>contextConfigLocation</param-name>
49
+    <param-value>classpath:context.xml</param-value>
34 50
   </context-param>
35 51
 
36 52
   <!-- Servlet Filters ================================================ -->
@@ -43,6 +59,5 @@
43 59
     <servlet-name>User</servlet-name>
44 60
     <url-pattern>/user</url-pattern>
45 61
   </servlet-mapping>
46
-
47 62
 </web-app>
48 63
         

+ 16 - 0
springMvc/src/main/webapp/success.jsp

@@ -0,0 +1,16 @@
1
+<%--
2
+  Created by IntelliJ IDEA.
3
+  User: 77179
4
+  Date: 2022/5/11
5
+  Time: 21:06
6
+  To change this template use File | Settings | File Templates.
7
+--%>
8
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9
+<html>
10
+<head>
11
+    <title>Title</title>
12
+</head>
13
+<body>
14
+    <h1>success!!!</h1>
15
+</body>
16
+</html>