繁体   English   中英

静态资源+ web-inf + Spring Boot

[英]Static resources + web-inf + Spring Boot

我有一个Spring Boot项目。 项目结构如下:

src 
|-  main
     |-java
        |-demo
     |-resources 
        |-static
            |-css/mycss.css

当我在这上面运行maven构建时,我的css被打包成这样:

web-inf
    |-classes
        |-demo
        |-static
            |-css/mycss.css

我的问题是为什么它是“web-inf / classes”文件夹中的包静态资源,我无法通过localhost:8080/css/mycss.css or localhost:8080/static/css/mycss.css访问我的CSS localhost:8080/css/mycss.css or localhost:8080/static/css/mycss.css

我的pom.xml是:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

src / main / resources是maven中的默认资源文件夹,因此默认情况下,您在此处放置的任何内容都将在classpath中,因此它将被放入WEB-INF / classes中。

我遵守标准

@SpringBootApplication
public class ConfigInitializer extends SpringBootServletInitializer{
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
        return application.sources(ConfigInitializer.class);
    }
    @Override
    public void onStartup(ServletContext container) throws ServletException {
        WebApplicationContext context = getContext();
        Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(context));
        registration.setLoadOnStartup(1);
        registration.addMapping("/*"); // required JBOSS EAP 6.2.0 GA
        super.onStartup(container);
    }
    private WebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation(ConfigInitializer.class.getName());
        return context;
    }
}

我可能做错了什么?

暂无
暂无

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

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