【问题标题】:JSP/SpringMVC not finding the resources folderJSP/SpringMVC 找不到资源文件夹
【发布时间】:2020-03-08 09:30:21
【问题描述】:

我正在使用 Spring MVC/JSP 和 Java 11 和 SpringMVC 5.2.0 开发一个 webapp,它从配置文件开始,但我想尝试一下编程配置(通过 java)。我已经运行了 Servlet,并且如果用户未登录,我已经成功地使登录页面“拦截”任何其他 .jsp 视图......但是,我无法从我的资源中获取我的资源文件夹。

应该从那里加载的每个资源(例如 <link type="image/png" rel="icon" href="${contextPath}/resources/img/icon.png">) 我收到 404 响应,在我的 IDE (Netbeans 11) 中,我在网络监视器中收到以下消息:

请求网址:http://localhost:8080/icon.png

方法:GET

状态:404

注意:网址应为http://localhost:8080/dragonline/resources/img/icon.png (<c:set var="contextPath" value="${pageContext.request.contextPath}"/>)

这是我的配置 java 文件:

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;
    }

}

还有我的项目结构:

icon.png:dragonline/src/main/webapp/resources/img/icon.png

视图(包括试图导入此图像的 jsp):dragonline/src/main/webapp/WEB-INF/views/login.jsp

我尝试将资源文件夹移动到任何地方,但每次都得到 404。

请,任何帮助将不胜感激。

【问题讨论】:

    标签: java spring-mvc jsp


    【解决方案1】:

    我想通了。问题是我没有在我的 Spring Security 配置文件中授予访问资源文件夹的权限,所以我通过添加来解决它:

    http.authorizeRequests().antMatchers("/resources/**").permitAll()

    在我的 WebSecurityAdapter 的 config() 方法中

    【讨论】:

      猜你喜欢
      • 2012-05-16
      • 2019-05-11
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      相关资源
      最近更新 更多