作者:mobiledu2502886053 | 来源:互联网 | 2023-10-13 10:05
我正在使用带有Java 11的Spring MVC / JSP和SpringMVC 5.2.0开发一个Web应用程序,该应用程序以配置文件开头,但是我想尝试一下程序化的配置(通过Java)。我已经运行了Servlet,并且如果用户未登录,则已经成功使登录页面“拦截”任何其他.jsp视图。但是,我无法从资源中获取资源。文件夹。
应该从那里加载的每个资源(例如
)收到404响应,并且在我的IDE(Netbeans 11)中,在网络监视器中收到以下消息:
请求网址:http://localhost:8080/icon.png
方法:GET
状态:404
注意:URL应为http://localhost:8080/dragonline/resources/img/icon.png(
)
这是我的配置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时。
请帮助我们。
我知道了。问题是我没有在Spring Security配置文件中授予访问资源文件夹的权限,所以我通过添加以下内容解决了该问题:
http.authorizeRequests().antMatchers("/resources/**").permitAll()
在我的WebSecurityAdapter的config()方法中