How to get object using Thymeleaf

Viewed 442

I want to generate User object using Thymeleaf template. How can I do this? I have an User class containing information as follow.

package com.rhv.um.model;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "firstname")
    private String firstName;
    @Column(name = "lastname")
    private String lastName;
    @Column(name = "email")
    private String email;
    @Column(name = "mobile")
    private Long mobile;
    @Column(name = "username")
    private String username;
    @Column(name = "password")
    private String password;
    @Transient
    private String confirmPassword;
    @Column(name = "isenable", columnDefinition = "boolean default 1")
    private boolean isEnable;
    @Column(name = "reason")
    private String reason;
    @ManyToMany
    @JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "roles_id"))
    private Set<Role> roles;

//Getter and Setter
}

I have an UserRepository class and UserService class too. UserService class is as follow. UserRepository class is same as UserService class

package com.rhv.um.service;

import java.util.List;

import com.rhv.um.model.User;

public interface UserService {
    void save(User user, boolean updatePassword);
    List<User> findAll();
    User findByUsername(String username);
}

Now I want to create an object of User from thymeleaf by using findByUsername(String username) method of UserService class. I want to get the details of currently logged-in user from this.

As I am using SpringSecurity5. I can get currently logged in user from thymeleaf as below.

<span th:text="${#authentication.getPrincipal().getUsername()}"></span>

I want to pass this username to my methods and get the User object. Not only the currently logged-in user. I do need to get User object of other users in the system.

I know few ways of doing it as below.

  1. Setting the values of currently logged-in user in cookie and fetch it from UI.
  2. Setting the values of currently logged-in user in session and fetch from UI.
  3. Setting the User object to model attribute and then use the object from UI.

Using above ways, My purpose is getting solved. But I want to set object from thymeleaf. How can I do it?

I had tried by using th:with of thymeleaf. It didn't worked for me

<div th:if="${userCredentials == null}" th:with="userCredentials=${T(your.package.here.UserCredentials).create()}"></div>

Post for above line of code is here

Please let me know if more information is needed. Also guide me with the code if I am doing anything wrong here.

0 Answers
Related