12.SpringBoot之WebMvcConfigurer

SpringBoot之WebMvcConfigurer

路径映射

就是当我们只需要在Controller对请求进行页面跳转,但是不需要进行数据处理。可以使用更简单的路径映射。

我们这里借助动态页面来实现:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

如下:自定义配置类并实现WebMvcConfigurer.

1
2
3
4
5
6
7
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/ooyhao").setViewName("ooyhao");
}
}

定义ooyhao.html页面:classpath:/resources/templates/ooyhao.html

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello springboot</h1>
</body>
</html>

浏览器访问localhost:8080/ooyhao 就可以看到页面了。

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×