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 "> - + + + + + + \ No newline at end of file diff --git a/springmvc-01-523/src/main/webapp/WEB-INF/jsp/index.jsp b/springmvc-01-523/src/main/webapp/WEB-INF/jsp/index.jsp index 4687cfc..8f239d0 100644 --- a/springmvc-01-523/src/main/webapp/WEB-INF/jsp/index.jsp +++ b/springmvc-01-523/src/main/webapp/WEB-INF/jsp/index.jsp @@ -13,5 +13,6 @@ <%--el表达式--%> ${userName} +

>>>>这里是index.jsp<<<<

diff --git a/springmvc-01-523/src/main/webapp/WEB-INF/jsp/show.jsp b/springmvc-01-523/src/main/webapp/WEB-INF/jsp/show.jsp new file mode 100644 index 0000000..3bf1dc8 --- /dev/null +++ b/springmvc-01-523/src/main/webapp/WEB-INF/jsp/show.jsp @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + User: zyx + Date: 2023/5/23 + Time: 14:25 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + ${address} + + diff --git a/springmvc-01-523/src/main/webapp/WEB-INF/web.xml b/springmvc-01-523/src/main/webapp/WEB-INF/web.xml index 189476f..9f05fcd 100644 --- a/springmvc-01-523/src/main/webapp/WEB-INF/web.xml +++ b/springmvc-01-523/src/main/webapp/WEB-INF/web.xml @@ -23,4 +23,17 @@ / + + encodingFilter + org.springframework.web.filter.CharacterEncodingFilter + + encoding + utf-8 + + + + encodingFilter + /* + + \ No newline at end of file