SpringBoot中的路径映射
在启动类父文件夹中新建WebMvcConfig.java
package cn.itxiaoliu;
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 WebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/xiaoliu").setViewName("hello");
}
}
在templates包下新建hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello spring boot!</h1>
</body>
</html>
浏览器访问
http://localhost:8080/xiaoliu

本文详细介绍如何在SpringBoot项目中实现自定义路径映射,通过创建WebMvcConfig配置类并实现WebMvcConfigurer接口,设置特定URL路径与视图页面的对应关系。以/xiaoliu为例,演示了如何跳转至hello.html页面,实现简单但实用的页面路由功能。
1665

被折叠的 条评论
为什么被折叠?



