简体   繁体   English

如何使用Spring MVC 4从另一个Web项目引用静态资源

[英]How to reference static resources from another web project with Spring MVC 4

How to reference static resources from another web project with Spring MVC 4. We have 2 projects one with conext root /SampleWeb and other with /SampleStatic. 如何使用Spring MVC 4从另一个Web项目引用静态资源。我们有2个项目,一个使用conext root / SampleWeb,另一个使用/ SampleStatic。 Sample web project is a Spring MVC 4 application that needs to access the static HTML pages from /SampleStatic project. 示例Web项目是一个Spring MVC 4应用程序,需要从/ SampleStatic项目访问静态HTML页面。

We tried with the below configurations for MVC resource handler but it does not work. 我们尝试使用以下配置为MVC资源处理程序,但它不起作用。 The code is in /SampleWeb AppConfig.java file 代码位于/ SampleWeb AppConfig.java文件中

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    registry.addResourceHandler("/views/**").addResourceLocations(
            "/SampleStatic/");
}

You have few alternatives. 你没有其他选择。 First note that your configuration is based on the Spring's Resources abstraction 首先请注意,您的配置基于Spring的Resources抽象

in Spring MVC the prefix inside the location instructs the lookup strategy, and there are four types 在Spring MVC中,位置内的前缀指示查找策略,有四种类型

classpath: - resources loaded from the classpath.
file: - resources loaded as a URL, from the filesystem.
http: - resources loaded as a URL.
(none) - depends on the underlying `ApplicationContext`

Your current config is the forth strategy. 您当前的配置是第四个策略。 If its not working, than check how you loaded the context to learn whether the path will be relative to classpath or the relative path based on the context file system location. 如果它不起作用,则检查如何加载上下文以了解路径是相对于类路径还是基于上下文文件系统位置的相对路径。

My advice to you, since you're mixing the two apps, is to find the least common denominator classpath , file or http and use the prefix explicitely 我给你的建议,因为你正在混合两个应用程序,是找到最小公分母classpathfilehttp并明确使用前缀

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

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