当网址更改时,弹簧mvc背景图像无法正常工作

Kha*_*oud 1 java jsp spring-mvc spring-boot

我想提供背景资料.当网址为(在登录页面中)时图像正确加载

/jobsspectrum/login
Run Code Online (Sandbox Code Playgroud)

在个人资料页面中使用

<body background="resources/images/black.jpg">
Run Code Online (Sandbox Code Playgroud)

但是当URL更改时,图像不会加载.例如.

/jobsspectrum/user/profile
because there is /user in the url before the page where i am using image in profile page.
Run Code Online (Sandbox Code Playgroud)

错误是

message /jobsspectrum/user/resources/images/black.jpg
description The requested resource is not available.
Run Code Online (Sandbox Code Playgroud)

我的图像在资源/图像下

当网址不是/ jobsspectrum /时,它不会选择资源

我的资源处理类是:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    registry.addResourceHandler("/resources/**").addResourceLocations("resources/");
    super.addResourceHandlers(registry);
}
Run Code Online (Sandbox Code Playgroud)

我简单的说法是只在前页中选取图像,当用户登录我的网址从/ jobsspectrum /更改为/ jobsspectrum/user/**时,资源图像部分未正确加载.

JB *_*zet 6

使用绝对路径而不是相对路径.

使用JSTL:

<body background="<c:url value='/resources/images/black.jpg' />">
Run Code Online (Sandbox Code Playgroud)

或者,没有JSTL:

<body background="${pageContext.request.contextPath}/resources/images/black.jpg">
Run Code Online (Sandbox Code Playgroud)