wuheng 2 ani în urmă
părinte
comite
2ce09c3b3f

+ 19 - 0
day02/pom.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>SpringMvc</artifactId>
+        <groupId>org.example</groupId>
+        <version>1.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>day02</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+
+</project>

+ 47 - 0
day02/src/main/java/com/lovecoding/mvc/RequestController.java

@@ -0,0 +1,47 @@
+package com.lovecoding.mvc;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+@Controller
+public class RequestController {
+
+    /**
+     * 如果我们使用 ModelAndView 作为共享数据
+     * 我们控制器 返回类型必须是  ModelAndView
+     * ModelAndView 视图必须填写正确
+     * @param req
+     * @return
+     */
+    @GetMapping("/requestDemo")
+    public ModelAndView requestDemo(HttpServletRequest req){
+        ModelAndView modelAndView = new ModelAndView();
+        modelAndView.setViewName("/requestDemo.jsp");
+        modelAndView.addObject("name", req.getParameter("name"));
+        return modelAndView;
+    }
+
+    /**
+     * 演示Session 共享数据
+     * Session 数据共享
+     * Session 生命周期 特别长
+     */
+    @GetMapping("/sessionDemo")
+    public String sessionDemo(HttpSession httpSession, String name){
+        httpSession.setAttribute("name", name);
+        return "/sessionDemo.jsp";
+    }
+
+    @GetMapping("/modelAndViewDemo")
+    public ModelAndView modelAndViewDemo( ModelAndView modelAndView ){
+        modelAndView.addObject("msg",
+                "我是ModelAndView Bean对象");
+        modelAndView.setViewName("/modelAndViewDemo.jsp");
+        return modelAndView;
+    }
+
+}

+ 12 - 0
day02/src/main/resources/spring-mvc.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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">
+
+    <!--  开启扫包  -->
+    <context:component-scan base-package="com.lovecoding.mvc" ></context:component-scan>
+
+
+
+</beans>

+ 20 - 0
day02/src/main/webapp/WEB-INF/web.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
+         version="4.0">
+
+    <servlet>
+        <servlet-name>springDispatcherServlet</servlet-name>
+        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
+        <init-param>
+            <param-name>contextConfigLocation</param-name>
+            <param-value>classpath:spring-mvc.xml</param-value>
+        </init-param>
+    </servlet>
+    <servlet-mapping>
+        <servlet-name>springDispatcherServlet</servlet-name>
+        <url-pattern>/</url-pattern>
+    </servlet-mapping>
+
+</web-app>

+ 24 - 0
day02/src/main/webapp/index.jsp

@@ -0,0 +1,24 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: lc
+  Date: 2023-03-26
+  Time: 上午 9:23
+  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>Day02</h1>
+
+<a href="<%=request.getContextPath()%>/requestDemo?name=张三"> 点击我跳转 requestDemo 页面 </a>
+<hr />
+<a href="<%=request.getContextPath()%>/sessionDemo?name=李四"> 点击我跳转 sessionDemo 页面 </a>
+<hr />
+<a href="<%=request.getContextPath()%>/modelAndViewDemo"> 点击我跳转 modelAndView 页面 </a>
+
+
+</body>
+</html>

+ 22 - 0
day02/src/main/webapp/modelAndViewDemo.jsp

@@ -0,0 +1,22 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: lc
+  Date: 2023-03-26
+  Time: 上午 10:04
+  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>
+  <%
+    out.print(
+            request.getAttribute("msg")
+    );
+  %>
+</h1>
+</body>
+</html>

+ 34 - 0
day02/src/main/webapp/requestDemo.jsp

@@ -0,0 +1,34 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: lc
+  Date: 2023-03-26
+  Time: 上午 9:32
+  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>
+<%
+
+        out.print(
+
+                session.getAttribute("name")
+
+        );
+
+        out.print(
+
+                request.getAttribute("name")
+
+        );
+
+%>
+
+    </h1>
+</body>
+</html>

+ 23 - 0
day02/src/main/webapp/sessionDemo.jsp

@@ -0,0 +1,23 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: lc
+  Date: 2023-03-26
+  Time: 上午 9:54
+  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>
+  <%
+
+
+      out.print(
+              session.getAttribute("name")
+      );
+
+  %>
+</body>
+</html>

+ 1 - 0
pom.xml

@@ -10,6 +10,7 @@
     <version>1.0-SNAPSHOT</version>
     <modules>
         <module>day01</module>
+        <module>day02</module>
     </modules>
 
     <properties>