2017-08-10 214 views
-1

请真的需要帮助。 enter image description here尝试在Thymeleaf和Spring Security项目中插入图片时出现404错误

这是我的项目的结构,我想插入图片到我的网页是这样的:

      <div class="modal-header" align="center"> 
      <img class="img-circle" id="img_logo" src="../../static/images/asblogo.jpg" 
       th:src="@{/images/asblogo.jpg}"/> 
         </div> 
模板的login.html

。我SecurityConfig和WebMvcConfig看起来像以下: 输入图像的描述在这里

enter image description here

enter image description here 我不知道为什么我仍然得到404或302错误。

请问,你能帮助我吗?我会非常感谢。 谢谢!!!

+0

一个问题:只有当您尝试将图像添加到登录页面时才会出现此错误?当你移除img标签时会发生什么?你能看到登录页面吗? –

回答

0

有不同的选择,让您的图片资源:

选项1:通过自己的配置映射的静态资源

转到WebMVCConfig类和映射你的网站的所有静态资源:

覆盖所述addResourceHandlers方法

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

选项2:让Spring为您

在这种情况下映射静态资源只是去@EnableWebMvc注释从WebMVCConfig

除去最后img标签应该是这样的:

img标签:

<img th:src="@{/images/yourlogo.jpg}" alt="myImage"/> 

要解决404错误

转到WebMVCConfig类并修复登录页面的路径。你所得到的错误,因为春天尝试重定向到/login路径但路径不上WebMVCConfig类,如下面的例子存在,而不是/logout使用/login

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

希望它能帮助。

问候。

+0

谢谢丹尼尔C.解决方案1工作正常! –

+0

非常好。在这种情况下你能接受这个答案作为解决方案吗?问候 –

相关问题