2017-04-14 177 views
1

我目前需要使用Spring Boot为React应用提供服务。它正在为根URL - localhost:8080/工作,但当然没有任何子路由被Spring Controller识别。React-Router在为Spring Boot提供React App时遇到问题

我不知道如何获得反应路由和弹簧请求映射排列在没有硬编码的每一个可能的路线上,以映射到index.html - 然后一些路线有可变的子路线。


这是我HomeController服务于index.html

@Controller 
public class HomeController { 

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

这里是路由在渲染我的阵营应用

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="https://stackoverflow.com/users/:userId" component={User} />} 
     </Route> 
     </Route> 
    </Router> 
    </Provider> 
) 

任何帮助,不胜感激!


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

@Controller 
public class HomeController { 

    @RequestMapping(value = {"/", "https://stackoverflow.com/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] 

回答

1

对我来说,我试过在这link答案。 我没有控制器。

基本上它是如何定义的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:/"); 
    } 
} 
0

我有同样的问题,这映射为我工作:

@RequestMapping(value = {"/","https://stackoverflow.com/users/{id}"}) 
相关问题