How to display a Java variable onto ThymeLeaf HTML using the span tag?

I’m currently writing a project that incorporates the use of ThymeLeaf HTML for the frontend and Java for the backend. I’m trying to display a variable from one of my Java controller classes onto my HTML page using ThymeLeaf. The variable is essentially the username and it’s only to be displayed once the user logs in and is redirected back to the main page. The error that I’m getting here is that it can’t resolve username. So, the variable is clearly not being read in my ThymeLeaf HTML file for some reason. The syntax seems to be right based on what I’ve read from the ThymeLeaf documentation. And I’ve been told that it should be working, but it’s not working for whatever reason. Anyways, I’m wondering what is causing this issue to occur because I’m not sure why it’s not being read in the first place? It keeps telling me that it can’t resolve username despite the fact that I have the variable in my controller class. The code doesn’t have any errors as far as my controller class goes. The code is posted down below which includes the ThymeLeaf HTML page where I’m trying to display the username if the user logs in. And the second part is the controller class for the user login.

  <div class="d-flex justify-content-between align-items-center mb-3">
    <h1>Auto Parts</h1>
        <a th:href="@{/login}" class="btn btn-primary btn-sm mb-3">Login</a>
    <a th:href="@{/about}" class="btn btn-primary btn-sm mb-3" >About Us</a>
        <span th:if="${username}" class="ms-3">Welcome, <span th:text="${username}">User</span>!</span>

    </div>
@Controller
public class LoginController {

    private static final String HARDCODED_USERNAME = "Carter";
    private static final String HARDCODED_PASSWORD = "Password907";

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, Model model) {


        if (username.equals(HARDCODED_USERNAME) && password.equals(HARDCODED_PASSWORD)) {
            model.addAttribute("username", username);
            return "redirect:/mainscreen";
        } else  {
          model.addAttribute("loginError", true);
            return "login";
        }
    }
    // Mapping for mainscreen page
    @RequestMapping("/mainscreen")
    public String mainscreen(Model model) {
        return "mainscreen"; // returns mainscreen.html
    }

}

I’ve tried to just display the variable without using the if statement. And this didn’t work either because it somehow won’t read the variable. I’ve tried changing my model attribute name and it didn’t work either. Nothing I did worked and I can’t seem to figure out why this is an issue for me because it should be working without any issues.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật