【问题标题】:Circular View Path Exception in Spring BootSpring Boot中的圆形视图路径异常
【发布时间】:2021-09-17 11:28:59
【问题描述】:

我正在尝试使用以下控制器为每个请求映射返回“index.html”页面:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class ReactWebController {
    @RequestMapping(value = {"/", "/login/**", "/logout/**", "/forgotpassword/**", "/home/**", "/dashboard/**"
            , "/user/**", "/profile/**", "/catalog/**", "/permission/**", "/diconnect/**", "/nsm/**", "/session/**"
            , "/settings/**", "/log/**", "/config/**", "/delete/**", "/initialize/**", "/initial/**", "/changepassword/**",
            "/portalconnect/**", "/portal/**", "/portal/user/**", "/portal/profile/**", "/portal/permission/**",
            "/portal/catalog/**", "/portal/metadata-browse/**","/portal/search/**","/portal-object-overview/**"})
    public String react() throws Exception {
        return "index.html";
    }
}

问题是,当我刷新 URL 时,当路径如下:http://localhost:18170/MartServer/user 时它正在工作,但当它具有多个文件夹类型的路径时则不行,例如http://localhost:18170/MartServer/portal/user。我得到以下异常详细信息。

编辑:但在端口 3000(前端)并非如此,我没有进行任何完整项目的构建,后端和前端是分开的。

ERROR [/MartServer].[dispatcherServlet].log: Servlet.service() for servlet [dispatcherServlet] in context with path [/MartServer] threw exception [Circular view path [index.html]: would dispatch back to the current handler URL [/MartServer/portal/index.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause
javax.servlet.ServletException: Circular view path [index.html]: would dispatch back to the current handler URL [/MartServer/portal/index.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

我也尝试添加addViewControllers()addResourceHanlders(),但没有成功。谁能帮我解决这个问题。谢谢!

前端为React JS,index.html在“npm run build”后生成,用于控制器

【问题讨论】:

    标签: java spring spring-boot spring-mvc


    【解决方案1】:

    尝试实现以下提到的属性,看看它们是否对您的情况有帮助:

    1. 在您的 ReactWebController 类上使用 @RestController 而不是 @Controller

    2. 将自定义 context-path 添加到您的 spring-boot 应用程序的 application.properties 文件中:

    server.servlet.context-path=MartServer/portal

    1. 您还可以使用 View Resolver。可以在application.properties文件中配置,如下图:
    spring.mvc.view.prefix=/WEB-INF/
    spring.mvc.view.suffix=.html
    

    查看官方Spring Boot documentation 了解有关可配置属性的更多详细信息。

    【讨论】:

    • 前2点是绝对错误的。第三个也是错误的,因为视图是为 /user 渲染的,所以很明显视图解析器工作正常。
    【解决方案2】:

    据我所知,抛出异常是正确的,因为: 你调用 http://localhost/MartServer/portal/user 得到路径 http://localhost/MartServer/portal/index.html。您在控制器中定义了路径 "/portal/**""/portal/user/**" => 路径再次匹配。

    我认为最好回顾一下你的所有路径。

    例如,如果您让"/portal/**" 它也将匹配 http://localhost/MartServer/portal/user 的路径。但这取决于您想要实现的目标。

    【讨论】:

    • 那么,你有什么建议?如果我删除 /portal/**,我不会得到任何 URL 中包含门户的内容。
    • 我不明白你的意思是什么:“你没有得到任何 URL 中有门户的东西”?据我所见,您映射“/portal/**” - 这是一条通用路径,该路径也匹配“/portal/user/**”....认为您使用通用路径,如“/portal /**” 或者您定义所有子特定路径,例如“/portal/user/**”
    • 我的意思是即使我给出子特定路径或一般路径,当我刷新时,这两种方式都不起作用
    猜你喜欢
    • 2016-06-28
    • 2017-09-12
    • 2017-07-02
    • 2020-12-25
    • 2013-09-19
    • 1970-01-01
    • 2012-05-20
    • 2018-10-14
    • 2015-01-22
    相关资源
    最近更新 更多