0

I have created a SpringBoot application with thymeleaf template, This application running successfully on Eclipse IDE and Running War file by MAC terminal command:

java -jar ./SpringBootTestMe.war

But problem is that it is not running on external tomcat-9.0, Giving below ERROR:

Sun Jan 26 09:01:37 IST 2020 There was an unexpected error (type=Internal Server Error, status=500). Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "index" - line 13, col 28)

My source code are

File pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.spboot</groupId>
    <artifactId>SpringBootTestMe</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootTest</name>
    <packaging>war</packaging>

    <!-- lookup parent from repository -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <start-class>com.spboot.web.TestApp</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

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

</project>

SpringBootMainClass

package com.spboot.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class TestApp extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(TestApp.class, args);
        System.out.println("--------AppStarted--------");
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(TestApp.class);
    }
}

HomeController

@Controller
@RequestMapping("SpringBootTestMe")
public class HomeController {

    @RequestMapping(value={"", "login"})
    public String login(UserRecord userRecord) {
        System.out.println("HomeController Called...");
        return "index";
    }
}

ModalClass

public class UserRecord implements Serializable {

    private Long id;
    private String username;
    private String email;

    //Setters and getters
}

index.html

<!DOCTYPE html>
<html xmlns:th="https://thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Login Page</title>
</head>
<body>
<div>
    <form action="#" th:action="@{/login}" th:object="${userRecord}" method="post">
        <table>
            <tr>
                <td><label for="username">User Name</label></td>
                <td><input type="text" th:field="*{username}" /></td>
                <td th:if="${#fields.hasErrors('username')}" th:errors="*{username}">Username Error</td>
            </tr>
            <tr>
                <td><label for="email">Email</label></td>
                <td><input type="text" th:field="*{email}"></input></td>
                <td th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Name Error</td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit"></input></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>

It's giving error on username text field, tomcat not recognising the th:field Can anybody help me please.

Thanks

Vy Do
  • 46,709
  • 59
  • 215
  • 313

0 Answers0