Controller

This commit is contained in:
peri 2024-09-19 23:31:28 +08:00
parent 9ab944a1ea
commit 86cafedf57
32 changed files with 431 additions and 21 deletions

View File

@ -0,0 +1,13 @@
<component name="ArtifactManager">
<artifact type="exploded-war" name="05-SpringMVC-Quick-Start:Web exploded">
<output-path>$PROJECT_DIR$/out/artifacts/05_SpringMVC_Quick_Start_Web_exploded</output-path>
<root id="root">
<element id="javaee-facet-resources" facet="05-SpringMVC-Quick-Start/web/Web" />
<element id="directory" name="WEB-INF">
<element id="directory" name="classes">
<element id="module-output" name="05-SpringMVC-Quick-Start" />
</element>
</element>
</root>
</artifact>
</component>

View File

@ -2,15 +2,23 @@
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="01-Spring-Quick-Start" />
<module name="02-Spring-Dependency-Injection-1" />
<module name="04-Spring-Annotate" />
<module name="SpringMVC-Start" />
<module name="03-Spring-Annotate" />
<module name="Spring-Quick-Start" />
<module name="SpringBoot-Quick-Start" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="02-Spring-Dependency-Injection-1" target="1.8" />
</bytecodeTargetLevel>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="SpringBoot-Quick-Start" options="-parameters" />
</option>
</component>
</project>

17
.idea/encodings.xml Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/02-Spring-Dependency-Injection-1/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/02-Spring-Dependency-Injection-1/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/02-Spring-Dependency-Injection-2/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/02-Spring-Dependency-Injection-2/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/03-Spring-Annotate/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/03-Spring-Annotate/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/04-Spring-Annotate/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/04-Spring-Annotate/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/05-SpringMVC-Quick-Start/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/05-SpringMVC-Quick-Start/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/06-Spring-Quick-Start/06-Spring-Quick-Start/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/06-SpringBoot-Quick-Start/src/main/java" charset="UTF-8" />
</component>
</project>

View File

@ -16,5 +16,10 @@
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://maven.aliyun.com/repository/public" />
</remote-repository>
</component>
</project>

View File

@ -6,13 +6,13 @@
<list>
<option value="$PROJECT_DIR$/01-Spring-Quick-Start/pom.xml" />
<option value="$PROJECT_DIR$/02-Spring-Dependency-Injection-1/pom.xml" />
<option value="$PROJECT_DIR$/03-Spring-Annotate/pom.xml" />
<option value="$PROJECT_DIR$/04-Spring-Annotate/pom.xml" />
<option value="$PROJECT_DIR$/05-SpringMVC-Quick-Start/pom.xml" />
<option value="$PROJECT_DIR$/06-Spring-Quick-Start/06-Spring-Quick-Start/pom.xml" />
<option value="$PROJECT_DIR$/06-SpringBoot-Quick-Start/pom.xml" />
</list>
</option>
<option name="ignoredFiles">
<set>
<option value="$PROJECT_DIR$/02-Spring-Dependency-Injection-1/pom.xml" />
</set>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />

View File

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>edu.leijiaqi</groupId>
<artifactId>01-Spring-Quick-Start</artifactId>
<artifactId>02-Spring-Dependency-Injection-1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
@ -19,6 +19,11 @@
<artifactId>spring-context</artifactId>
<version>6.1.11</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
</dependencies>
</project>
</project>

View File

@ -1,6 +1,7 @@
package edu.leijiaqi;
import edu.leijiaqi.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@ -9,7 +10,6 @@ public class Main {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
Student student = (Student) context.getBean("student");
student.study();
System.out.println(student);
}
}

View File

@ -1,7 +1,6 @@
package edu.leijiaqi.entity;
public class ArtTeacher implements Teacher{
@Override
public class ArtTeacher {
public void teach() {
System.out.println("我是美术老师,我教你画画");
}

View File

@ -1,6 +1,6 @@
package edu.leijiaqi.entity;
public class PETeacher implements Teacher{
public class PETeacher implements Teacher {
public void teach() {
System.out.println("我是体育老师,我教你运动");
}

View File

@ -1,12 +1,22 @@
package edu.leijiaqi.entity;
import lombok.Data;
@Data
public class Student {
private Teacher teacher = new PETeacher();
// private Teacher teacher = new PETeacher();
private Teacher teacher;
private String name;
public void study(){
teacher.teach();
}
// 要使用依赖注入 要提供一个set方法
public void setTeacher(Teacher teacher){
public Student(Teacher teacher, String name){
this.teacher = teacher;
this.name = name;
}
// 要使用依赖注入 要提供一个set方法
// public void setTeacher(Teacher teacher){
// this.teacher = teacher;
// }
}

View File

@ -2,8 +2,10 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="teacher" class="edu.leijiaqi.entity.ArtTeacher"></bean>
<bean name="teacher" class="edu.leijiaqi.entity.PETeacher"></bean>
<bean name="student" class="edu.leijiaqi.entity.Student">
<property name="teacher" ref="teacher"></property>
<!-- <property name="teacher" ref="teacher"></property>-->
<constructor-arg name="teacher" ref="teacher"></constructor-arg>
<constructor-arg name="name" value="leijiaqi"></constructor-arg>
</bean>
</beans>

View File

@ -0,0 +1,17 @@
<?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>edu.leijiaqi</groupId>
<artifactId>03-Spring-Annotate</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,7 @@
package edu.leijiaqi;
public class ArtTeacher {
public void teach() {
System.out.println("美术");
}
}

View File

@ -0,0 +1,11 @@
package edu.leijiaqi;
import edu.leijiaqi.config.MainConfiguration;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class);
Student student =(Student)context.getBean();
}
}

View File

@ -0,0 +1,7 @@
package edu.leijiaqi;
public class PETeacher {
public void teach() {
System.out.println("体育");
}
}

View File

@ -0,0 +1,11 @@
package edu.leijiaqi;
public class Student {
private Teacher teacher;
public Student(Teacher teacher) {
this.teacher = teacher;
}
public void study() {
teacher.teach();
}
}

View File

@ -0,0 +1,4 @@
package edu.leijiaqi;
public interface Teacher {
}

View File

@ -0,0 +1,9 @@
package edu.leijiaqi.config;
import edu.leijiaqi.Teacher;
public class MainConfiguration implements Teacher {
private Teacher teacher;
public Student()
}

View File

@ -0,0 +1,17 @@
<?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>edu.leijiaqi</groupId>
<artifactId>04-Spring-Annotate</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,15 @@
package edu.leijiaqi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class IndexController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
System.out.println("Welcome to Spring MVC");
return "Hello Spring MVC";
}
}

View File

@ -0,0 +1,33 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启包扫描-->
<context:component-scan base-package="edu.leijiaqi.controller"/>
<!--开启注解驱动注册一些必要的Bean到SpringMVC容器里面-->
<mvc:annotation-driven>
<!-- 处理乱码 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 视图解析器
作用1.捕获后端控制器的返回值="index"
2.解析: 在返回值的前后 拼接 ==> "/index.jsp"
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

View File

@ -0,0 +1,6 @@
<?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">
</web-app>

View File

@ -0,0 +1,25 @@
package edu.leijiaqi.springbootquickstart.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@RestController
public class ArrayParamsController {
//数组参数
@RequestMapping(value = "/arrayTest",method = RequestMethod.POST)
public String arrayTest(String[] hobby){
System.out.println(Arrays.toString(hobby));
return "Get 请求,hobby=" + Arrays.toString(hobby);
}
//集合参数
@RequestMapping(value = "arrayTest1",method = RequestMethod.POST)
public String arrayTest1(@RequestParam List<String> hobby) {
System.out.println(hobby);
return "Get 请求hobby=" + hobby;
}
}

View File

@ -0,0 +1,18 @@
package edu.leijiaqi.springbootquickstart.Controller;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@RestController
public class DateParamsController {
//URL:http://localhost:8080/dateTest?dateTime=2024-09-19 16:52:00
@RequestMapping(value = "/dateTest")
public String dateTest(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime dateTime){
System.out.println(dateTime);
return dateTime.toString();
}
}

View File

@ -0,0 +1,25 @@
package edu.leijiaqi.springbootquickstart.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
//注解
@RestController
public class HelloController {
//URL:协议://IP:端口/路径
//URL:http://localhost:8080/hello
//注解
// @GetMapping("/hello") //通过Get请求访问到方法
// public String hello() {
// return "你好。。。。。。。";
// }
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
return "Hello World...";
}
}

