skip to Main Content

I am trying to print the username of the logged in person on my webpage and it is not printing the username. It just says Welcome,. I receive no errors in my console or on the web browser console

Do I need to use the Principal class instead or try to retrieve the username from somewhere else? What else could I be missing? Sorry if the questions aren’t so great I am still learning on what proper questions to ask.

Here is my method also my UserModel class does have the proper getters and setters.

    @GetMapping("/products")
    public String getProducts(Model model, UserModel userModel) {
        List<ProductModel> products = productService.getAllProducts();
        model.addAttribute("products", products);
        model.addAttribute("person", userModel.getUsername());
        return "products";
    }

Here is my HTML code

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Products</title>
    <!-- Add any necessary CSS or JavaScript files -->
</head>
<body>
    <h1>Products</h1>
    <h2>Welcome, <span th:text="${userModel.Username}"></span></h2>
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Price</th>
                <th>Availability</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <!-- Use Thymeleaf to iterate over the list of products -->
            <tr th:each="product : ${products}">
                <td th:text="${product.name}"></td>
                <td th:text="${product.price}"></td>
                <td th:text="${product.availability}"></td>
                <td>
                    <a th:href="@{/productDetails/{id}(id=${product.id})}">Details</a> |
                    <a th:href="@{/updateProduct/{id}(id=${product.id})}">Update</a> |
                    <a th:href="@{/deleteProduct/{id}(id=${product.id})}">Delete</a>
                </td>
            </tr>
        </tbody>
    </table>
    
    <p><a th:href="@{/createProduct}">Create Product</a></p>
</body>
</html>

Edit:
Also when I run in debug mode and put a break on model.addAttribute("person", userModel.getUsername()); it tells me 0x000000080036c000.invokeVirtual(Object, Object, Object, Object) line: not available

2

Answers


  1. Chosen as BEST ANSWER

    I ended up going a different route and found the HttpSession class and was able to implement that into my code since I do not need beefed up security features yet like the Principal class provides. My loginUser method was also not storing the name of the user like it should of. Here is the code I implemented.

        public String getProducts(Model model, HttpSession session) {
            
            if (session.getAttribute("loggedInUser") == null) {
                return "redirect:/login";
            }
            
            UserModel user = (UserModel) session.getAttribute("loggedInUser");
            List<ProductModel> products = productService.getAllProducts();
            model.addAttribute("products", products);
            model.addAttribute("person", user.getFirstName());
            System.out.println(user);
            return "products";
        }
    

  2. In html you need to access username using the same attribute name you added the username to in the controller. i.e.

    Replace

    <h2>Welcome, <span th:text="${userModel.Username}"></span></h2>
    

    With

    <h2>Welcome, <span th:text="${person}"></span></h2>
    

    Also make sure userModel.getUsername() in the controller has a value. If it doesn’t you can add a dummy username to confirm it’s passed to html. Eg

    Replace

    model.addAttribute("person", userModel.getUsername());
    

    With

    model.addAttribute("person", "John Doe");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search