Spring-Boot ResourceLocations不添加css文件导致404 [英] Spring-Boot ResourceLocations not adding the css file resulting in 404

查看:564
本文介绍了Spring-Boot ResourceLocations不添加css文件导致404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作的spring-boot应用程序,在本地计算机上运行就好了。但我注意到,当我做mvn包,然后没有我的css或java脚本,位于

  / src / main / wepapp / css 

正被复制到目标目录中创建的jar文件(包)。



spring启动参考指南


65.3将现有应用程式转换为Spring Boot 静态资源可以在类路径根目录中移动到/ public(或/ static或/ resources或
/ META-INF / resources)。



24.1 .4静态内容不要使用src / main / webapp文件夹,如果你的应用程序将被打包成一个jar。虽然这个文件夹是一个
的通用标准,它只能使用war包装,它将$ b如果你生成一个jar,大多数生成工具会默认忽略$ b。


这意味着我可以把所有的js和css文件夹到文件夹中

  / src / main / resources / static 

ie现在我的结构看起来像这样

  / src / main / resources / static / css / 
/ src / main / resources / static / js /

我所有的百里香模板都位于

  / src / main / resources / templates / 

我这样做,据我所知,我需要添加ResourceHandler到我的ResourceHandlerRegistry。以前,当我所有的ccs都在/ src / main / wepapp / css /时,我的ResourceHandlers看起来像这样,它对我来说工作得很好。

  @Override 
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler(/ pdfs / **)。addResourceLocations(/ pdfs /)。setCachePeriod(0) ;
registry.addResourceHandler(/ img / **)。addResourceLocations(/ img /)。setCachePeriod(0);
registry.addResourceHandler(/ js / **)。addResourceLocations(/ js /)。setCachePeriod(0);
registry.addResourceHandler(/ css / **)。addResourceLocations(/ css /)。setCachePeriod(0);

}

我已经尝试添加多个处理程序,例如

  registry.addResourceHandler(/ css / **)addResourceLocations(/ css /)。setCachePeriod(0); 

  registry.addResourceHandler(/ css / **)。addResourceLocations(/ static / css /)setCachePeriod(0); 

  registry.addResourceHandler(/ css / **)。addResourceLocations(/)。setCachePeriod(0); 

等。



但它们都不适合我。
显示html模板,但是当尝试定位/css/corresponing.css或/js/corresponing.js时,Web浏览器控制台正在重试404.

I故意在我的测试项目中禁用Spirng安全性,以简化此问题的调试。



我不完全理解的另一件事是部署程序集。我已经阅读了一篇文章说,当我想有特定的文件夹到我的目标包jar文件生成maven,我确实需要包括这些文件夹到我的部署程序集,我没有,但是mvn包仍然不能处理我的/ src / main / static文件夹的所有内容(包括子文件夹)到目标jar文件中。我看到,但是模板文件夹复制到jar文件。所以还有一些其他的魔术发生在幕后。



>



这是我如何声明我的thymeleaf布局中的css

  / src / main / resources / templates / layout.html 


<!DOCTYPE html>
< html>
< head>
< title layout:title-pattern =$ DECORATOR_TITLE - $ CONTENT_TITLE>任务列表< / title>
< link rel =stylesheettype =text / cssmedia =allth:href =@ {/ css / syncServer.css}href =../ css / syncServer.css />
...
< / head>
< body>
...
< / body>
< / html>

我的问题是:配置到目前为止是否正确,如果是这样什么其他选项/设置需要注意,以使应用程序找到css文件位于/ src / main / static / css /



添加一个



测试项目

  git@github.com:TheDictator / sArchitecture.git 


解决方案

如果你移动整个 static 目录插入资源并完全删除 addResourceHandlers 配置,那么一切工作正常。

这意味着资源结构如下图所示:




Well i have a working spring-boot app that is running on a local computer just fine. However I noticed that when i do mvn package then none of my css or java scripts, locates in

/src/main/wepapp/css

are being copied into the jar file (package) created in the target directory.

spring boot reference guide says

65.3 Convert an existing application to Spring Boot "Static resources can be moved to /public (or /static or /resources or /META-INF/resources) in the classpath root."

24.1.4 Static Content "Do not use the src/main/webapp folder if your application will be packaged as a jar. Although this folder is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar."

So that means that i can put all my js and css folders into the folder

/src/main/resources/static

i.e. now my structure looks like that

/src/main/resources/static/css/
/src/main/resources/static/js/

all of my thymeleaf templates however are still located in

/src/main/resources/templates/

I did that, and as far as i understand know i need to add the ResourceHandler to my ResourceHandlerRegistry. Previously when all of my ccs were in "/src/main/wepapp/css/" my ResourceHandlers looked like that and it worked very well for me.

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/pdfs/**").addResourceLocations("/pdfs/").setCachePeriod(0);
    registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(0);
    registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(0);
    registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(0);

}

I have tried adding multiple handlers like

 registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(0);

or

 registry.addResourceHandler("/css/**").addResourceLocations("/static/css/").setCachePeriod(0);

or

 registry.addResourceHandler("/css/**").addResourceLocations("/").setCachePeriod(0);

etc.

but none of them worked for me. The html templates are displayed but the web browser console is reporing 404 when trying to locate /css/corresponing.css or /js/corresponing.js

I have deliberately disabled Spirng security in my test project, in order to simplify debugging of this problem.

One more thing thing that i do not completely understand is the deployment assembly. I have read an article that said that when i do want to have particular folders into my target package jar file generated by maven, i do need to include those folder into my deployment assembly, well i did however "mvn package" is still not copping all of the content(inlcuding subfolders) of my /src/main/static folder into the target jar file. I see however the "templates" folder copied into the jar file. So there is some other magic happening behind the scene.

Here is how do i declare the css in my thymeleaf layout i.e.

/src/main/resources/templates/layout.html


<!DOCTYPE html>
<html>
  <head>
    <title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">Task List</title>
    <link rel="stylesheet" type="text/css" media="all"  th:href="@{/css/syncServer.css}"  href="../css/syncServer.css" />
    ...
  </head>
  <body>
      ...
  </body>
</html>

My question is: Is the configuration i done so far correct and if so what other options/settings i need to be aware of in order to make the app find the css files locates in /src/main/static/css/

Addition one

test project

git@github.com:TheDictator/sArchitecture.git

解决方案

If you move you the whole static directory into the resources and totally remove the addResourceHandlers configuration, then everything works fine.

That means that resources structure would look like the following image:

这篇关于Spring-Boot ResourceLocations不添加css文件导致404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