View File

@ -0,0 +1,17 @@
package edu.leijiaqi.springbootquickstart.Controller;
import edu.leijiaqi.springbootquickstart.pojo.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class JsonParamsController {
@RequestMapping(value = "/jsonTest1",method = RequestMethod.POST)
public String jsonTest1(@RequestBody User user){
System.out.println(user);
return "Post 请求,name="+user.getUsername()+",password="+user.getPassword()+",province="+user.getAddress().getProvince()+",city="+user.getAddress().getCity();
}
}

View File

@ -0,0 +1,33 @@
package edu.leijiaqi.springbootquickstart.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.RestController;
@RestController
public class PathParamsController {
@RequestMapping(value = "/pathTest1/{id}",method = RequestMethod.POST)
public String pathTest1(@PathVariable Integer id) {
return "Path:id="+id;
}
@RequestMapping(value = "/pathTest2/{id}/{name}",method = RequestMethod.POST)
public String pathTest2(@PathVariable Integer id,@PathVariable String name) {
return "Path:id="+id+",name="+name;
}
//通配符请求
//**任意路径
@RequestMapping(value = "/pathTest3/**",method = RequestMethod.POST)
public String pathTest3(){
return "通配符请求:**";
}
//*只能有一级路径
@RequestMapping(value = "/pathTest4/*",method = RequestMethod.POST)
public String pathTest4(){
return "通配符请求:*";
}
}

View File

@ -0,0 +1,21 @@
package edu.leijiaqi.springbootquickstart.Controller;
import edu.leijiaqi.springbootquickstart.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PojoParamsController {
@RequestMapping(value = "/pojoTest1",method = RequestMethod.POST)
public String pojoTest1(User user){
System.out.println(user);
return "Post 请求,name="+user.getUsername()+",password="+user.getPassword();
}
@RequestMapping(value = "pojoTest2",method = RequestMethod.POST)
public String pojoTest2(User user){
System.out.println(user);
return "Post 请求,name="+user.getUsername()+",password="+user.getPassword()+",province="+user.getAddress().getProvince()+",city="+user.getAddress().getCity();
}
}

View File

@ -0,0 +1,46 @@
package edu.leijiaqi.springbootquickstart.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SimpleParamsController {
//1. Get 无参数
@RequestMapping(value = "/getTest1",method = RequestMethod.GET)
public String getTest1(){
return "Get 请求";
}
//2. Get 有参数
//URL:http://localhost:8080/getTest2?nickname=leijiaqi&age=20
@RequestMapping(value = "/getTest2",method = RequestMethod.GET)
public String getTest2(String nickname,int age){
System.out.println("Get 请求,name="+nickname+",age="+age);
return "Get 请求,name="+nickname+",age="+age;
}
//3. Get 有参数 方法的参数和请求的参数不一致
//URL:http://localhost:8080/getTest3?nickname=leijiaqi&age=20
@RequestMapping(value = "/getTest3",method = RequestMethod.GET)
public String getTest3(@RequestParam(value = "nickname",required = false) String nickname, int age){
System.out.println("Get 请求,name="+nickname+",age="+age);
return "Get 请求,name="+nickname+",age="+age;
}
//4.Post 无参数
@RequestMapping(value = "postTest1",method = RequestMethod.POST)
public String postTest1(){
return "Post 请求";
}
//5.Post 有参数
//URL:http://localhost:8080/postTest2
@RequestMapping(value = "postTest2",method = RequestMethod.POST)
public String postTest2(String nickname,int age){
System.out.println("Post 请求,name="+nickname+",age="+age);
return "Post 请求,name="+nickname+",age="+age;
}
}

View File

@ -0,0 +1,10 @@
package edu.leijiaqi.springbootquickstart.pojo;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Address {
private String province;
private String city;
}

View File

@ -0,0 +1,18 @@
package edu.leijiaqi.springbootquickstart.pojo;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class User {
private String username;
private String password;
private Address address;
//右键+generate+getter and setter
public String toString() {
return "User{" + "username='" + username + '\'' + ", password=' " + password + '\'' + '}';
}
}

View File

@ -3,6 +3,7 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="17" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>