From 3e2ee2669b1703774e2bdf19c0335c30d794497e Mon Sep 17 00:00:00 2001
From: zyx <1029606625@qq.com>
Date: Tue, 23 May 2023 16:34:29 +0800
Subject: [PATCH] 5/23 16:34
---
.../mvc/controller/DemoController.java | 18 +++++++++
.../czyx007/mvc/controller/MyController.java | 2 +-
.../mvc/controller/UserController.java | 39 ++++++++++++++++++-
.../src/main/resources/springmvc.xml | 7 +++-
.../src/main/webapp/WEB-INF/jsp/index.jsp | 1 +
.../src/main/webapp/WEB-INF/jsp/show.jsp | 16 ++++++++
.../src/main/webapp/WEB-INF/web.xml | 13 +++++++
7 files changed, 93 insertions(+), 3 deletions(-)
create mode 100644 springmvc-01-523/src/main/java/cn/czyx007/mvc/controller/DemoController.java
create mode 100644 springmvc-01-523/src/main/webapp/WEB-INF/jsp/show.jsp
diff --git a/springmvc-01-523/src/main/java/cn/czyx007/mvc/controller/DemoController.java b/springmvc-01-523/src/main/java/cn/czyx007/mvc/controller/DemoController.java
new file mode 100644
index 0000000..46c6b23
--- /dev/null
+++ b/springmvc-01-523/src/main/java/cn/czyx007/mvc/controller/DemoController.java
@@ -0,0 +1,18 @@
+package cn.czyx007.mvc.controller;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2023/5/23 - 15:18
+ */
+@Controller
+@RequestMapping("/demo")
+public class DemoController {
+ @GetMapping("/getUser")
+ public String getUser(){
+ return "show";
+ }
+}
diff --git a/springmvc-01-523/src/main/java/cn/czyx007/mvc/controller/MyController.java b/springmvc-01-523/src/main/java/cn/czyx007/mvc/controller/MyController.java
index dcf073c..7181010 100644
--- a/springmvc-01-523/src/main/java/cn/czyx007/mvc/controller/MyController.java
+++ b/springmvc-01-523/src/main/java/cn/czyx007/mvc/controller/MyController.java
@@ -23,7 +23,7 @@ public class MyController implements Controller {
//1.封装数据
modelAndView.addObject("userName", "abc");
//2.封装视图
- modelAndView.setViewName("WEB-INF/jsp/index.jsp");
+ modelAndView.setViewName("index");
return modelAndView;
}
}
diff --git a/springmvc-01-523/src/main/java/cn/czyx007/mvc/controller/UserController.java b/springmvc-01-523/src/main/java/cn/czyx007/mvc/controller/UserController.java
index 67b41a6..b325046 100644
--- a/springmvc-01-523/src/main/java/cn/czyx007/mvc/controller/UserController.java
+++ b/springmvc-01-523/src/main/java/cn/czyx007/mvc/controller/UserController.java
@@ -1,7 +1,11 @@
package cn.czyx007.mvc.controller;
import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
/**
@@ -9,6 +13,7 @@ import org.springframework.web.servlet.ModelAndView;
* @createTime : 2023/5/23 - 11:40
*/
@Controller
+@RequestMapping("/user")
public class UserController {
@RequestMapping("/user")
public ModelAndView showUser(){
@@ -22,7 +27,39 @@ public class UserController {
//1.封装数据
modelAndView.addObject("userName", "blue");
//2.封装视图
- modelAndView.setViewName("WEB-INF/jsp/index.jsp");
+ modelAndView.setViewName("index");
return modelAndView;
}
+
+// @RequestMapping("/getUser")
+ @GetMapping({"/getUser", "/findUser", "/user01"})
+ public String getUser(Model model){
+ model.addAttribute("address", "北京");
+ return "show";
+ }
+
+ @GetMapping("/show")
+ public String show(
+ @RequestParam(value = "user", required = false, defaultValue = "liuyan") String userName,
+ @RequestParam(value = "age", required = false, defaultValue = "20") Integer age,
+ @RequestParam(value = "address", required = true) String address
+ ){
+ System.out.println("userName = " + userName);
+ System.out.println("age = " + age);
+ System.out.println("address = " + address);
+ return "index";
+ }
+
+ @PostMapping ("/showPost")
+ public String showPost(
+ @RequestParam(value = "user", required = false, defaultValue = "liuyan") String userName,
+ @RequestParam(value = "age", required = false, defaultValue = "20") Integer age,
+ @RequestParam(value = "address", required = true) String address
+ ){
+ System.out.println("userName = " + userName);
+ System.out.println("age = " + age);
+ System.out.println("address = " + address);
+ return "index";
+ }
+
}
diff --git a/springmvc-01-523/src/main/resources/springmvc.xml b/springmvc-01-523/src/main/resources/springmvc.xml
index 0855495..d4fe451 100644
--- a/springmvc-01-523/src/main/resources/springmvc.xml
+++ b/springmvc-01-523/src/main/resources/springmvc.xml
@@ -11,9 +11,14 @@
http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
-
+