美文网首页
spring thymeleaf 修改页面不会自动更新解决方法

spring thymeleaf 修改页面不会自动更新解决方法

作者: dozenx | 来源:发表于2019-08-26 16:50 被阅读0次

在本地化开发的时候 我们经常使用 application的main方法 或者 spring-boot:run的方式启动项目

申明

本方法不支持application.java的main方法启动方式,应为他跨过了mvn 执行了启动.

问题排查过程

在spring-boot:run的时候 我想用thymeleaf来渲染前端,但是发现修改页面并不能刷新到浏览器更新.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

我试了很多网上的方式 讲要配置application.properties的cache 要配置 application.yml 的spring.devtools.restart都不行,或者太恶心,修改一下页面整个应用居然要重启一遍真是无语.简直是技术的倒退.

后来想想,以前开发spring-mvc的时候用tomcat:run的时候没有这么多事情的,
纳闷spring-boot:run也是走mvn 为什么不行呢

于是我重新检查了 pom.xml

问题解决方式

第一步

在pom.xml 的build resources里加上指定的静态文件 如*.html

<build>
        <plugins>
          
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>

           ====
        </plugins>

        <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/webapp</directory>
                <includes>
                    <include>**/*.jsp</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>

            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.cfg</include>
                    <include>**/*.html</include>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>

            </resource>
        </resources>

    </build>

第二步

以spring-boot:run 启动项目

小结

大家可以自行按照自己的需求进行添加
然后就发现可以了

然后我就把application.properties application.yml 还有idea的配置都还原回去了 ,
根本不需要配置那些东西,加入thymeleaf的功能只需要两步骤就好了

maven 果然是java开发者的一大神器也

相关文章

网友评论

      本文标题:spring thymeleaf 修改页面不会自动更新解决方法

      本文链接:https://www.haomeiwen.com/subject/mpjdectx.html