3

Spring认证指南:了解如何使用 Spring MVC 和 Thymeleaf 创建网页

 2 years ago
source link: https://blog.51cto.com/u_15326439/5154840
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

原标题:了解如何使用 Spring MVC 和 Thymeleaf 创建网页(来源:Spring中国教育管理中心)

Spring认证指南:了解如何使用 Spring MVC 和 Thymeleaf 创建网页_spring

本指南将引导您完成使用 Spring 创建“Hello, World”网站的过程。

你将建造什么

您将构建一个具有静态主页并且还将接受 HTTP GET 请求的应用程序:http://localhost:8080/greeting。

它将响应一个显示 HTML 的网页。HTML 的正文将包含一个问候语:“Hello, World!”

name您可以使用查询字符串中的可选参数自定义问候语。该 URL 可能是http://localhost:8080/greeting?name=User.

name参数值覆盖默认值,并通过World内容更改为“Hello,User!”反映在响应中

你需要什么

  • 约15分钟
  • 最喜欢的文本编辑器或 IDE
  • JDK 1.8或更高版本
  • Gradle 4+或Maven 3.2+
  • 您还可以将代码直接导入 IDE:
  • 弹簧工具套件 (STS)
  • IntelliJ IDEA

如何完成本指南

像大多数 Spring入门指南一样,您可以从头开始并完成每个步骤,也可以绕过您已经熟悉的基本设置步骤。无论哪种方式,您最终都会得到工作代码。

从头开始,请继续从 Spring Initializr 开始。

跳过基础知识,请执行以下操作:

  • 下载并解压缩本指南的源存储库,或使用Git克隆它:git clone https://github.com/spring-guides/gs-serving-web-content.git
  • 光盘进入gs-serving-web-content/initial
  • 跳转到创建 Web 控制器。

完成后,您可以对照中的代码检查结果gs-serving-web-content/complete。

从 Spring Initializr 开始

您可以使用这个预先初始化的项目并单击 Generate 下载 ZIP 文件。此项目配置为适合本教程中的示例。

手动初始化项目:

  1. 导航到https://start.spring.io。该服务提取应用程序所需的所有依赖项,并为您完成大部分设置。
  2. 选择 Gradle 或 Maven 以及您要使用的语言。本指南假定您选择了 Java。
  3. 单击Dependencies并选择Spring WebThymeleafSpring Boot DevTools
  4. 单击生成
  5. 下载生成的 ZIP 文件,该文件是根据您的选择配置的 Web 应用程序的存档。

如果您的 IDE 具有 Spring Initializr 集成,您可以从您的 IDE 完成此过程。

你也可以从 Github 上 fork 项目并在你的 IDE 或其他编辑器中打开它。

创建 Web 控制器

在 Spring 构建网站的方法中,HTTP 请求由控制器处理。@Controller您可以通过注释轻松识别控制器。在以下示例中,通过返回 a 的名称(在本例中GreetingController为 )来处理 GET 请求。A负责呈现 HTML 内容。以下清单(来自)显示了控制器:/greetingViewgreetingViewsrc/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";
}

}复制

这个控制器简洁明了,但是有很多事情要做。我们一步一步分解。

@GetMapping注释确保 HTTP GET 请求/greeting映射到greeting()方法。

@RequestParam将查询字符串参数的值绑定name到方法的name参数中greeting()。此查询字符串参数不是required。如果请求中不存在,则使用defaultValueof World。参数的值name被添加到Model对象中,最终使视图模板可以访问它。

方法体的实现依赖于视图技术(在本例中为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>

确保你的类路径上有 Thymeleaf(工件坐标:)

org.springframework.boot:spring-boot-starter-thymeleaf。它已经存在于 Github 的“初始”和“完整”示例中。

Spring Boot 开发工具

开发 Web 应用程序的一个常见功能是编写更改代码、重新启动应用程序并刷新浏览器以查看更改。整个过程会消耗大量时间。为了加快这个刷新周期,Spring Boot 提供了一个方便的模块,称为spring-boot-devtools。Spring Boot 开发工具:

  • 启用热插拔。
  • 切换模板引擎以禁用缓存。
  • 启用 LiveReload 以自动刷新浏览器。
  • 基于开发而不是生产的其他合理默认值。

运行应用程序

Spring Initializr 为您创建了一个应用程序类。在这种情况下,您不需要进一步修改 Spring Initializr 提供的类。以下清单(来自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 Boot 根据类路径设置、其他 bean 和各种属性设置开始添加 bean。例如,如果spring-webmvc位于类路径上,则此注释将应用程序标记为 Web 应用程序并激活关键行为,例如设置DispatcherServlet.
  • @ComponentScan: 告诉 Spring 在包中查找其他组件、配置和服务com/example,让它找到控制器。

该main()方法使用 Spring Boot 的SpringApplication.run()方法来启动应用程序。您是否注意到没有一行 XML?也没有web.xml文件。这个 Web 应用程序是 100% 纯 Java,您不必处理任何管道或基础设施的配置。

构建一个可执行的 JAR

您可以使用 Gradle 或 Maven 从命令行运行应用程序。您还可以构建一个包含所有必要依赖项、类和资源的单个可执行 JAR 文件并运行它。构建可执行 jar 可以在整个开发生命周期、跨不同环境等中轻松地将服务作为应用程序交付、版本化和部署。

如果您使用 Gradle,则可以使用./gradlew bootRun. 或者,您可以使用构建 JAR 文件./gradlew build,然后运行 JAR 文件,如下所示:

java -jar build/libs/gs-serving-web-content-0.1.0.jar

如果您使用 Maven,则可以使用./mvnw spring-boot:run. 或者,您可以使用构建 JAR 文件,./mvnw clean package然后运行该 JAR 文件,如下所示:

java -jar 目标/gs-serving-web-content-0.1.0.jar

此处描述的步骤创建了一个可运行的 JAR。您还可以构建经典的 WAR 文件。

显示记录输出。应用程序应在几秒钟内启动并运行。

测试应用程序

现在网站正在运行,请访问http://localhost:8080/greeting,您应该会看到“Hello, World!”。

通过访问提供name查询字符串参数http://localhost:8080/greeting?name=User。注意消息是如何从“Hello, World!”改变的。到“你好,用户!”:

这一变化表明,@RequestParam安排在GreetingController按预期工作。该name参数已被赋予默认值World,但可以通过查询字符串显式覆盖它。

静态资源,包括 HTML、JavaScript 和 CSS,可以通过将它们放到源代码中的正确位置从 Spring Boot 应用程序提供。默认情况下,Spring Boot 从位于/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/resources/static/index.html):

<!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 开发了一个网页。​


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK