【问题标题】:React blank screen in Springboot using Thymeleaf使用 Thymeleaf 在 Springboot 中反应空白屏幕
【发布时间】:2020-06-10 17:02:48
【问题描述】:

我有一个要求,我想通过 Thymeleaf 使用 React 代码。 我使用 create-react-app 和 npm start 工作正常。没有添加任何其他代码。 这是我的设置- src\main\resources\templates\index.html

<!DOCTYPE HTML>
<html>
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="/css/main.5f361e03.chunk.css" rel="stylesheet">
    <script src="/js/2.b38996be.chunk.js"></script>
    <script src="/js/main.8a01ac9e.chunk.js"></script>
</head>
<body>
  <div class="root"></div>
</body>
</html>

你可以看到我在上面链接了我的 React 和 CSS 构建代码。

src\main\resources\static 这个位置有css、js、媒体文件夹

当我转到http://localhost:8080/ 时,前端是空的。

这是我的 pom 文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.test</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

链接到 Github 项目- https://github.com/kedarnadkarny/springreact

【问题讨论】:

    标签: java reactjs spring spring-boot thymeleaf


    【解决方案1】:

    我相信您的问题,因为在 index.html 中您忘记加载 /static/main-runtime*.js 加载您的 reactjs 应用程序需要该运行时。

    您可以使用这个 maven 插件 https://github.com/maasdi/react-assets-maven-plugin 来自动化这个过程。

    PS:我也遇到了类似的问题,我必须创建插件来解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 2018-06-17
      • 1970-01-01
      相关资源
      最近更新 更多