SpringBoot跳转到静态html页面&&静态文件放置位置
时间: 2024-03-10 19:49:05 浏览: 48
要在SpringBoot中实现跳转到静态HTML页面,可以使用控制器(Controller)的方式来实现。具体的实现步骤如下:
1. 在SpringBoot项目的resources文件夹下创建一个static文件夹,用来存放静态HTML文件。
2. 在控制器中添加一个请求映射(RequestMapping)注解,并指定需要跳转的静态HTML页面路径。
3. 在方法体中使用ModelAndView返回静态HTML页面的名称。
示例代码如下:
```java
@Controller
public class HtmlController {
@RequestMapping("/index")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index.html");
return modelAndView;
}
}
```
在上面的代码中,我们定义了一个控制器类HtmlController,并在其中添加了一个请求映射注解@RequestMapping("/index"),表示当访问http://localhost:8080/index时,将会执行index()方法。
在index()方法中,我们使用ModelAndView类来指定需要跳转的静态HTML页面名称,即modelAndView.setViewName("index.html")。
最后返回ModelAndView对象即可。
需要注意的是,SpringBoot默认情况下会将静态文件放置在classpath下的static目录中,如果需要更改静态文件的存放位置,可以在application.properties中添加以下配置:
```properties
# 指定静态文件存放位置
spring.resources.static-locations=classpath:/static/,file:/usr/local/static/
```
上面的配置表示将静态文件放置在classpath下的static目录和本地磁盘的/usr/local/static/目录下。