SpringBoot默认配置中static目录下的html文件不使用模板引擎

本文介绍如何在SpringBoot中正确配置默认访问的首页index.html,特别是使用Thymeleaf作为模板引擎时,需要将index.html放置在template目录下并设置对应的viewController。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版本:SpringBoot 1.5.10.RELEASE + thymeleaf 3.0.9.RELEASE

Springboot默认配置下访问的默认页面是 classpath路径下的四个目录中的index.html,这四个目录分别是
        /static 
        /public
        /resources
        /META-INFO/resources

而Springboot自动生成项目结构中有static和template,于是就想着把index.html页面直接放在static目录下。放入之后发现thymeleaf的th标签属性不会被替换。而在template文件下新建的html页面可以正常使用th属性。于是将index.html页面放在了template目录下,并添加viewController修改默认访问页面为template下的index.html.
package com.went.controller;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
* Created by tanwen on 2018/7/15.
*/
@Configuration
public class DefaultView extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
        super.addViewControllers(registry);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值