模板引擎,相信在此之前,我们最熟悉的应该是jsp了吧,SSH或者SSM项目基本使用的都是jsp,而今天的springboot却让我们不用jsp,已知原因如下:
对于Jetty和Tomcat,如果使用war,它应该可以工作。使用可执行jar时,不支持JSP。
Undertow不支持JSP。
创建自定义error.jsp页面不会覆盖默认视图以进行错误处理。 应改用自定义错误页面。
由于spring boot支持 Netty,Tomcat,Jetty和Undertow,所以从大局考虑,介意开发者不使用jsp了,
那么我们今天就是用thymeleaf模板引擎,也是springboot推荐得一种。
创建一个maven项目,
1、然后在pom文件中加入web依赖和模板依赖:
<?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>
<groupId>com.osy</groupId>
<artifactId>SringbootGetStart</artifactId>
<version>1.0-SNAPSHOT</version>
<!--Springboot的必须必须依赖,他是parent标签,有兴趣点击进去,他帮我们维护了很多包-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<!--指定阿里云仓库-->
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<!--web的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--thymeleaf模板引擎依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、创建引用启动类:
package com.osy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@EnableWebMvc // 启用webMvc
@SpringBootApplication // 启动应用注解
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3、在src/main/resources下面创建static和templates目录,并且在static创建index.js和templates下创建index.html
4、创建IndexController.java
package com.osy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("index")
public String index(){
return "index";
}
}
5、启动项目,访问http://localhost:8080/index
访问成功!
这里我使用的2.3.1,对于js或者css刚开始是无法访问的,官方说的放在static或者public下面就是静态资源,可以访问的,但是我的这里是404,原因未知,所以就自定义了静态资源访问。
package com.osy.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfiguration {
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
// 添加静态资源注册
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}
};
}
}
如果你的项目也访问不到,不妨这样试试。