使用http://localhost:8887/hello?name=community
**返回
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback
Sat Nov 23 17:42:18 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available
**编译器run
2019-11-23 17:38:20.780 INFO 9072 --- [ main] c.x.community.CommunityApplication : Starting CommunityApplication on SW-PC with PID 9072 (G:\community\target\classes started by SW in G:\community)
2019-11-23 17:38:20.784 INFO 9072 --- [ main] c.x.community.CommunityApplication : No active profile set, falling back to default profiles: default
2019-11-23 17:38:22.429 INFO 9072 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8887 (http)
2019-11-23 17:38:22.440 INFO 9072 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-23 17:38:22.440 INFO 9072 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-23 17:38:22.518 INFO 9072 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-23 17:38:22.518 INFO 9072 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1678 ms
2019-11-23 17:38:22.750 INFO 9072 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-23 17:38:23.136 INFO 9072 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8887 (http) with context path ''
2019-11-23 17:38:23.140 INFO 9072 --- [ main] c.x.community.CommunityApplication : Started CommunityApplication in 2.996 seconds (JVM running for 4.612)
比老师多了这几行
2019-11-23 17:38:35.007 INFO 9072 --- [nio-8887-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-11-23 17:38:35.008 INFO 9072 --- [nio-8887-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-11-23 17:38:35.016 INFO 9072 --- [nio-8887-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms**
代码
G:\community\src\main\java\com\xjuygur\community\controller\HelloController.java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController{
@GetMapping("/hello")
public String hello(@RequestParam(name="name") String name, Model model){
model.addAttribute("name",name);
return "hello";
}
}
**html
G:\community\src\main\resources\templates\hello.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
pom.xml
G:\community\pom.xml
<?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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xj-uygur</groupId>
<artifactId>community</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>community</name>
<description>community</description>
<properties>
<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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>