无法在Spring启动中映射<img/>,百万美元

时间:2018-01-11 09:49:29

标签: html image spring-boot

我找了很久的答案。我开始简单的Spring启动maven项目。我无法在我的html文件上显示任何img。我试图将图像放在不同的位置,如模板和静态,但这些都不起作用。一切正常(冬眠,postgre,bootstrap,百日咳)只有图像是我无法显示的东西。寻求帮助:))

这是放在index.html

<img th:src="@{/images/pruff.png}"
      src="../images/pruff.png" class="img-responsive"/>

Img放在/src/main/resources/static/images/pruff.png

2 个答案:

答案 0 :(得分:1)

它应与:

一起使用
 <img src="/images/pruff.png" class="img-responsive"/>

我创建了一个示例maven项目,因为它看起来像是一个常见问题。

https://github.com/adinafometescu/tutorials/tree/master/spring-boot-image

答案 1 :(得分:0)

我认为使用th:src选项是不必要的。您应该仅将此操作用于Spring / Java输出。

编辑:也许尝试扩展WebMvcConfigurerAdapter以配置静态资源位置


    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

    @Configuration
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter {

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler(
                    "/webjars/**",
                    "/img/**",
                    "/css/**",
                    "/js/**")
                    .addResourceLocations(
                            "classpath:/META-INF/resources/webjars/",
                            "classpath:/static/img/",
                            "classpath:/static/css/",
                            "classpath:/static/js/");
        }

    }

然后使用th:src / th:href

调用正确的文件