gpt4 book ai didi

java - VersionResourceResolver 不起作用

转载 作者:行者123 更新时间:2023-12-01 13:13:23 24 4
gpt4 key购买 nike

我的 spring 应用程序中有这样的静态资源结构:

\src
\main
\webapp
\resouces
\css\..
\js\..

并像这样配置了 VersionResourceResolver :
    @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/")
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)).resourceChain(true)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
}

我在我的 jsp 中使用 jSTL 包含了资源:
<script type="text/javascript" src="<c:url value="/resources/js/my.js"/>"></script>

但是当我运行应用程序时,我看不到任何类型的版本控制。
我错过了什么?

我也试过:

https://stackoverflow.com/a/38407644/3603806

但没有运气

更新

我包括这样的资源:
<link rel="stylesheet" href="<c:url value="/resources/css/mycss.css?v=2" />">
<script type="text/javascript" src="<c:url value="/resources/js/myjs.js?v=1"/>"></script>

这就是正在生成的内容:
<link rel="stylesheet" href="/app/resources/css/mycss.css?v=2">
<script type="text/javascript" src="/app/resources/js/myjs.js?v=1"></script>

索引页使用:
<welcome-file-list>
<welcome-file>/WEB-INF/views/index.jsp</welcome-file>
</welcome-file-list>

Controller :用于家庭等...
@RequestMapping(value = "/home", method = RequestMethod.GET)
public ModelAndView home(HttpServletRequest request, ModelMap model) {
...
}

最佳答案

可能是 ResourceUrlProvider is null 为您在 encodeURLResourceUrlEncodingFilter.ResourceUrlEncodingResponseWrapper

@Override
public String encodeURL(String url) {
ResourceUrlProvider resourceUrlProvider = getResourceUrlProvider();
if (resourceUrlProvider == null) {
logger.debug("Request attribute exposing ResourceUrlProvider not found");
return super.encodeURL(url);
}
initIndexLookupPath(resourceUrlProvider);
if (url.length() >= this.indexLookupPath) {
String prefix = url.substring(0, this.indexLookupPath);
int suffixIndex = getQueryParamsIndex(url);
String suffix = url.substring(suffixIndex);
String lookupPath = url.substring(this.indexLookupPath, suffixIndex);
lookupPath = resourceUrlProvider.getForLookupPath(lookupPath);
if (lookupPath != null) {
return super.encodeURL(prefix + lookupPath + suffix);
}
}
return super.encodeURL(url);
}

要修复它,请注册 ResourceUrlEncodingFilterweb.xml或在 WebApplicationInitializer , 如下:
    <filter>
<filter-name>resourceUrlEncodingFilter</filter-name>
<filter-class>
org.springframework.web.servlet.resource.ResourceUrlEncodingFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>resourceUrlEncodingFilter</filter-name>
<servlet-name>spring-dispatcher</servlet-name>
</filter-mapping>

哪里 spring-dispatcherDispatcherServlet , 作为:
    <servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

并确保您的 my jsp通过 spring-dispatcher 呈现, 例如:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

@RequestMapping(value = "/my-jsp", method = RequestMethod.GET)
public String home() {
return "my";
}
}

我在 GitHub 上创建了一个示例应用程序 - spring-resource-versioning ,它可以回答您的进一步问题。

希望有帮助!

关于java - VersionResourceResolver 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41871290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com