Springboot设置默认访问路径方法实现
前言
当使用springboot与其他框架结合编写web前后端时,可能存在这样的需求:我想在访问10.10.10.100时,实际上需要访问10.10.10.100/hello页面。(端口已省略,自行设置)
解决
方案1-实现WebMvcConfigurer接口
搜过很多博客,里面的内容虽然可以用。但是基本上都是基于继承WebMvcConfigurerAdapter类实现的,而官方的源码里面已经不推荐使用该类了。
下面给出我的解决方案,很简单:
importorg.springframework.context.annotation.Configuration;
importorg.springframework.core.Ordered;
importorg.springframework.web.servlet.config.annotation.ViewControllerRegistry;
importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
publicclassDefaultViewimplementsWebMvcConfigurer{
@Override
publicvoidaddViewControllers(ViewControllerRegistryregistry){
registry.addViewController("/").setViewName("hello");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
补充&注意点
setViewName:将/指向/hello
这里需要注意,因为我的项目里把url的访问路径后缀".html"全部都去掉了,所以可以这么用。如果你的不是,需要做对应调整。
补充我的application.properties文件部分配置:
server.port=80 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.mvc.favicon.enabled=true
@Configuration别忘了加,我自己就是忘了加,以为没生效,折腾半天。
方案2-@Controller路由设置
@Controller
publicclassPageController{
@GetMapping(value="/")
publicStringdefaultPath(Modelmodel){
return"hello";
}
}
properties文件配置同方案1一致
到此这篇关于Springboot设置默认访问路径方法实现的文章就介绍到这了,更多相关Springboot默认访问路径内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下