我的maven spring项目目录结构如下所示。我正在使用基于Spring-4注释的配置。我配置资源如下所示。我尝试了许多Stackoverflow问题和其他网站中建议的许多方法

Spring4加载静态资源

http://imwill.com/spring-mvc-4-add-static-resources-by-annotation/#.U5GZlXKs9i4

但是jsp文件无法加载资源,所有静态内容请求返回404错误.我在jsp中尝试了这些东西,

 <link href="resources/css/bootstrap.css" rel="stylesheet" media="screen">
 <link href="/resources/css/bootstrap.css" rel="stylesheet" media="screen">
 <link href="css/bootstrap.css" rel="stylesheet" media="screen">

编辑:我正在使用servlet2.5,因为截至目前我无法将我的项目从JBoss5升级到更高版本。JBoss5不支持servlet3,这很重要吗?

@Configuration
@ComponentScan("com.mgage.mvoice")
public class MyAppWebConfig extends WebMvcConfigurerAdapter {
     public void addResourceHandlers(ResourceHandlerRegistry registry) {  
        // I tried these many combinations separately.

        ResourceHandlerRegistration resourceRegistration = registry
            .addResourceHandler("resources/**");
        resourceRegistration.addResourceLocations("/resources/**");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/**");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/**");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("classpath:/resources/"); 
              // do the classpath works with the directory under webapp?
     }

}

Project structure

有帮助吗?

解决方案

这个工作,

   registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
.

和在JSP文件中,我引用了像

这样的静态资源
<link href="resources/css/bootstrap.css" rel="stylesheet" media="screen">
.

其他提示

我猜这有点晚了,但我刚刚刚刚面临着类似的问题。在摔跤几天后,最后它结果我的DispatcherServlet未配置为处理请求,因此从未抬起过资源。所以我希望其他人会发现这个答案有用。

如果向上给出的调度程序servlet映射到上面的CONFIC类,则映射到根(“/”),而是给顶单(例如“/ data /”),那么您可能面临同样的问题。< / p>

假设我有一个映射为我的调度程序servlet的“/ data / *”。所以我的呼叫看起来像

http://localhost:8080/myWebAppContext/data/command
.

,我认为如果我有资源映射,例如, “/ content / ** / *”,然后我可以访问它作为

http://localhost:8080/myWebAppContent/content/resourcePath
.

但这不是真的,我应该使用

http://localhost:8080/myWebAppContent/data/content/resourcePath
.

代替。这对我来说并不清楚,因为大多数样本都使用Dispatcher Servlet的映射来“/”,因此它不是那里的问题。考虑到稍后我应该先知道它 - /数据/告诉DispatcherServlet应该评估呼叫,并且内容/告诉Servlet资源处理程序是“控制器”。

但我想在我的前端(AngularJS)中非常清楚我是否查找数据(通过REST服务)或内容(返回纯文本)。数据来自数据库,但内容来自文件(例如pdf文档)。因此,我决定向Dispatcher Servlet添加两个映射:

public class MidtierWebConfig implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(MidtierAppConfig.class);

    servletContext.addListener(new ContextLoaderListener(rootContext));

    AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
    dispatcherContext.register(MidtierDispatcherConfig.class);

    Dynamic netskolaDispatcher = servletContext.addServlet(
        "dispatcher",
        new DispatcherServlet(dispatcherContext)
    );
    netskolaDispatcher.setLoadOnStartup(1);
    netskolaDispatcher.addMapping("/data/*");
    netskolaDispatcher.addMapping("/content/*");
}

}
.

midtierAppConfig类是空的,但MidTierdisPatcherConfig定义了静态资源:

@Configuration
@ComponentScan("my.root.package")
@EnableWebMvc
public class MidtierDispatcherConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
            .addResourceHandler("/courses/**/*")
            .addResourceLocations("/WEB-INF/classes/content/")
        ;
    }
}
.

现在,当我想要访问我的@clersect的时候,我使用/ data / prefix,并且当我想访问我的资源时,我使用/ content / prefix。警告是,如果我有@requestmapping(“/ app”)类,它具有@requestmapping(“/关于”)方法,那么数据/应用程序/关于内容/ app /关于该方法都会调用该方法(而且没有实际尝试我想我也可以访问资源/ app / courses / wherypath的资源),因为调度员们介绍“数据/”和“content /”,并且仅分析了URL的其余部分(“应用程序/关于“在这两种情况下)找到正确的@Controller。

无论如何,我达到的当前解决方案足够满意,所以我会把它留下。

这对我管用。档案可于 /resources/js/select.js.小心你没有失踪 @EnableWebMvc 注释。...

@EnableWebMvc
@EnableTransactionManagement
public class ApplicationContextConfig extends WebMvcConfigurerAdapter {

    @Bean(name = "viewResolver")
    public InternalResourceViewResolver getViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
        .addResourceLocations("/resources/");
    }
}

可以简化网页的URI,只能包含资源的文件名:
<link href="bootstrap.css" rel="stylesheet" media="screen">
适当的配置可能如下:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("*.css").addResourceLocations("/resources/css/");
}
.

spring连接'/ resources / css /'字符串,其中包含从URI中提取的任何文件名来标识资源的实际位置。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top