静态资源+ web-inf + Spring Boot

时间:2015-01-21 19:01:01

标签: java css spring maven

我有一个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

我的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>

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

我遵循标准

@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;
    }
}

我可能做错了什么?