注意:项目中有加入thymeleaf的引擎模板
以下是pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bootcrud</groupId>
<artifactId>springboot-crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-crud</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--layout是布局功能扩展-->
<!--布局功能的支持,2.0以上版本支持thymeleaf3-->
<!--比如:thymeleaf3,layout2以上版本;thymeleaf2,layout1-->
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.5</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!--thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- jquery-webjars -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在properties节点中,加入了模板引擎后的jar,会在resources文件下出现templates的文件夹,我们可以把我们要访问的html放在这个文件夹下,templates就会自动帮我们找到templates文件夹下的html。
SpringbootApplication是springboot的主配置类,用main方法来启动springboot。
config文件:
package com.index.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//使用WebMvcConfigurerAdapter可以来扩展springmvc的功能
@Configuration
//@EnableWebMvc 不要接管Springmvc
public class MyMvcConfig extends WebMvcConfigurerAdapter {
//所有的WebMvcConfigurer组件都会一起起作用
//@Bean 将组件注册在容器中
@Bean
public WebMvcConfigurer webMvcConfigurer(){
WebMvcConfigurer adapter = new WebMvcConfigurerAdapter(){
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
};
return adapter;
}
}
以上代码就类似于我们以前controller中的:
@RequestMapping({"/","index.html"})
public String index(){
return "login";
}
这就很好理解了。
而在config中的这两句话,它们两句话的作用都是一模一样的。
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
可以试着在刚才的templates文件夹下加入一个html,运行springboot时,都会访问到主页面。
注:
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
的setViewName("login")中,login就是html的名字。
如果主页是index.html,只需要把在setViewName内的login改成index即可访问到。
分享到这个里,我打王者去了。