java - 静态内容未从 Spring Boot 应用程序加载到 JSP 页面中

标签 java html spring-mvc jsp spring-boot

我正在创建一个 SpringBoot Web 应用程序,同时渲染带有一些静态实体(如图像)的 .jsp 页面。 我尝试将静态文件添加到从 META_INFstatic 的每个文件夹中,但在控制台上收到 404 错误,并且不显示图像。

<小时/>

Here is my project structure

<小时/>

HTML代码如下:

<img src="image/nia-logo.png">

<小时/>

Spring Boot Application类如下:

@SpringBootApplication
@PropertySource("classpath:application.properties")
@EnableWebMvc
public class FeedbackApplication  implements WebMvcConfigurer{  

@Autowired
private Environment environment;


public static void main(String[] args) throws Exception {
    SpringApplication.run(FeedbackApplication.class, args);
}



@Bean
public ViewResolver getViewResolver() {     
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/jsp/");
    viewResolver.setSuffix(".jsp");
    viewResolver.setViewClass(JstlView.class);
    return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!registry.hasMappingForPattern("/resources/**")) {
        registry.addResourceHandler("/resources/**").addResourceLocations(
                "classpath:/resources/static/");
    }

}

}

<小时/>

请帮助我并指出我在哪里犯了错误。

最佳答案

在 addResourceHandlers 配置中,它定义任何以 /resources/** 开头的调用被视为资源文件。

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!registry.hasMappingForPattern("/resources/**")) {
        registry.addResourceHandler("/resources/**").addResourceLocations(
            "classpath:/static/");
    }
}

所以你需要做的就是将 src url 更改为 <img src="/resources/image/nia-logo.png">

希望这有帮助!快乐编码:)

关于java - 静态内容未从 Spring Boot 应用程序加载到 JSP 页面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52550010/

相关文章:

java - libgdx spritebatch 不渲染纹理

javascript - JavaScript 中拖放功能的问题

java - Spring MVC 和 jQuery 自动完成

java - 作为 WAR 运行 Spring Webflux 应用程序

java - 何时使用 type.length-1;和 type.length();在 java

java - 如何阻止swing应用程序的所有窗口并在它们之上显示JDialog?

html - IE边框半径显示错误

java - 使用 Spring REST 和 Jackson 解析以下 JSON 时获取 "The request sent by the client was syntactically incorrect"

java - Android/Java 上BluetoothSocket 的timeout 参数是什么单位?

html - 将样式应用于选择框,为什么不起作用?