弹簧控制器中的圆形视图 [英] Circular view in spring controller

查看:37
本文介绍了弹簧控制器中的圆形视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Spring 控制器中提供静态资源.我有一个 index.html 文件,应该返回给每个请求.我的控制器是:

I'm trying to serve static resources in my Spring controller. I have a index.html file, that should be returned to each request. My controller is:

@Controller
public class IndexController {
    @RequestMapping(value = "/**", method = RequestMethod.GET)
    public String index() {
        return "index.html";
    }
}

我还添加了资源处理程序:

Also i added resource handler:

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

但最后我有以下异常:

循环视图路径 [/index.html]:将再次调度回当前处理程序 URL [/index.html].检查您的 ViewResolver 设置!(提示:由于默认视图名称生成,这可能是未指定视图的结果.)

Circular view path [/index.html]: would dispatch back to the current handler URL [/index.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

我了解,在我的配置中,所有请求都将通过我的 Spring 控制器处理.

I understand, that in my configuration all requests will be handled with my Spring controller.

所以当我要求时,例如http://localhost/someText,这个请求将被我的控制器处理,然后我的控制器发送重定向到 //localhost/index.html,这个请求去 Spring 的DispatcherServlet 然后它再次重定向到我的控制器,导致上面的异常.

So when i request for, e.g. http://localhost/someText, that request will be processed with my controller, then my controller sends redirect to //localhost/index.html, this request goes to Spring's DispatcherServlet and then it redirected to my controller again, that causes exception above.

另外我已经尝试了一些选项:制作网络过滤器,它将处理我对 /index.html 的请求,而不是通过 spring servlet,而是通过默认的(在我的情况下 DefaultServlet of undertow),但这是不可能的,因为我的应用程序没有打包为 war 并且我的 index.html 文件位于 /src/java/resources.

Also I already tried some options: to make web filter, that will process my request to /index.html not via spring servlet, but via default one (in my case DefaultServlet of undertow), but it's not possible, cause my app is not packaged as war and my index.html file located at /src/java/resources.

实际上我发现,对我来说,主要问题是在 spring 的 DispatcherServlet 处理程序中,我的请求 (//localhost/index.html) 被检测为我的控制器,而不是资源处理程序.

Actually I found, that main problem for me is that in spring's DispatcherServlet handler for my request (//localhost/index.html) detected as my controller, not resource handler.

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    for (HandlerMapping hm : this.handlerMappings) {
        if (logger.isTraceEnabled()) {
            logger.trace(
                        "Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
        }
        HandlerExecutionChain handler = hm.getHandler(request);
        if (handler != null) {
            return handler;
        }
    }
    return null;
}

那么也许有一种方法可以更改 HandlerMapping 的顺序,使资源映射具有比端点映射器更高的优先级?或者是否可以从我的控制器中排除对 /index.html 的请求,以便能够使用 spring 资源处理程序处理它?<​​/p>

So maybe there is a way to change HandlerMapping's order to make resource mapping have higher priority that endpoint mapper? Or is it possible to exclude request to /index.html from my controller to be able to handle it with spring resource handlers?

推荐答案

我通过以下方式发现了这一点:我定义了请求,应该在一些精确的控制器中处理.然后我创建了过滤器,它添加到路径中的所有其他请求前缀/root".我制作了一个控制器,它处理所有为我的 index.html 服务的/root/**"请求.

I've figured out that in following way: I defined requests, that should be processed in some exact controllers. Then i created Filter, that adds to all other requests prefix "/root" in path. And i made a controller, that processes all "/root/**" requests serving my index.html.

所以,基本上,当我请求/api/someApi"时 - 所以它会带着 API 东西进入我的控制器.但是当我请求/someStuff/"时——在过滤器中它变成/root/someStuff/"——所以它进入我的控制器,返回/index.html".

So, basically, when i request "/api/someApi" - so it goes to my controller with API stuff. But when i request "/someStuff/" - in filter it becomes "/root/someStuff/" - so it goes to my controller that returns "/index.html".

这篇关于弹簧控制器中的圆形视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