Springboot跳转首页实现
实现方式一:通过控制器跳转 index.html
- 引入thymeleaf
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.在templates目录下放置index.html页面
3.在springbootApplication同级目录下编写controller控制器
package com.allen.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping({"/", "/index"})
public String index(){
return "index";
}
}
实现方式二:通过扩展mvc实现
package com.allen.config;
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 MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
}
首页配置注意点
1.所有页面的静态资源都需要使用 thymeleaf 接管;@{}
2.页面国际化
- 需要配置i18n文件
- 需要修改按钮绑定参数
- 需要自定义组件 LocaleResolver
- 将组件注册到bean容器中