Getting org.thymeleaf.exceptions.TemplateProcessingException in spring boot

27 views Asked by At

I am new to spring boot. So please don't mind if the question is old.

Using spring boot, I am trying to enter some user details into the DB and then fetch the same.

Below is the code for my controller class:

@GetMapping("/")
public String home() {
    return "home";
}
@PostMapping("/register")
public String empRegister(@ModelAttribute EmpProfile emp, Model model) {
    
    System.out.println(emp.toString());
    
    System.out.println(emp.getFirstName());
    System.out.println(emp.getLastName());
    System.out.println(emp.getUnit());
    
    model.addAttribute("firstName", emp.getFirstName());
    model.addAttribute("lastName", emp.getLastName());
    model.addAttribute("unit", emp.getUnit());

    
    empRepo.save(emp);
    
    return "saveEmp";
}

Below is the code of the home.html file:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home Page</title>
</head>
<body>
    <form action="register" method="post">
        <label for="firstName">First name :</label> <br/>
        <input type="text" name="firstName"/> <br/>
    
        <label for="lastName">Last name :</label> <br/>
        <input type="text" name="lastName"/> <br/>
        
        <label for="unit">Unit :</label> <br/>
        <input type="text" name="unit"/> <br/>
        <button type="submit">Submit</button>
    
    </form>
</body>
</html>

After I click on the submit button, it is properly inserting the row into the DB. But then it is throwing the following exception:

2024-02-18 18:30:53.741 ERROR 26208 --- [nio-8080-exec-3] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-3] Exception processing template "saveEmp": Exception evaluating SpringEL expression: "emp.firstName" (template: "saveEmp" - line 14, col 37)

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "emp.firstName" (template: "saveEmp" - line 14, col 37)
    at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:292) ~[thymeleaf-spring5-3.0.15.RELEASE.jar:3.0.15.RELEASE]
    at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166) ~[thymeleaf-3.0.15.RELEASE.jar:3.0.15.RELEASE]
    at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66) ~[thymeleaf-3.0.15.RELEASE.jar:3.0.15.RELEASE]

On the browser I am getting the following message:

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Feb 18 18:30:53 IST 2024
There was an unexpected error (type=Internal Server Error, status=50

Actually I am wanting to display the fields which got inserted on the GUI also. Below is my code of the saveEmp.html file:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1> Welcome page</h1>

    <!-- First name = <h2 th:text="${firstname}"></h2>
    Last name = <h2 th:text="${lastname}"></h2>
    Email = <h2 th:text="${email}"></h2> -->
    
    <span>First name = </span><b><span th:text="${emp.firstName}"></b></span>
    <span>Last name = </span><b><span th:text="${emp.lastName}"></b></span>
    <span>Email = </span><b><span th:text="${emp.unit}"></b></span>
</body>
</html>

Can anyone please tell what is going wrong here? What is missing here?

0

There are 0 answers