as far as I am a big fan of division frontend and backend I would like to transform my login form from Thymeleaf to REST solution. Could anyone help me with it? I got a code as following:

public class LoginController {

public String login () {
    return "login";

and my form:

<!DOCTYPE html>
<html xmlns:th="">
<form action="" method="post">
    <legend>Please Login</legend>
    <!-- use param.error assuming FormLoginConfigurer#failureUrl 
 contains the query parameter error -->
    <div th:if="${param.error != null}">
        Failed to login.
        <div th:if="${SPRING_SECURITY_LAST_EXCEPTION != null}">
            Reason: <span
    <!-- the configured LogoutConfigurer#logoutSuccessUrl is /login?logout and contains the query param logout -->
    <div th:if="${param.logout != null}">You have been logged out.</div>
        <label for="username">Username</label> <input type="text"
                                                      id="username" name="username" />
        <label for="password">Password</label> <input type="password"
                                                      id="password" name="password" />
        <button type="submit" class="btn">Log in</button>

My idea is to transform a controller into the REST one and than use AJAX to post the JSON with username and password. Is it a good solution? Also to which address should I send it?

1 Answers

Abhijeet On

Well, it's not a good approach to use thymeleaf with REST controller. Because your architecture be like, you will have one client application which calls your Server application consist of REST controller. So your design be like following

(Client Application) [thymeleaf pages -> client controller -> client service ] => [REST controller -> Service -> repository] (Server Application)

You can refer code here if you want to go with this approach

Else you can go with a different approach in which you will have Server application with REST controller and client application with your client code with your HTML page, AJAX calls and navigation logic. This approach will be like, your client code will be in Angular/React like framework and your server-side application will have your controllers only