Spring Boot中静态文件获得Thymeleaf支持(配置porm.xml)
目录
理论
演示
理论
当把静态文件放到templates时,能获取模板的支持,但需要配置好对应的porm.xml才能得到支持,不然总是访问404。
演示
如下项目结构:
如下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>
关键的地方:
有两种方式进行调用,一种是用空方法:
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;}
}
程序运行截图如下:
Spring Boot中静态文件获得Thymeleaf支持(配置porm.xml)相关推荐
- Spring boot 默认静态资源路径与手动配置访问路径的方法
这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 在application.propertis中配置 ##端口号 s ...
- Spring Boot中使用MongoDB的连接池配置
因为今天开发遇到了性能问题,可能与MongoDB的连接有关,所以稍稍深入看了一下,正好搜到原来有人写过这篇相关的内容,所以转载过来.回头有时间可以写个扩展到SpringForAll里,主体思路还是一样 ...
- Spring Boot 中yml文件小图标不正确解决方案
正确图标如图: 如果你采用后缀名为yml则会出现灰色图标,并且输入属性名称是不会有任何提示,所以可以尝试将后缀名改为yaml;
- Spring Boot中Starter是什么
比如我们要在Spring Boot中引入Web MVC的支持时,我们通常会引入这个模块spring-boot-starter-web,而这个模块如果解压包出来会发现里面什么都没有,只定义了一些POM依 ...
- 如何把网上下载的前端页面在Spring Boot中跑起来(CSS,JavaScript,程序运行等路径设置)
这个功能非常有用,估计99.99%的java web开发者都干过,本人是初学者,特写这个博客记录下! 方便本人以后查阅,方便以后进行投机取巧 这里使用thymeleaf模板引擎! 在网上下载了一个Bo ...
- Spring Boot 中三种跨域场景总结
文章目录 1.什么是跨域 2.解决方案 2.1 存在的问题 3.SpringSecurity 3.1 方式一 3.2 方式二 4.OAuth2 5.小结 跨域这个问题松哥之前写过文章,但是最近收到小伙 ...
- 8.Spring Boot中使用thymeleaf
Spring Boot中使用thymeleaf Spring Boot支持FreeMarker.Groovy.Thymeleaf和Mustache四种模板解析引擎,官方推荐使用Thymeleaf. s ...
- Spring Boot中的缓存支持(一)注解配置与EhCache使用
随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring 3开始提供了强大的基于注解的缓 ...
- 一种灵活的API设计模式:在Spring Boot中支持GraphQL
导读:GraphQL是一种基于api的查询语言,它提供了一种更高效.强大和灵活的数据提供方式.它是由Facebook开发和开源,目前由来自世界各地的大公司和个人维护.本文作者先介绍了GraphQL,随 ...
最新文章
- 如何构建一个安全的人工智能世界?
- 进入postgresql
- app登录界面背景 css_计算机毕业设计中Java web实现简登录页面(MyBatis+jsp+servlet+html+css+javascript)...
- ASP.NET MVC3 中的AJAX
- 【LeetCode笔记】124. 二叉树中的最大路径和(Java、二叉树、DFS)
- 算法:阶乘的五种算法
- linux 在家工作_我如何调整在家工作的习惯
- 模版 ----- 实数二分
- 一个用SAM维护多个串的根号特技
- 直接拿来用!十大Material Design开源项目
- 高频变压器设计,可提供完整设计过程资料
- 征信系统如何保障信息主体的合法权益?
- 关于二级域名与三级域名的解释
- 史上最全面Java面试汇总(面试题+答案)
- 量化系统工程师需要学什么?
- 基于HTML+CSS+JavaScript技术设计的博客网站(web前端期末大作业)
- 风控人都在夸交通出行数据好用,那就来看看如何挖掘与应用此类数据
- 【JAVA 文件概述】
- PMI-ACP练习题(16)
- 基于CentOS7.6系统搭建远程GIT服务器