当前位置: 首页 > news >正文

SpringBoot 配置视图控制器

通过配置视图控制器(View Controllers)来简化视图的映射,而不需要编写额外的控制器方法。主要用于处理静态页面或简单的视图映射。

1. 使用 WebMvcConfigurer 配置视图控制器

通过实现 WebMvcConfigurer 接口并重写 addViewControllers 方法来配置视图控制器。

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/backend/toLoginPage").setViewName("/backend/login.html");
        registry.addViewController("/backend").setViewName("/backend/index.html");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将 /backend/** 映射到 classpath:/backend/
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
    }
    
}

资源resources目录结构
在这里插入图片描述

2. 运行应用程序

当你启动 Spring Boot 应用程序后,访问 http://127.0.0.1:8080/backend 将会显示 index.html 页面,访问 http://127.0.0.1:8080/backend/toLoginPage 将会显示 login.html 页面。
在这里插入图片描述
在这里插入图片描述


文章转载自:
http://asmara.tmizpp.cn
http://abo.tmizpp.cn
http://allometric.tmizpp.cn
http://astrocompass.tmizpp.cn
http://canard.tmizpp.cn
http://beng.tmizpp.cn
http://cannonproof.tmizpp.cn
http://charlatanry.tmizpp.cn
http://aphasiology.tmizpp.cn
http://birotation.tmizpp.cn
http://astonish.tmizpp.cn
http://babelize.tmizpp.cn
http://bismuthous.tmizpp.cn
http://annunciator.tmizpp.cn
http://animadvert.tmizpp.cn
http://calamus.tmizpp.cn
http://benioff.tmizpp.cn
http://allege.tmizpp.cn
http://abeyance.tmizpp.cn
http://boloney.tmizpp.cn
http://azotemia.tmizpp.cn
http://cantrip.tmizpp.cn
http://absentmindedly.tmizpp.cn
http://boy.tmizpp.cn
http://albuminose.tmizpp.cn
http://cerebrospinal.tmizpp.cn
http://bottlebrush.tmizpp.cn
http://beauideal.tmizpp.cn
http://bakelite.tmizpp.cn
http://applied.tmizpp.cn
http://www.dtcms.com/a/60079.html

相关文章:

  • 网络的正则拓扑与自然生长
  • Java爬虫获取淘宝商品详情数据的完整指南
  • 基于SpringBoot的餐厅点餐管理系统设计与实现(源码+SQL脚本+LW+部署讲解等)
  • C++:入门详解(关于C与C++基本差别)
  • Lua怎么学?Lua编程实战:从基础语法到高级特性
  • mysql,docker一键创建以及链接报错:Public Key Retrieval is not allowed
  • 2025-03-09 学习记录--C/C++-PTA 练习11-4 字符定位(最后一次找到的字符)
  • 前端 | CORS 跨域问题解决
  • Linux上位机开发实战(开篇)
  • 认识vue2脚手架
  • LangChain4j开发RAG入门示例
  • Unity Dots从入门到精通之 Prefab引用 转 实体引用
  • QT:TCP示例
  • linux查看定时任务与设置定时任务
  • C#的判断语句总结
  • C++面试题:C++怎么避免头文件循环引用?
  • 游戏引擎学习第146天
  • 学习小程序开发--Day1
  • 学习网络安全需要哪些基础?
  • C++单例进化论
  • P8686 [蓝桥杯 2019 省 A] 修改数组--并查集 or Set--lower_bound()的解法!!!
  • 设计模式 一、软件设计原则
  • Spring源码探析(二):BootstrapContext初始化深度解析(默认配置文件加密实现原理)
  • [算法笔记]cin和getline的并用、如何区分两个数据对、C++中std::tuple类
  • uniapp版本加密货币行情应用
  • Unity DOTS从入门到精通之EntityCommandBufferSystem
  • C#模拟鼠标点击,模拟鼠标双击,模拟鼠标恒定速度移动,可以看到轨迹
  • 【vitepress】如何搭建并部署自己的博客网站
  • sqlserver中的锁模式 | SQL SERVER如何开启MVCC(使用row-versioning)【启用行版本控制减少锁争用】
  • 基于单片机的智慧农业大棚系统(论文+源码)