5/24 16:40

This commit is contained in:
zyx 2023-05-24 16:40:14 +08:00
parent 3e2ee2669b
commit 6305cdf583
21 changed files with 575 additions and 4 deletions

15
.idea/compiler.xml generated
View File

@ -9,16 +9,23 @@
<outputRelativeToContentRoot value="true" />
<module name="printer-522work" />
<module name="spring519" />
<module name="spring-mybatis01-522-523" />
<module name="mybatis519" />
<module name="springmvc-01-523" />
<module name="demo516" />
<module name="demo515" />
<module name="spring-mybatis01-522-523" />
<module name="springboot-524" />
<module name="mybatis519" />
<module name="springmvc-02-524" />
<module name="spring522-annotation" />
<module name="spring522" />
<module name="mybatis516" />
<module name="demo516" />
<module name="demo515" />
<module name="mybatis517-518" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="springboot-524" options="-parameters" />
</option>
</component>
</project>

4
.idea/encodings.xml generated
View File

@ -21,7 +21,11 @@
<file url="file://$PROJECT_DIR$/spring522-annotation/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spring522/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spring522/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/springboot-524/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/springboot-524/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/springmvc-01-523/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/springmvc-01-523/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/springmvc-02-524/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/springmvc-02-524/src/main/resources" charset="UTF-8" />
</component>
</project>

3
.idea/misc.xml generated
View File

@ -4,6 +4,7 @@
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$/demo515" />
<file type="web" url="file://$PROJECT_DIR$/springmvc-01-523" />
<file type="web" url="file://$PROJECT_DIR$/springmvc-02-524" />
</component>
<component name="MavenProjectsManager">
<option name="originalFiles">
@ -21,6 +22,8 @@
<option value="$PROJECT_DIR$/printer-522work/pom.xml" />
<option value="$PROJECT_DIR$/spring-mybatis01-522-523/pom.xml" />
<option value="$PROJECT_DIR$/springmvc-01-523/pom.xml" />
<option value="$PROJECT_DIR$/springmvc-02-524/pom.xml" />
<option value="$PROJECT_DIR$/springboot-524/pom.xml" />
</list>
</option>
</component>

43
springboot-524/pom.xml Normal file
View File

@ -0,0 +1,43 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-524</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-524</name>
<description>springboot-524</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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!";
}
}

View File

@ -0,0 +1,2 @@
#server:
# port: 8088

View File

@ -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() {
}
}

73
springmvc-02-524/pom.xml Normal file
View File

@ -0,0 +1,73 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springmvc-02-524</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<!--springmvc的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!--引入json的依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
</project>

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<User> getUserList(){
List<User> 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<User>
* msg数据返回成功/失败
* code200/500
* Map自动转换为JSON格式
*/
@GetMapping("/getUserMap")
// @ResponseBody
public Map<String,Object> getUserMap(){
Map<String,Object> map = null;
try {
map = new HashMap<>();
List<User> 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";
}
}

View File

@ -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<String,Object> map){
Set<Map.Entry<String,Object>> entries = map.entrySet();
for (Map.Entry<String, Object> 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<User> 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<String,Object> map){
User user = new User(1003, "柳岩-map", "ly@163.com");
map.put("user", user);
return "showUser";
}
}

View File

@ -0,0 +1,23 @@
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<context:component-scan base-package="cn.czyx007.mvc.*"/>
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:resources mapping="/statics/**" location="/statics/"/>
</beans>

View File

@ -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" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--el表达式--%>
${userName}
<h1> >>>>这里是index.jsp<<<< </h1>
</body>
</html>

View File

@ -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" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${address}
</body>
</html>

View File

@ -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" %>
<html>
<head>
<title>Title</title>
</head>
<body>
用户数据:
${user.id} -- ${user.name} --- ${user.email}
</body>
</html>

View File

@ -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" %>
<html>
<head>
<title>Title</title>
</head>
<body>
用户数据:
<c:forEach items="${userList}" var="user">
${user.id} -- ${user.name} --- ${user.email} <br>
</c:forEach>
</body>
</html>

View File

@ -0,0 +1,39 @@
<?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>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化参数加载springmvc.xml文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--加载优先级,数字越小越高-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!-- /* 拦截处理所有请求包括jsp-->
<!-- <url-pattern>/*</url-pattern>-->
<!-- / 拦截处理所有请求不包括jsp-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello, springmvc!</h1>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 773 KiB