第一步: 創建一個SpringBoot應用 第二步: 創建一個實體,用來存儲數據,在src/main/java/com/example/first下創建包entity , 在entity下創建Person.java文件 第三步: 創建resource, 在src/main/java/com/exam ...
第一步: 創建一個SpringBoot應用
第二步: 創建一個實體,用來存儲數據,在src/main/java/com/example/first下創建包entity , 在entity下創建Person.java文件
package com.example.first.entity; public class Person { private String name; private int age; //seter , getter,toString方法 }
第三步: 創建resource, 在src/main/java/com/example/first下創建包resource, resource下創建PersonResource.java
package com.example.first.resource; import com.example.first.entity.Person; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/person") public class PersonResource { @RequestMapping(value = "/show",method = RequestMethod.GET) /**請求地址: localhost:8080/person/show?name=zhang&age=23 請求方式是get*/ private String show(@RequestParam("name")String name,@RequestParam("age")int age,ModelMap map){ /**@RequestParam("name")綁定請求地址中的name到參數name中 ModelMap map 存放返回內容*/ Person person = new Person(); person.setName(name); person.setAge(age); map.addAttribute("person",person); return "index"; /**返回的是顯示數據的html的文件名*/ } }
第三步:創建index.html文件
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" lang="zh"> <head> <meta charset="UTF-8"/> <title>Title</title> </head> <body> <span th:text="${person.name}"></span><hr/> <span th:text="${person.age}"></span><hr/> <span th:text="${person}"></span> </body> </html>
第四步:在application.properties中添加模板引擎
spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode = HTML5
第五步: 在pom文件中添加依賴
<?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>com.example.first</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
第五步:啟動前目錄結構
第六步: 配置啟動
第七步:啟動SpringbootApplication
package com.example.first; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } }
第八步:在地址欄輸入http://localhost:8080/person/show?name=zhang&age=23
第九步: 顯示效果