🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Spring Boot `ViewControllerRegistry`教程 > 原文: [http://zetcode.com/springboot/viewcontrollerregistry/](http://zetcode.com/springboot/viewcontrollerregistry/) Spring Boot `ViewControllerRegistry`教程展示了如何使用`ViewControllerRegistry`创建简单的路由。 Spring 是用于创建企业应用的流行 Java 应用框架。 Spring Boot 是 Spring 框架的演进,可帮助您轻松创建独立的,生产级的基于 Spring 的应用。 ## `ViewControllerRegistry` `ViewControllerRegistry`允许创建预先配置了状态代码和/或视图的简单自动化控制器。 ## Spring Boot `ViewControllerRegistry`示例 在下面的示例中,我们使用`ViewControllerRegistry`创建一条简单路由。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───config │ │ AppConfig.java │ └───resources │ ├───static │ │ index.html │ └───templates │ hello.html └───test └───java ``` 这是项目结构。 `pom.xml` ```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>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</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> </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 Web 应用的入门工具。 在`spring-boot-maven-plugin`提供了 Maven 的 Spring Boot 支持,使我们能够打包可执行的 JAR 或 WAR 档案。 它的`spring-boot:run`目标运行 Spring Boot 应用。 `com/zetcode/config/AppConfig.java` ```java 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()`方法注册了一条新路由。 `resources/templates/hello.html` ```java <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello page</title> </head> <body> <p> Hello there </p> </body> </html> ``` `hello.html`视图显示一条简单消息。 `resources/static/index.html` ```java <!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> ``` 这是一个主页。 `com/zetcode/Application.java` ```java 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`启用自动配置和组件扫描。 ```java $ mvn spring-boot:run ``` 应用运行后,我们可以导航到`localhost:8080/`。 在本教程中,我们展示了如何使用 Spring `ViewControllerRegistry`创建简单的路由。 * [Spring Boot PostgreSQL 教程](/springboot/postgresql/) * [Spring Boot `@Bean`](/articles/springbootbean/) * [Spring Boot `@Qualifier`注解](/articles/springbootqualifier/)