gpt4 book ai didi

java - Spring MVC 重写 ResourceHttpRequestHandler

转载 作者:行者123 更新时间:2023-12-02 02:47:42 25 4
gpt4 key购买 nike

我正在尝试在 Spring MVC 应用程序中重写 ResourceHttpRequestHandler。

到目前为止我有以下类(class):

@Controller
public class ResourceHttpRequestHandlerReplacer extends ResourceHttpRequestHandler implements BeanFactoryPostProcessor {

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.handleRequest(request, response);
}

public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
throws BeansException {
String[] names = factory.getBeanNamesForType(ResourceHttpRequestHandler.class);

for (String name : names) {
BeanDefinition bd = factory.getBeanDefinition(name);
bd.setBeanClassName("com.project.controllers.ResourceHttpRequestHandlerReplacer");
}
}

}

现在它应该只调用 super handleRequest,所以什么都不会改变,但我收到了这条消息:

Caused by: java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler [locations=[], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@16943e88]]] does not run within a ServletContext. Make sure the object is fully configured!

我正在尝试覆盖,以便我可以实现一些自定义逻辑,尝试查找非静态但未由 Controller 映射的资源,因为用户将能够为其 CMS 中的页面定义 URL .

谁能告诉我哪里出了问题?

谢谢

最佳答案

看起来您的ResourceHttpRequestHandlerReplacer不知道servletContext,这种情况很少见,因为您是从ResourceHttpRequestHandler扩展它的。尝试将其添加到您的类(class)中

@Autowired
ServletContext servletContext;

关于java - Spring MVC 重写 ResourceHttpRequestHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44345193/

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