简体   繁体   English

Spring Intellij 中的启动不会加载图像

[英]Spring Boot in Intellij won't load image

I'm just starting to learn Spring reading "Spring In Action" and I cannot get my image to load on anything other than port 8080 (where tomcat launches).我刚刚开始学习 Spring 阅读“Spring In Action”,除了端口 8080(tomcat 启动)以外,我无法将图像加载到任何其他端口上。 This would be fine normally, but I have to keep restarting the program to load any changes to my webpage.这通常会很好,但我必须不断重新启动程序才能加载对我的网页的任何更改。

Intellij tries to run my web program on port 63342, but no images load, only text. Intellij 尝试在端口 63342 上运行我的 web 程序,但没有加载图像,只有文本。 I believe this is an issue with Thymeleaf.我相信这是 Thymeleaf 的问题。 I tried pointing tomcat to port 63342 but it won't load (port is in use?)我尝试将 tomcat 指向端口 63342 但它不会加载(端口正在使用中?)

I would like it to where I can get the reload on save feature to work, and especially being able to use the shortcuts in intellij to launch the web page.我希望它可以让我可以在保存时重新加载功能工作,特别是能够使用 intellij 中的快捷方式来启动 web 页面。

Any suggestions?有什么建议么?

Post the Thymeleaf code and you Java code and I'll see what I can do发布 Thymeleaf 代码和你 Java 代码,我会看看我能做什么

Basically the problem is, as I understand, that you're trying to open your .html template directly from IDEA.据我了解,基本上问题是您试图直接从 IDEA 打开您的.html模板。 Which means you're not using the thymeleaf engine to render it - you're just opening the template itself and not the resulting page .这意味着您没有使用thymeleaf engine来呈现它 - 您只是打开模板本身而不是结果页面 If you open the source code of this page using your browser's dev tools, you will see that all your expressions stay unprocessed which leads to it trying to fetch the resources on the wrong port (eg 63342 instead of 8080 where your application is really running and is ready to serve the resources).如果您使用浏览器的开发工具打开此页面的源代码,您将看到您的所有表达式都未处理,这导致它试图在错误的端口上获取资源(例如,您的应用程序真正运行的端口是63342而不是8080 ,并且已准备好为资源提供服务)。

Thus, you cannot edit your templates like this.因此,您不能像这样编辑您的模板。 But Thymeleaf does have an option to do that.但是 Thymeleaf 确实可以选择这样做。 If you're running your Spring Boot application from the IDE directly (eg a profile in Intellij IDEA Ultimate) you can add the following to your application.properties :如果您直接从 IDE 运行Spring Boot application (例如 Intellij IDEA Ultimate 中的配置文件),您可以将以下内容添加到您的application.properties

spring.thymeleaf.cache=false

This will tell your Spring template resolver that you do not want to cache your .html templates and it will load the files from disk every time, so if you modify your template and then hit IDE's build button it should re-package your template and when you refresh your page in browser it will load (and process.) the edited one.这将告诉您的 Spring 模板解析器您不想缓存您的.html模板,它每次都会从磁盘加载文件,所以如果您修改模板然后点击 IDE 的build按钮,它应该重新打包您的模板以及何时您在浏览器中刷新页面,它将加载(并处理)已编辑的页面。

Caution!警告! Only use this for development builds, set value to true for production environments to prevent unnecessary re-reading.仅将其用于开发构建,将值设置为true用于生产环境以防止不必要的重读。

See Spring Boot Docs on Hot Swapping for more.有关更多信息,请参阅有关热插拔的Spring 引导文档

if you want intelj to display your image you have to add a regular src and add a th:src to show the image when the program is run.如果您希望 intelj 显示您的图像,您必须添加一个常规src并添加一个th:src以在程序运行时显示图像。 Try adding both尝试同时添加

<img th:src="@{/assets/img/image.svg}" src="../static/assets/img/image.svg"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM