Spring Boot ViewControllerRegistry 教程展示了如何使用 ViewControllerRegistry 创建简单的路由。
春天 是用于创建企业应用程序的流行 Java 应用程序框架。 弹弓贴 是 Spring 框架的演变,它有助于以最小的努力创建独立的、生产级的基于 Spring 的应用程序。
ViewControllerRegistry
允许创建预先配置状态代码和/或视图的简单自动控制器。
在下面的示例中,我们使用 ViewControllerRegistry
创建了一个简单的路由。
pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───config │ │ AppConfig.java │ └───resources │ ├───static │ │ index.html │ └───templates │ hello.html └───test └───java
这是项目结构。
<?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.zetcode</groupId> <artifactId>viewregistry</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.7</version> </parent> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.6.7</version> </plugin> </plugins> </build> </project>
Spring Boot 启动器是一组方便的依赖描述符,极大地简化了 Maven 配置。 spring-boot-starter-parent
有一些 Spring Boot 应用程序的通用配置。 spring-boot-starter-web
是使用 Spring MVC 构建 web(包括 RESTful 应用程序)的入门工具。它使用 Tomcat 作为默认的嵌入式容器。 spring-boot-starter-thymeleaf
是使用 Thymeleaf 视图构建 MVC 网络应用程序的入门工具。
spring-boot-maven-plugin
在 Maven 中提供 Spring Boot 支持,允许我们打包可执行的 JAR 或 WAR 存档。它的 spring-boot:run
目标运行 Spring Boot 应用程序。
package com.zetcode.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class AppConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/hello").setViewName("hello"); } }
在 AppConfig
中,我们使用 ViewControllerRegistry 的
addViewController()
方法注册了一个新路由。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello page</title> </head> <body> <p> Hello there </p> </body> </html>
hello.html
视图显示一条简单的消息。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home page</title> </head> <body> <p> This is home page. Go to <a href="hello">hello page</a> </p> </body> </html>
这是一个主页。
package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Application
设置 Spring Boot 应用程序。 @SpringBootApplication
启用自动配置和组件扫描。
$ mvn spring-boot:run
应用程序运行后,我们可以导航到 localhost:8080/
。
在本教程中,我们展示了如何使用 Spring ViewControllerRegistry
创建简单的路由。
地址:https://www.cundage.com/article/springboot-viewcontrollerregistry.html