diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 5875f50..e4bf485 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -9,16 +9,23 @@ - - + + + + + + - - + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml index 9076776..3131fb6 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -21,7 +21,11 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index e289113..85083a6 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,6 +4,7 @@ + diff --git a/springboot-524/pom.xml b/springboot-524/pom.xml new file mode 100644 index 0000000..7382b80 --- /dev/null +++ b/springboot-524/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.8 + + + + com.example + springboot-524 + 0.0.1-SNAPSHOT + springboot-524 + springboot-524 + + + 11 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/springboot-524/src/main/java/cn/czyx007/Application.java b/springboot-524/src/main/java/cn/czyx007/Application.java new file mode 100644 index 0000000..81fcc2a --- /dev/null +++ b/springboot-524/src/main/java/cn/czyx007/Application.java @@ -0,0 +1,13 @@ +package cn.czyx007; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/springboot-524/src/main/java/cn/czyx007/controller/HelloController.java b/springboot-524/src/main/java/cn/czyx007/controller/HelloController.java new file mode 100644 index 0000000..4ce146b --- /dev/null +++ b/springboot-524/src/main/java/cn/czyx007/controller/HelloController.java @@ -0,0 +1,21 @@ +package cn.czyx007.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/24 - 15:47 + */ +@RestController +public class HelloController { + @GetMapping("/hello") + public String hello(){ + return "hello,springboot!!!"; + } + + @GetMapping("/good") + public String good(){ + return "good,springboot!"; + } +} diff --git a/springboot-524/src/main/resources/application.yml b/springboot-524/src/main/resources/application.yml new file mode 100644 index 0000000..0327679 --- /dev/null +++ b/springboot-524/src/main/resources/application.yml @@ -0,0 +1,2 @@ +#server: +# port: 8088 \ No newline at end of file diff --git a/springboot-524/src/test/java/cn/czyx007/ApplicationTests.java b/springboot-524/src/test/java/cn/czyx007/ApplicationTests.java new file mode 100644 index 0000000..c352b84 --- /dev/null +++ b/springboot-524/src/test/java/cn/czyx007/ApplicationTests.java @@ -0,0 +1,13 @@ +package cn.czyx007; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/springmvc-02-524/pom.xml b/springmvc-02-524/pom.xml new file mode 100644 index 0000000..a73b40d --- /dev/null +++ b/springmvc-02-524/pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + + org.example + springmvc-02-524 + 1.0-SNAPSHOT + war + + + 11 + 11 + UTF-8 + + + + + + org.springframework + spring-context + 5.0.3.RELEASE + + + + org.springframework + spring-webmvc + 5.0.3.RELEASE + + + org.springframework + spring-web + 5.0.3.RELEASE + + + javax.servlet + javax.servlet-api + 3.0.1 + provided + + + javax.servlet + jsp-api + 2.0 + provided + + + jstl + jstl + 1.2 + + + junit + junit + 4.12 + + + org.projectlombok + lombok + 1.18.20 + + + + com.fasterxml.jackson.core + jackson-databind + 2.9.0 + + + + + + \ No newline at end of file diff --git a/springmvc-02-524/src/main/java/cn/czyx007/mvc/bean/Condition.java b/springmvc-02-524/src/main/java/cn/czyx007/mvc/bean/Condition.java new file mode 100644 index 0000000..08cd1a2 --- /dev/null +++ b/springmvc-02-524/src/main/java/cn/czyx007/mvc/bean/Condition.java @@ -0,0 +1,14 @@ +package cn.czyx007.mvc.bean; + +import lombok.Data; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/24 - 9:09 + */ +@Data +public class Condition { + private String userName; + private Integer age; + private String address; +} diff --git a/springmvc-02-524/src/main/java/cn/czyx007/mvc/bean/User.java b/springmvc-02-524/src/main/java/cn/czyx007/mvc/bean/User.java new file mode 100644 index 0000000..e77ec86 --- /dev/null +++ b/springmvc-02-524/src/main/java/cn/czyx007/mvc/bean/User.java @@ -0,0 +1,18 @@ +package cn.czyx007.mvc.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/24 - 9:54 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class User { + private Integer id; + private String name; + private String email; +} diff --git a/springmvc-02-524/src/main/java/cn/czyx007/mvc/controller/JSONController.java b/springmvc-02-524/src/main/java/cn/czyx007/mvc/controller/JSONController.java new file mode 100644 index 0000000..3c9f08c --- /dev/null +++ b/springmvc-02-524/src/main/java/cn/czyx007/mvc/controller/JSONController.java @@ -0,0 +1,85 @@ +package cn.czyx007.mvc.controller; + +import cn.czyx007.mvc.bean.User; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/24 - 10:52 + */ +//@Controller +@RestController +public class JSONController { + +// @ResponseBody + @GetMapping("/json") + public String showJson(){ + return "hello,json!!!"; + } + + @GetMapping("/json2") +// @ResponseBody + public Boolean showJson2(){ + return true; + } + + @GetMapping("/getUser") +// @ResponseBody + public User getUSer(){ + return new User(1006, "迪丽热巴", "rb@163.com"); + } + + @GetMapping("/getUserList") +// @ResponseBody + public List getUserList(){ + List users = Arrays.asList( + new User(1003, "柳岩", "ly@163.com"), + new User(1004, "柳岩2", "ly2@163.com"), + new User(1005, "柳岩3", "ly3@163.com") + ); + return users; + } + + /** + * data:封装数据 List + * msg:数据返回成功/失败 + * code:200/500 + * Map自动转换为JSON格式 + */ + @GetMapping("/getUserMap") +// @ResponseBody + public Map getUserMap(){ + Map map = null; + try { + map = new HashMap<>(); + List users = Arrays.asList( + new User(1003, "柳岩", "ly@163.com"), + new User(1004, "柳岩2", "ly2@163.com"), + new User(1005, "柳岩3", "ly3@163.com") + ); + map.put("data", users); + map.put("msg", "数据返回成功"); + map.put("code", 200); + } catch (Exception e){ + e.printStackTrace(); + map.put("data", null); + map.put("msg", "数据返回失败"); + map.put("code", 500); + } + return map; + } + + @PostMapping("/getUserJson") + public String getUserJson(@RequestBody User user){ + System.out.println("user = " + user); + return "ok"; + } +} diff --git a/springmvc-02-524/src/main/java/cn/czyx007/mvc/controller/UserController.java b/springmvc-02-524/src/main/java/cn/czyx007/mvc/controller/UserController.java new file mode 100644 index 0000000..baf1ae0 --- /dev/null +++ b/springmvc-02-524/src/main/java/cn/czyx007/mvc/controller/UserController.java @@ -0,0 +1,132 @@ +package cn.czyx007.mvc.controller; + +import cn.czyx007.mvc.bean.Condition; +import cn.czyx007.mvc.bean.User; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import javax.servlet.jsp.PageContext; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/23 - 11:40 + */ +@Controller +@RequestMapping("/user") +public class UserController { + @RequestMapping("/user") + public ModelAndView showUser(){ + //接收器群 + System.out.println("成功接收到请求"); + //处理请求-不处理-将来要处理 + System.out.println("处理请求...不处理-将来要处理"); + //返回响应 + //ModelAndView封装数据和视图 + ModelAndView modelAndView = new ModelAndView(); + //1.封装数据 + modelAndView.addObject("userName", "blue"); + //2.封装视图 + 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"; + } + + + @PostMapping("/showCondition") + public String showCondition(Condition condition){ + System.out.println("codnition = " + condition); + return "index"; + } + + @PostMapping("/showMap") + public String showMap(@RequestParam Map map){ + Set> entries = map.entrySet(); + for (Map.Entry entry : entries) { + System.out.println(entry.getKey() + "," + entry.getValue()); + } + return "index"; + } + + //Restful风格传递数据 + @GetMapping("/showRestful/{id}/{userName}") + public String showRestful(@PathVariable("id") Integer id, + @PathVariable("userName") String name){ + System.out.println("id = " + id); + System.out.println("name = " + name); + return "index"; + } + + @GetMapping("/showView") + public String showView(Model model){ + User user = new User(1003, "柳岩", "ly@163.com"); + model.addAttribute("user", user); + return "showUser"; + } + + @GetMapping("/showViewList") + public String showViewList(Model model){ + List users = Arrays.asList( + new User(1003, "柳岩", "ly@163.com"), + new User(1004, "柳岩2", "ly2@163.com"), + new User(1005, "柳岩3", "ly3@163.com") + ); + model.addAttribute("userList", users); + return "showUserList"; + } + + @GetMapping("/showServlet") + public String showServlet(HttpServletRequest request, + HttpServletResponse response, + HttpSession session, + ServletContext context, + PageContext pageContext){ + return "show"; + } + + @GetMapping("/showViewMap") + public String showViewMap(Map map){ + User user = new User(1003, "柳岩-map", "ly@163.com"); + map.put("user", user); + return "showUser"; + } +} diff --git a/springmvc-02-524/src/main/resources/springmvc.xml b/springmvc-02-524/src/main/resources/springmvc.xml new file mode 100644 index 0000000..cea9722 --- /dev/null +++ b/springmvc-02-524/src/main/resources/springmvc.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/springmvc-02-524/src/main/webapp/WEB-INF/jsp/index.jsp b/springmvc-02-524/src/main/webapp/WEB-INF/jsp/index.jsp new file mode 100644 index 0000000..8f239d0 --- /dev/null +++ b/springmvc-02-524/src/main/webapp/WEB-INF/jsp/index.jsp @@ -0,0 +1,18 @@ +<%-- + Created by IntelliJ IDEA. + User: zyx + Date: 2023/5/23 + Time: 10:38 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +<%--el表达式--%> + ${userName} +

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

