【问题标题】:Loading React Pages from Spring Controller从 Spring Controller 加载 React 页面
【发布时间】:2019-04-22 15:15:20
【问题描述】:

我的 Spring Boot 应用程序中有这段代码

@RestController
public class SampleController {

public SampleController() {
    File file = new File("restresources");
    System.out.println("File -> "+ file.getAbsolutePath());
}

@GetMapping(value = "/sample")
public String sampleText(HttpServletRequest request){
    System.out.println("Sample Text");
    return "Sample Text";
}

@RequestMapping()
public String acceptAny(){
    return "ASDasdasdasdasd";
}

}

acceptAny 方法接受来自浏览器的任何 url?但我的公共文件夹中也有反应文件。因此,当我点击 localhost:8090/ 时,它没有加载 react index.html,而是加载了 acceptAny() 方法。

如何加载 react index.html 而不是调用这个 acceptAny() 方法,或者有什么方法可以从 Controller 本身加载 React index.html?

非常感谢任何帮助!

【问题讨论】:

  • 什么时候应该调用acceptAny() 方法?换句话说,哪个 URL 将运行 acceptAny() 方法?

标签: spring reactjs spring-boot spring-mvc localhost


【解决方案1】:

首先你有删除方法acceptAny 没有任何有用的职责。

你应该构建你的 reactjs 项目并将构建文件复制到 resources/publicresources/static,而不是运行项目并在浏览器中打开 localhost:8090/

【讨论】:

    猜你喜欢
    • 2014-11-15
    • 2020-06-27
    • 1970-01-01
    • 2018-05-02
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多