SpringBoot项目通过ip可以直接访问主页

1.修改端口80

想要通过ip直接访问到主页,就要使用默认的80端口

# 端口号和上下文路径
server:
  port: 80
  servlet:
    context-path: /

 2.引入pom依赖

<!-- 访问html文件依赖begin -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3.设置模板前缀

如果你要转到的页面是在static目录中的,也可以将thymeleaf修改为static

spring:
  thymeleaf:
    prefix: classpath:/thymeleaf/

4.编写controller类

编写一个IndexController 类,其中index.html是直接在thymeleaf目录下

@Controller
public class IndexController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }
}

到此运行即可直接通过ip来访问index.html了

猜你喜欢

转载自blog.csdn.net/yueyue763184/article/details/128247001