+ + diff --git a/springmvc-02-524/src/main/webapp/WEB-INF/jsp/show.jsp b/springmvc-02-524/src/main/webapp/WEB-INF/jsp/show.jsp new file mode 100644 index 0000000..3bf1dc8 --- /dev/null +++ b/springmvc-02-524/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-02-524/src/main/webapp/WEB-INF/jsp/showUser.jsp b/springmvc-02-524/src/main/webapp/WEB-INF/jsp/showUser.jsp new file mode 100644 index 0000000..a864ece --- /dev/null +++ b/springmvc-02-524/src/main/webapp/WEB-INF/jsp/showUser.jsp @@ -0,0 +1,17 @@ +<%-- + Created by IntelliJ IDEA. + User: zyx + Date: 2023/5/24 + Time: 9:55 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + 用户数据: + ${user.id} -- ${user.name} --- ${user.email} + + diff --git a/springmvc-02-524/src/main/webapp/WEB-INF/jsp/showUserList.jsp b/springmvc-02-524/src/main/webapp/WEB-INF/jsp/showUserList.jsp new file mode 100644 index 0000000..1e41f0c --- /dev/null +++ b/springmvc-02-524/src/main/webapp/WEB-INF/jsp/showUserList.jsp @@ -0,0 +1,20 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: zyx + Date: 2023/5/24 + Time: 9:59 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + 用户数据: + + ${user.id} -- ${user.name} --- ${user.email}
+
+ + diff --git a/springmvc-02-524/src/main/webapp/WEB-INF/web.xml b/springmvc-02-524/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..6230924 --- /dev/null +++ b/springmvc-02-524/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,39 @@ + + + + + dispatcherServlet + org.springframework.web.servlet.DispatcherServlet + + + contextConfigLocation + classpath:springmvc.xml + + + 1 + + + dispatcherServlet + + + + / + + + + encodingFilter + org.springframework.web.filter.CharacterEncodingFilter + + encoding + utf-8 + + + + encodingFilter + /* + + + \ No newline at end of file diff --git a/springmvc-02-524/src/main/webapp/index.html b/springmvc-02-524/src/main/webapp/index.html new file mode 100644 index 0000000..09b8bc8 --- /dev/null +++ b/springmvc-02-524/src/main/webapp/index.html @@ -0,0 +1,10 @@ + + + + + Title + + +

hello, springmvc!

+ + \ No newline at end of file diff --git a/springmvc-02-524/src/main/webapp/statics/img.png b/springmvc-02-524/src/main/webapp/statics/img.png new file mode 100644 index 0000000..ca5d9af Binary files /dev/null and b/springmvc-02-524/src/main/webapp/statics/img.png differ