wuheng 2 years ago
parent
commit
51253d7314

+ 32 - 5
day01/src/main/java/com/lovecoding/springmvc/DemoContorller.java

@@ -2,19 +2,46 @@ package com.lovecoding.springmvc;
 
 
 import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.*;
 
 @Controller
+@RequestMapping(value = "/demoContorller")
 public class DemoContorller {
 
 
-    @RequestMapping(path = "/demo")
-    public String demo(){
+    @RequestMapping(
+            value = {  "/save/{id}" },
+            method = { RequestMethod.POST, RequestMethod.GET }
+            //params = {"username=zhangsan"},
+            //headers = {"User-Agent"},
+            //consumes = { "multipart/form-data" }, 用来限制 请求数据类型
+            //produces = { "text/html" }
+    )
+    public String demo( @RequestParam(value = "user", required = false) String username  ){
 
-        System.out.println( "DemoContorller Run!!!" );
+        System.out.println( "DemoContorller Run!!!" + username );
 
-        return "/index.jsp";
+        return "/demo.jsp";
     }
 
+    /**
+     * 这种通过 URL 路径传递参数的风格
+     * 一般叫做 RESTful 协议
+     * @param studentId
+     * @return
+     */
+    @RequestMapping(
+            value = {"/updateStudent/{studentId}" }
+    )
+    public String hello( @PathVariable(
+            value = "studentId",
+            required = false
+    ) int studentId ){
+
+        System.out.println( "studentId :" + studentId );
+
+        return "/demo.jsp";
+
+    }
 
 }

+ 86 - 0
day01/src/main/java/com/lovecoding/springmvc/StudentController.java

@@ -0,0 +1,86 @@
+package com.lovecoding.springmvc;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.HttpSession;
+
+@Controller
+@RequestMapping("/student")
+public class StudentController {
+
+    /**
+     * 新增操作
+     * @param studentId
+     * @return
+     */
+    @RequestMapping(
+            value ="/{studentId}",
+            method = RequestMethod.POST)
+    public String add(@PathVariable(
+            value = "studentId",
+            required = true) Long studentId, HttpSession httpSession){
+        System.out.println( "新增学员操作成功!!: " + studentId );
+        httpSession.setAttribute("action", "添加成功 ID:" + studentId);
+
+        return "/success.jsp";
+    }
+
+    /**
+     * 删除操作
+     * @param studentId
+     * @return
+     */
+    @RequestMapping(
+            value ="/{studentId}",
+            method = RequestMethod.DELETE)
+    @ResponseBody
+    public String del(@PathVariable(
+            value = "studentId",
+            required = true) Long studentId, HttpSession httpSession){
+        System.out.println( "删除学员操作成功!!: " + studentId );
+        httpSession.setAttribute("action", "删除成功 ID:" + studentId);
+
+        return "Delete Student Success!!: " + studentId;
+        //return "/success.jsp";
+    }
+
+    /**
+     * 修改操作
+     * @param studentId
+     * @return
+     */
+    @RequestMapping(
+            value ="/{studentId}",
+            method = RequestMethod.PUT)
+    @ResponseBody
+    public String update(@PathVariable(
+            value = "studentId",
+            required = true) Long studentId, HttpSession httpSession){
+        System.out.println( "修改学员操作成功!!: " + studentId );
+        httpSession.setAttribute("action", "修改成功 ID:" + studentId);
+        return "Update Student Success!!: " + studentId;
+        //return "/success.jsp";
+    }
+
+    /**
+     * 查询操作
+     * @param studentId
+     * @return
+     */
+    @RequestMapping(
+            value ="/{studentId}",
+            method = RequestMethod.GET)
+    public String get(@PathVariable(
+            value = "studentId",
+            required = true) Long studentId, HttpSession httpSession){
+        System.out.println( "查询学员操作成功!!: " + studentId );
+        httpSession.setAttribute("action", "查询成功 ID:" + studentId);
+
+        return "/success.jsp";
+    }
+
+}

BIN
day01/src/main/webapp/WEB-INF/lib/el-api.jar


BIN
day01/src/main/webapp/WEB-INF/lib/jsp-api.jar


BIN
day01/src/main/webapp/WEB-INF/lib/servlet-api.jar


+ 16 - 0
day01/src/main/webapp/demo.jsp

@@ -0,0 +1,16 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: lc
+  Date: 2023-03-25
+  Time: 上午 10:35
+  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>Demo-JSP</h1>
+</body>
+</html>

+ 21 - 0
day01/src/main/webapp/success.jsp

@@ -0,0 +1,21 @@
+<%@ page import="org.springframework.http.HttpRequest" %>
+<%@ page import="org.springframework.web.context.request.SessionScope" %>
+<%--
+  Created by IntelliJ IDEA.
+  User: lc
+  Date: 2023-03-25
+  Time: 下午 2: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> <%
+    String action = (String) session.getAttribute("action");
+    out.print( action );
+%> </h1>
+</body>
+</html>