Search code examples
springhibernatedeploymentspring-data-jpathymeleaf

template might not exist or might not be accessible by any of the configured Template Resolvers after deploy


For some reason, after the deployment, I started to give a 500 error when saving (that is, the post fulfills the request, but reloading the same page already by get causes an error. And the first time the get request this page opens to show the filling form). Although everything works well on the local computer. I ask for help!

My pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>ru.asu</groupId>
    <artifactId>pdn</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>pdn</name>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <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-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
            <version>3.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <!-- JAXB -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>6.0.8</version>
            </plugin>
        </plugins>
    </build>

</project>

My Controller:

@GetMapping("/new_violation")
public String showNewViolationForm(Model model) {
    Violation violation = new Violation();
    model.addAttribute("violation", violation);
    return "new_violation";
}

@PostMapping("/new_violation")
public String saveViolation(
        @Valid @ModelAttribute Violation violation,
        BindingResult bindingResult,
        Model model
) {
    if (bindingResult.hasErrors()) {
        Map<String, String> errorsMap = getErrors(bindingResult);
        model.mergeAttributes(errorsMap);
        model.addAttribute("violation", violation);
    } else {
        violationService.save(violation);
    }
    return "/new_violation";
}

And some of my Thymeleaf:

<tr class="text-center">
        <form action="#" method="post" th:action="@{/new_violation}" th:object="${violation}">
            <div>
                <th>
                    <label>
                        <input th:field="*{numProtocol}" type="text"/>
                    </label>
                    <span class="form-control is-invalid" th:errors="*{numProtocol}"
                          th:if="${#fields.hasErrors('numProtocol')}">
                    </span>
                </th>
                <th>
                    <label>
                        <input th:field="*{dateProtocol}" type="date"/>
                    </label>
                    <span class="form-control is-invalid" th:errors="*{dateProtocol}"
                          th:if="${#fields.hasErrors('dateProtocol')}">
                    </span>
                </th>
                <th>
                    <label>
                        <input th:field="*{violationAddress}" type="text"/>
                    </label>
                    <span class="form-control is-invalid" th:errors="*{violationAddress}"
                          th:if="${#fields.hasErrors('violationAddress')}">
                    </span>
                </th>
                <th>
                    <div>
                        <label>
                            <input th:field="*{child.fio}" type="text"/>
                        </label>
                        <span class="form-control is-invalid" th:errors="*{child.fio}"
                              th:if="${#fields.hasErrors('child.fio')}">
                        </span>
                    </div>
                </th>
                <th>
                    <label>
                        <input th:field="*{child.address}" type="text"/>
                    </label>
                    <span class="form-control is-invalid" th:errors="*{child.address}"
                          th:if="${#fields.hasErrors('child.address')}">
                    </span>
                </th>

                <th>
                    <button class="btn btn-primary bg-danger" type="submit">Сохранить</button>
                </th>
            </div>
        </form>

Solution

  • Looks like there is a typo in your return statement.

    Instead of

    return "/new_violation";
    

    try this:

    return "new_violation";
    

    As the same appears in your get mapping. I am assuming the same should be in your post mapping too.