【问题标题】:React-Router issues when serving React App with Spring Boot使用 Spring Boot 服务 React App 时出现 React-Router 问题
【发布时间】:2017-09-10 19:39:56
【问题描述】:

我目前需要使用 Spring Boot 为我的 React 应用程序提供服务。它适用于根 url - localhost:8080/,但 Spring 控制器当然不会识别任何子路由。

我不确定如何让 React Routing 和 Spring 请求映射排列起来,而无需在每个可能的路由中进行硬编码以映射到 index.html - 然后一些路由有可变的子路由。


这是我的HomeController,服务于index.html

@Controller
public class HomeController {

    @RequestMapping(value = "/")
    public String index() {
        return "index.html";
    }
}

这是我的 React 应用程序中的路由渲染

const Root = ({ store }) => (
  <Provider store={store}>
    <Router history={browserHistory}>
      <Route path="/" component={App}>
        <IndexRedirect to="users"/>
        <Route path="/org" component={OrgSearch}>
          {<Route path="/org/:orgId" component={Org}/>}
        </Route>
        <Route path="/users" component={UserSearch}>
          {<Route path="/users/:userId" component={User} />}
        </Route>
      </Route>
    </Router>
  </Provider>
)

非常感谢任何帮助!


编辑:我尝试添加一些通配符功能,但它呈现出一些奇怪的行为。这是HomeController 的更新代码。

@Controller
public class HomeController {

    @RequestMapping(value = {"/", "/users/**", "/org/**"})
    public String index() {
        return "index.html";
    }
}

我可以访问//users,但不能访问/users//users/2545

这是我尝试访问后者时的错误。

2017-04-14 09:21:59.896 ERROR 58840 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path [index.html]: would dispatch back to the current handler URL [/users/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 [/users/index.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
    at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:205) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:145) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1282) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]

【问题讨论】:

    标签: spring reactjs spring-boot react-router


    【解决方案1】:

    我遇到了类似的问题,并通过将带有 '**' 的 URL 分隔成单独的带注释的方法来解决它:

    @Controller
    public class HomeController {
    
        @RequestMapping(value = {"/"})
        public String index() {
            return "index.html";
        }
    
        @RequestMapping(value = {"/users/{**}"})
        public String users() {
            return "/";
        }
    
        @RequestMapping(value = {"/org/{**}"})
        public String org() {
            return "/";
        }
    }
    

    【讨论】:

    • 这篇文章有点旧,但我遇到了同样的问题。如果我返回 "index.html" ,我实际上会在浏览器上获得文本 "index.html" 。我确实在静态文件夹中有 react 打包的 index.html。不知道我在这里错过了什么?
    【解决方案2】:

    对我来说,我在link 中尝试了答案。 我没有控制器。

    基本上它是关于定义 addViewController...

    @Configuration
    public class WebConfiguration implements WebMvcConfigurer {
    
      @Override
      public void addViewControllers(ViewControllerRegistry registry) {
          registry.addViewController("/{spring:\\w+}")
                .setViewName("forward:/");
          registry.addViewController("/**/{spring:\\w+}")
                .setViewName("forward:/");
          registry.addViewController("/{spring:\\w+}/**{spring:?!(\\.js|\\.css)$}")
                .setViewName("forward:/");
      }
    }
    

    【讨论】:

    • 请注意,\\w+ 正则表达式与连字符不匹配,因此您可能需要根据您的路线进行调整
    【解决方案3】:

    我遇到了同样的问题,这个映射对我有用:

    @RequestMapping(value = {"/","/users/{id}"})
    

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 2018-01-14
      • 2020-04-18
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多