Spring Boot ViewControllerRegistry教程展示了如何使用ViewControllerRegistry创建简单的路由。
Spring 是用于创建企业应用的流行 Java 应用框架。 Spring Boot 是 Spring 框架的演进,可帮助您轻松创建独立的,生产级的基于 Spring 的应用。
ViewControllerRegistry
ViewControllerRegistry允许创建预先配置了状态代码和/或视图的简单自动化控制器。
Spring Boot ViewControllerRegistry示例
在下面的示例中,我们使用ViewControllerRegistry创建一条简单路由。
pom.xmlsrc├───main│ ├───java│ │ └───com│ │ └───zetcode│ │ │ Application.java│ │ └───config│ │ AppConfig.java│ └───resources│ ├───static│ │ index.html│ └───templates│ hello.html└───test└───java
这是项目结构。
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.0http://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
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;@Configurationpublic class AppConfig implements WebMvcConfigurer {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/hello").setViewName("hello");}}
在AppConfig中,我们使用ViewControllerRegistry的addViewController()方法注册了一条新路由。
resources/templates/hello.html
<!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
<!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
package com.zetcode;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic 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创建简单的路由。
