首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring4- addResourceHandlers不解析静态资源

Spring4- addResourceHandlers不解析静态资源
EN

Stack Overflow用户
提问于 2014-07-31 14:16:42
回答 4查看 96.7K关注 0票数 36

我的maven spring项目目录结构如下所示。我使用的是基于Spring-4注释的配置。我像下面这样配置资源。我尝试了很多方法,这在很多堆栈溢出问题和其他网站中都是建议的。

Spring 4 loading static resources

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

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

代码语言:javascript
复制
 <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,因为到目前为止,我还不能将我的项目从JBoss 5升级到更高的版本。JBoss5不支持servlet 3,这很重要吗?

代码语言:javascript
复制
@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?
     }

}

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-08-01 03:22:59

这起作用了

代码语言:javascript
复制
   registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");

在jsp文件中,我引用了静态资源,如

代码语言:javascript
复制
<link href="resources/css/bootstrap.css" rel="stylesheet" media="screen">
票数 24
EN

Stack Overflow用户

发布于 2014-09-06 23:33:40

我想现在有点晚了,但是最近我遇到了一个类似的问题。经过几天的搏斗,最终发现我的DispatcherServlet没有配置来处理请求,因此资源从来没有被查找过。因此,我希望其他人会发现这个答案是有用的。

如果上面提供配置类的dispatcher servlet不是映射到根("/"),而是映射到顶部的单词(例如"/data/"),那么您可能面临同样的问题。

假设我的dispatcher servlet有一个映射为"/data/*“。所以我的电话看起来

代码语言:javascript
复制
http://localhost:8080/myWebAppContext/data/command

我想,如果我有一个资源映射,例如"/content/**/*",那么我可以访问它

代码语言:javascript
复制
http://localhost:8080/myWebAppContent/content/resourcePath

但这不是真的,我应该用

代码语言:javascript
复制
http://localhost:8080/myWebAppContent/data/content/resourcePath

而不是。对于我来说,这一点还不清楚,因为大多数示例都使用根"/“来进行dispatcher servlet的映射,因此这并不是一个问题。在以后的考虑中,我应该早知道- /data/告诉DispatcherServlet应该评估调用,content/告诉servlet资源处理程序是“控制器”。

但是,我想在我的前端(angularJs)中非常清楚地说明,我是通过其他服务查找数据,还是查找内容(返回纯文本)。数据来自数据库,但内容来自文件(例如pdf文档)。因此,我决定向dispatcher servlet添加两个映射:

代码语言:javascript
复制
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定义了静态资源:

代码语言:javascript
复制
@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/")
        ;
    }
}

现在,当我想要访问我的@Controller's时,我使用/data/前缀,当我想访问我的资源时,我使用/content/前缀。请注意,如果我有一个@RequestMapping("/app")类,它有一个@RequestMapping("/about")方法,那么data/app/ just和content/app/about都会调用该方法(而且不用实际尝试,我猜我也可以访问资源/app/courses/whatEverPath ),因为调度员会同时查找"data/“和"content/",并且只分析url的其余部分(在这两种情况下都是”app/about“),以找到正确的@Controller。

不管怎么说,我目前所达到的解决方案对我来说已经足够令人满意了,所以我将保持现状。

票数 13
EN

Stack Overflow用户

发布于 2015-06-05 09:51:11

这对我有用。/resources/js/select.js提供的文件。注意,您没有缺少@EnableWebMvc注释.

代码语言:javascript
复制
@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/");
    }
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25061237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档