Search code examples
javaintellij-ideathymeleaf

Thymeleaf Exception evaluating SpringEL


I'm a novice just learning using intellij idea and Thymeleaf,

Why can't I find out how to fix this problem how ever I try.

UserController.java

package com.test01.controller;

import com.test01.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {
    @GetMapping("/register")
    public String doDefault(){
        return "register";
    }

    @PostMapping("/register")
    public String register(User user) {
        return "success";
    }
}

User.java

package com.test01.model;

import lombok.AllArgsConstructor;
import lombok.Data;

@AllArgsConstructor
@Data
public class User {
    private Integer id;
    private String username;
    private String password;
}

register.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>register</title>
</head>
<body>
<form th:action="@{/user/register}" method="post">
  <table border="0">
    <tr>
      <td>username:</td>
      <td><input type="text" name="username"/></td>
    </tr>
    <tr>
      <td>password:</td>
      <td><input type="password" name="password" /></td>
    </tr>
    <tr>
      <td><input type="submit" value="submit" /></td>
      <td><input type="reset" value="reset"></td>
    </tr>
  </table>
</form>
</body>
</html>

success.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>success</title>
</head>
<body th:object="${user}">
<h3>[[*{usesrname}]],success! </h3>
<table border="0">
  <caption>Your registration information is</caption>

  <tr>
    <td>username:</td>
    <td th:text="*{username}"></td>
  </tr>

  <tr>
    <td>password:</td>
    <td th:text="*{password}"></td>
  </tr>
</table>

</body>
</html>

enter image description here

enter image description here

enter image description here

Cannot resolve 'username' Cannot resolve 'password'


Solution

  • package com.example;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class TestProgramApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(TestProgramApplication.class, args);
        }
    
    }
    

    User.java

    package com.example;
    
    public class User {
        private Integer id;
        private String username;
        private String password;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        
        
    }
    

    UserController.java

    package com.example;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    
    @Controller
    public class UserController {
    
        @GetMapping("/register")
        public String doDefault() {
            return "register";
        }
    
        @PostMapping("/user/register")
        public String register(User user) {
            return "success";
        }
    }
    

    register.html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <title>register</title>
    
    <head>
        <meta charset="ISO-8859-1">
        <title>Insert title here</title>
    </head>
    
    <body>
    
    
        <form th:action="@{/user/register}" method="post">
            <table border="0">
                <tr>
                    <td>username:</td>
                    <td><input type="text" name="username" /></td>
                </tr>
                <tr>
                    <td>password:</td>
                    <td><input type="password" name="password" /></td>
                </tr>
                <tr>
                    <td><input type="submit" value="submit" /></td>
                    <td><input type="reset" value="reset"></td>
                </tr>
            </table>
        </form>
    
    
    </body>
    
    </html>
    

    success.html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <title>success</title>
    
    <head>
        <meta charset="ISO-8859-1">
        <title>Insert title here</title>
    </head>
    
    <body th:object="${user}">
        <h3>[[*{username}]],success! </h3>
        <table border="0">
            <caption>Your registration information is</caption>
    
            <tr>
                <td>username:</td>
                <td th:text="*{username}"></td>
            </tr>
    
            <tr>
                <td>password:</td>
                <td th:text="*{password}"></td>
            </tr>
        </table>
    
    </body>
    
    </html>
    

    pom.xml

    <?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>3.1.2</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>TestProgram</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>TestProgram</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>17</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-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    register page register page

    success page success page

    project structure

    project structure