Spring Boot ViewControllerRegistry 教程展示了如何使用 ViewControllerRegistry 创建简单的路由。
Spring 是用于创建企业应用的流行 Java 应用框架。 Spring Boot 是 Spring 框架的演进,可帮助您轻松创建独立的,生产级的基于 Spring 的应用。
ViewControllerRegistry
ViewControllerRegistry
允许创建预先配置了状态代码和/或视图的简单自动化控制器。
Spring Boot ViewControllerRegistry
示例
在下面的示例中,我们使用ViewControllerRegistry
创建一条简单路由。
这是项目结构。
pom.xml
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 的春季启动支持,使我们能够打包可执行的 JAR 或 WAR 档案。 它的spring-boot:run
目标运行春季启动应用。
com/zetcode/config/AppConfig.java
在AppConfig
中,我们使用ViewControllerRegistry's
addViewController()
方法注册了一条新路由。
resources/templates/hello.html
hello.html
视图显示一条简单消息。
resources/static/index.html
这是一个主页。
com/zetcode/Application.java
Application
设置 Spring Boot 应用。 @SpringBootApplication
启用自动配置和组件扫描。
应用运行后,我们可以导航到localhost:8080/
。
在本教程中,我们展示了如何使用 Spring ViewControllerRegistry
创建简单的路线。