使用 Spring 创建“Hello, World”网站的过程。
创建 Web 控制器
在Spring构建网站的方法中,HTTP请求由控制器处理。您可以通过@Controller注释轻松识别控制器。在下面的示例中,通过返回视图的名称来处理 GET 请求(在本例中为 )。A 负责呈现 HTML 内容。以下清单(来自 )显示了控制器:GreetingController
/greeting
greeting
View
src/main/java/com/example/servingwebcontent/GreetingController.java
package com.example.servingwebcontent;
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 GreetingController {
@GetMapping("/greeting")
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";
}
}
这个控制器简洁明了,但还有很多事情要做。我们一步一步地分解它。
注释确保 HTTP GET 请求映射到方法。@GetMapping
/greeting
greeting()
@RequestParam将查询字符串参数的值绑定到方法的参数中。此查询字符串参数不是 。如果请求中不存在,则使用 of。参数的值将添加到 Model 对象,最终使其可供视图模板访问。name
name
greeting()
required
defaultValue
World
name
方法主体的实现依赖于视图技术(在本例中为 Thymeleaf)来执行 HTML 的服务器端呈现。Thymeleaf 分析模板并计算表达式以呈现在控制器中设置的参数的值。以下清单(来自 )显示了模板:greeting.html
th:text
${name}
src/main/resources/templates/greeting.html
greeting.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>
确保类路径上有百里香叶(工件坐标:)。它已经存在于 Github 的“初始”和“完整”示例中。org.springframework.boot:spring-boot-starter-thymeleaf |
Spring Boot Devtools
开发 Web 应用程序的一个常见功能是编写更改代码、重新启动应用程序以及刷新浏览器以查看更改。整个过程会占用大量时间。为了加快这个刷新周期,Spring Boot 提供了一个方便的模块,称为 spring-boot-devtools。Spring Boot Devtools:
-
启用热插拔。
-
切换模板引擎以禁用缓存。
-
启用 LiveReload 以自动刷新浏览器。
-
其他基于开发而不是生产的合理默认值。
运行应用程序
Spring 初始化器会为您创建一个应用程序类。在这种情况下,你不需要进一步修改 Spring 初始化器提供的类。以下清单(来自 )显示了应用程序类:src/main/java/com/example/servingwebcontent/ServingWebContentApplication.java
package com.example.servingwebcontent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServingWebContentApplication {
public static void main(String[] args) {
SpringApplication.run(ServingWebContentApplication.class, args);
}
}
@SpringBootApplication
是一个方便的注释,它添加了以下所有内容:
-
@Configuration
:将类标记为应用程序上下文的 Bean 定义源。 -
@EnableAutoConfiguration
:告诉 Spring 引导根据类路径设置、其他 bean 和各种属性设置开始添加 bean。例如,如果 在类路径上,则此注释会将应用程序标记为 Web 应用程序并激活关键行为,例如设置 .spring-webmvc
DispatcherServlet
-
@ComponentScan
:告诉 Spring 在包中查找其他组件、配置和服务,让它找到控制器。com/example
该方法使用 Spring Boot 的方法启动应用程序。您是否注意到没有一行 XML?也没有文件。此 Web 应用程序是 100% 纯 Java,您无需处理配置任何管道或基础结构。main()
SpringApplication.run()
web.xml
构建可执行的 JAR
您可以使用 Gradle 或 Maven 从命令行运行应用程序。您还可以构建一个包含所有必需依赖项、类和资源的可执行 JAR 文件并运行该文件。通过构建可执行 jar,可以轻松地在整个开发生命周期中跨不同环境等将服务作为应用程序进行交付、版本控制和部署。
如果使用 Gradle,则可以使用 .或者,您可以使用 JAR 文件生成 JAR 文件,然后运行该文件,如下所示:./gradlew bootRun
./gradlew build
java -jar build/libs/gs-serving-web-content-0.1.0.jar
如果使用 Maven,则可以使用 运行应用程序。或者,您可以使用 JAR 文件生成 JAR 文件,然后运行该文件,如下所示:./mvnw spring-boot:run
./mvnw clean package
java -jar target/gs-serving-web-content-0.1.0.jar
此处描述的步骤将创建一个可运行的 JAR。您还可以构建经典的 WAR 文件。 |
将显示日志记录输出。应用程序应在几秒钟内启动并运行。
测试应用程序
现在网站正在运行,请访问 ,您应该在其中看到“你好,世界!http://localhost:8080/greeting
通过访问提供查询字符串参数。请注意消息如何从“Hello, World!”更改为“Hello, User!”:name
http://localhost:8080/greeting?name=User
此更改表明 中的@RequestParam安排按预期工作。该参数的默认值为 ,但可以通过查询字符串显式覆盖它。GreetingController
name
World
添加主页
静态资源,包括 HTML 和 JavaScript 和 CSS,可以通过将它们放在源代码中的正确位置来从 Spring Boot 应用程序中提供。缺省情况下,Spring 引导从类路径中的资源提供静态内容 (或 )。该资源是特殊的,因为如果存在,它将用作“欢迎页面,”)。因此,您需要创建以下文件(您可以在 中找到):/static
/public
index.html
"serving-web-content/ which means it is served up as the root resource (that is, at `http://localhost:8080/
src/main/resourc
<!DOCTYPE HTML>
<html>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Get your greeting <a href="/greeting">here</a></p>
</body>
</html>
重新启动应用程序时,您将在 处看到 HTML。http://localhost:8080/
总结
祝贺!您刚刚使用 Spring 开发了一个网页。