简体   繁体   English

来自控制器和其他类的Spring Boot静态资源

[英]Spring Boot Static Resources From Controllers and Other Classes

I've been trying to solve this issue and I can't find the answer. 我一直试图解决这个问题,但我找不到答案。

I've been having to use src/main/resources/public/whatever... in order to access static resources from class files. 我一直不得不使用src/main/resources/public/whatever...来从类文件中访问静态资源。 I was under the impression that if you use /static /public /resources... that Spring Boot already has those paths configured. 我的印象是,如果您使用/static /public /resources... Spring Boot已经配置了这些路径。

I'm lost at this point and not sure how I can access these files without having to prepend src/main/resources/public. 我现在迷路了,不知道如何在不必预先添加src/main/resources/public.情况下访问这些文件src/main/resources/public.

EDIT 编辑

Based on the suggestions regarding the static content, I created a private directory to protect files etc... that I don't want publicly available. 根据有关静态内容的建议,我创建了一个私人目录来保护文件等...我不想公开提供。 And left css, images, etc... in the /public folder. 并在/ public文件夹中留下css,图像等。

Thank you all for the suggestions and recommendations, I appreciate it! 谢谢大家的建议和建议,我很感激!

After more searching I ran across an example that used the ClassLoader. 经过更多搜索后,我遇到了一个使用ClassLoader的示例。 I've only been using Spring for a short time and not aware of all the tools that are available, there might be a better way than this; 我只是在短时间内使用Spring并且没有意识到所有可用的工具,可能有比这更好的方法; if so please tell let me know. 如果是这样请告诉我。

ClassLoader classLoader = getClass().getClassLoader()
new File(classLoader.getResource('public/file-name-or-directory').getFile())

So far it looks like it's working, I need to do some further testing and will post back if any issues. 到目前为止看起来它正在工作,我需要做一些进一步的测试,如果有任何问题将发回。

The directories you list, /static , /public and /resources , are for static content that will be served up by a web-application. 您列出的目录, /static/public/resources ,用于将由Web应用程序提供的静态内容 Things like javascript files and images that will always be the same no matter who requests them and when, as opposed to JSPs for example that need to be processed on the server per request. 像javascript文件和图像这样的东西总是相同的,无论是谁请求它们以及什么时候,而不是例如需要在服务器上按需处理的JSP。

If you need to access a configured property in one of your Java classes (it must be a Spring bean), then you can put it in your application.properties file, and have Spring inject it using the @Value annotation. 如果您需要访问其中一个Java类中的已配置属性(它必须是Spring bean),那么您可以将它放在application.properties文件中,并让Spring使用@Value注释注入它。

Example code from Spring docs : Spring docs的示例代码:

import org.springframework.stereotype.*
import org.springframework.beans.factory.annotation.*

@Component
public class MyBean {

    @Value("${name}")
    private String name;

    // ...

}

Spring Boot expect you to use for content served directly src/main/resources/static Spring Boot希望您使用直接服务的内容src / main / resources / static

and to use for templating src/main/resources/templates 并用于模板src / main / resources / templates

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

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