Thymeleaf: cannot handling input form Spring Boot

cidalia

I am new in web app development and I want to create a login page for my web app (using Spring boot + Thymeleaf), but I cannot display the form.html when I make a request for /form.

This is my Entity class:

 package com.example.springboot.model;

    public class ApplicationUser {
    private String username;
    private String password;

    public ApplicationUser(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public ApplicationUser() { }

    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;
    }
}

My form.html looks like this:

 <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Form Submission</title>
</head>
<body>
    <h1>User form</h1>
    <form action="#" th:action="@{/form}" th:object="${applicationuser}" method="post">
        <p>Username: <input type="text" th:field="*{username}"/></p>
        <p>Password: <input type="text" th:field="*{password}"/></p>
        <p><input type="submit" value="Send"/></p>
    </form>
</body>
</html>

And the result.html is

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Result</title>
</head>
<body>
    <h1>Result</h1>
    <p th:text="'username: ' + ${applicationuser.username}"/>
    <p th:text="'password: ' + ${applicationuser.password}"/>
    <a href="/form">Submit another</a>
</body>
</html>

But applicationuser from th:object="${applicationuser}" is highlighted in red ("cannot resolve "applicationuser"). Can this be a problem? How to resolve it?

My Controller class is

 @RestController
public class Test {

    @RequestMapping(value = "/form",method = RequestMethod.GET)
    public String userForm(Model model){
        model.addAttribute("applicationuser",new ApplicationUser());
        return "form";
    }

    @RequestMapping(value = "/form",method = RequestMethod.POST)
    public String userSubmit(@ModelAttribute ApplicationUser user,Model model){
        model.addAttribute("applicationuser",user);
        String info = String.format("User Submission: username = %s, password = %s", user.getUsername(),user.getPassword());
        System.out.println(info);
        return "result";
    }
}

I'm just trying to get input form and display them in console, but no way... When I make a request in my browser: http://localhost:8080/form I obtain a white page with follow string "form", it doesn't display the textfields and button. I have in pom.xml the thymeleaf dependency:

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

Does anyone known what is the problem with my code?

Avijit Barua

Seems i got your problem. Change this section

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">  

to

<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:sf="http://www.w3.org/1999/xhtml" lang="en">

And change your

<p>Username: <input type="text" th:field="*{username}"/></p>
<p>Password: <input type="text" th:field="*{password}"/></p>

to

  <p>Username: <input type="text" id="username" name="username"/></p>
  <p>Password: <input type="text" id="password" name="password" /></p>

One more thing, Change you @RestController to @Controller If it works, let me know.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring Boot + Thymeleaf: Bind empty form input to NULL-string

Thymeleaf Spring boot - input Localdate

Spring Boot with Thymeleaf handling DateTime field

Spring Boot 3 and Thymeleaf - object not rendering in the form

Cannot get validation working with Spring Boot and Thymeleaf

Spring boot + thymeleaf in IntelliJ: cannot resolve vars

Spring boot thymeleaf cannot resolve arabic messages

Spring thymeleaf Form url cannot be resolved

handling multiple row submit with checkbox in thymeleaf and spring boot controller

Spring Boot, Spring Security and Thymeleaf: Apply CsrfFilter to website with form

Java : Cannot send a form with Angular 5 that contain an input of type file to a REST backend (Spring Boot)

Thymeleaf form data integration with Spring Boot model attributes

Post form in a th:each section [Spring-Boot + Thymeleaf]

How to set default form values for objects in Spring Boot : Thymeleaf

Spring Boot Ajax post form submit with model and thymeleaf

html form validation using thymeleaf not working spring boot

Thymeleaf + Spring Boot: Object creation with related object (Form & Return)

Spring Boot + Thymeleaf + Tomcat post form replace cyrillic letters with?

Spring boot thymeleaf field passing form inputs between pages

How to submit a hidden string in a form with Spring Boot and Thymeleaf?

Default values in input fields versus text areas in thymeleaf (spring boot)

Spring Boot cannot change Thymeleaf template directory with Java config

spring boot thymeleaf cannot find resources due to URL text

Thymeleaf cannot detect templates inside spring-boot project

Cannot insert date into the Oracle databe in Spring Boot via Thymeleaf

I cannot consume Spring Boot Thymeleaf endpoints with ajax

Thymeleaf form handling returning Null?

Spring Boot - Thymeleaf template

spring boot and thymeleaf