1

我创建了具有以下文件夹结构的示例 Spring MVC REST Maven 项目 文件夹结构

ResourceHandlerRegistry 配置如下

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.raju.spring_app")
public class RootConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static_res/*").addResourceLocations("/WEB-INF/html/static_res/");
    }
//Other methods 
}

Servlet映射如下

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/", "/static_res/*" };
    }
    //Other Methods
}

问题是每当我尝试访问资源 http://localhost:8080/spring4_rest_angular_demo/static/css/app.css

我收到 404 错误。我想保留此文件夹结构以在 index.jsp 文件中获取 css IntelliSense 建议。

<link href="static_res/css/app.css" rel="stylesheet"></link>
4

2 回答 2

1

几处更正:

代替

return new String[] { "/", "/static_res/*" };

return new String[] { "/" };

registry.addResourceHandler("/static_res/*")

registry.addResourceHandler("/static_res/**")

此外,正确的道路是

http://localhost:8080/spring4_rest_angular_demo/static_res/css/app.css

并不是

http://localhost:8080/spring4_rest_angular_demo/static/css/app.css

于 2015-12-08T09:07:35.627 回答
0

使用 Spring 3.0.4.RELEASE 及更高版本,您可以使用

<mvc:resources mapping="/resources/**" location="/public-resources/"/>

如http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources中所见

此外,您应该避免将页面放入WEB-INF. 将html/css/js层次较高的文件夹放在 web 应用程序文件夹下。通常,WEB-INF应该只有配置 xml 文件。

于 2015-12-08T04:04:03.747 回答