當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot中静态文件获得Thymeleaf支持(配置porm.xml)
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot中静态文件获得Thymeleaf支持(配置porm.xml)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
?
理論
演示
理論
當把靜態(tài)文件放到templates時,能獲取模板的支持,但需要配置好對應(yīng)的porm.xml才能得到支持,不然總是訪問404。
?
演示
如下項目結(jié)構(gòu):
如下templates中有如下的幾個html
prom.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.19.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.loginWebDemo</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>loginWeb</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties><dependencies><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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!--引入jquery-webjar--><dependency><groupId>org.webjars</groupId><artifactId>jquery</artifactId><version>3.3.1</version></dependency><!--引入bootstrap--><dependency><groupId>org.webjars</groupId><artifactId>bootstrap</artifactId><version>4.0.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>關(guān)鍵的地方:
?
有兩種方式進行調(diào)用,一種是用空方法:
package com.loginwebdemo.demo.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;@Controller public class HelloController {@RequestMapping({"/", "/index.html"})public String index(){return "index";}@ResponseBody@RequestMapping("/hello")public String hello(){return "Hello World!";} }第二種方式是使用Spring MVC
package com.loginwebdemo.demo.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.WebMvcConfigurerAdapter;@Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter {//所有的WebMvcConfigureAdapterregistry組建都會其作用@Bean //將組建注冊在容器中public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("login");registry.addViewController("index.html").setViewName("login");}};return adapter;} }程序運行截圖如下:
總結(jié)
以上是生活随笔為你收集整理的Spring Boot中静态文件获得Thymeleaf支持(配置porm.xml)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Arduino笔记-Rouch Sens
- 下一篇: Java笔记-使用RabbitMQ的Ja