使用Spring Boot提供React App时出现React-Router问题

qbo*_*olt 10 spring reactjs spring-boot react-router

我目前需要使用Spring Boot来提供我的React应用程序.它适用于根URL - localhost:8080/但当然没有子路由被Spring Controller识别.

我不确定如何在没有硬编码的情况下将React Routing和Spring请求映射排成一行,以便映射到每个可能的路由index.html- 然后一些路由具有可变的子路由.


这是我的HomeController服务index.html

@Controller
public class HomeController {

    @RequestMapping(value = "/")
    public String index() {
        return "index.html";
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的React App中的路由渲染

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>
)
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏!


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

@Controller
public class HomeController {

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

我可以访问/,/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]
Run Code Online (Sandbox Code Playgroud)

Fad*_*add 10

对我来说,我在这个链接中尝试了答案.我没有控制器.

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

@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {

  @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:/");
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,\\w+ 正则表达式与连字符不匹配,因此您可能需要根据您的路线进行调整 (2认同)