当前位置: 首页 > 知识库问答 >
问题:

JSP/SpringMVC找不到资源文件夹

马嘉勋
2023-03-14

方法:获取

现状:404

注意:URL应该是http://localhost:8080/dragonline/resources/img/icon.png( )

package com.midknightmunch.dragonline.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
import org.springframework.web.servlet.view.JstlView;

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = {"com.midknightmunch.dragonline"})
public class WebConfig implements WebMvcConfigurer{

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        //registry.addResourceHandler("/img/**").addResourceLocations("/resources/img/");
        //registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/");
        //registry.addResourceHandler("/js/**").addResourceLocations("/resources/js/");
        //I've tried with and without these 3 (the commented ones)
    }

    @Bean
    public ViewResolver internalViewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();        
        bean.setViewClass(JstlView.class);
        bean.setPrefix("/WEB-INF/views/");
        bean.setSuffix(".jsp");
        bean.setOrder(0);
        return bean;
    }

    @Bean
    public ViewResolver resourceBundleViewResolver() {
        ResourceBundleViewResolver bean = new ResourceBundleViewResolver();
        bean.setBasename("views");
        bean.setOrder(1);
        return bean;
    }

    @Bean("messageSource")
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource bean = new ReloadableResourceBundleMessageSource();

        String[] basenames = {"classpath:validation"};

        bean.setBasenames(basenames);

        return bean;
    }

}

共有1个答案

宗安宁
2023-03-14

我想通了。问题是我没有在Spring Security配置文件中授予访问resources文件夹的权限,所以我通过添加以下内容解决了这个问题:

http.authorizerequests().antmatchers(“/resources/**”).permitall()

在我的WebSecurityAdapter的config()方法中

 类似资料:
  • 我是Spring MVC的新手。在尝试基本程序时,我面临404错误页面。 Web.xml myDemoApp servletConfid。xml 控制器类 JSP页面 使用的URL: http://localhost/8050/springMVCDemo/getQuote.html 文件夹结构:[在此输入图像描述][1] 请告诉我这个错误的解决方法。

  • 我正在做一个项目,编译并运行我的IDE工作正常。所有资源都找到了,它正常工作。然而,当我构建我的. jar时,它能够找到一些资源,但不是全部。我的项目结构看起来是这样的: 当我运行jar文件时,它能够找到图像并使用它们,但是当我尝试访问html文件时,它不会打开。下面是jar的项目结构; 我以前从未处理过jar文件,所以这似乎是我犯的一个非常明显的错误。感谢您的帮助,谢谢! 编辑:访问找不到的文件

  • 我正在使用ADT 我的styles.xml:

  • 我使用grails已经快一年了。从现在开始,我想在gsp中链接css或js文件。我做了以下几件事: > 我在web-app文件夹下创建了一个新文件(例如资源文件),并将所有文件夹放在那里(例如导入bootstrap时,我在资源下有一个父文件夹bootstrap,在bootstrap下有css、img和js文件夹及其文件)。 然后,为了导入一个css文件,我做了以下操作(以下是相关文档): 这非常有

  • 问题内容: 我在当前工作目录上有文件,但是javac报告: 我正在研究ubuntu。 问题答案: 从上面的评论看来,您尝试过: 在您的Ubuntu系统上。该分离器是在UNIX系统上的Windows。 Ubuntu考虑了直到的命令,因此给出了消息。 应该编译好。

  • 我使用SpringBootMaven插件将应用程序打包为jar文件。 它可以在Itellij IDE中找到直接运行的资源文件,但之后找不到资源文件,显示错误为: JAVA木卫一。FileNotFoundException:类路径资源[jmxremote.password]无法解析为绝对文件路径,因为它不位于文件系统:jar:file:/home/XXX/target/yyyy中。罐子/BOOT-I