Hi I am currently working on a Spring Boot Web Application which provides a Project Management Dashboard for Authenticated Users. The app currently consists of 3 main entity classes: User, Role and Project. Both Role and Project have a ManyToMany relationship with the class User. I am using Thymeleaf to display object data within my HTML template but it does not seem to pull my data correctly from my controller.
Whenever, I call all user details and match the current user to the data row it works:
<div th:each="allUser:${allUsers}">
<div th:if="${#authentication.getName() == allUser.username}">
<span class="d-none d-lg-inline text-gray-600 small" th:text="${allUser.firstName}"></span>
<span class="mr-4 d-none d-lg-inline text-gray-600 small" th:text="${allUser.lastName}"></span>
</div>
</div>
However, when getting the current user in my controller it doesn't to work:
<div th:each="user:${users}">
<div th:if="${#authentication.getName() == user.username}">
<span class="d-none d-lg-inline text-gray-600 small" th:text="${user.firstName}"></span>
<span class="mr-4 d-none d-lg-inline text-gray-600 small" th:text="${user.lastName}"></span>
</div>
</div>
Additionally, I am trying to display project information for projects assigned to the current user but that also does not print anything to the dashboard:
<tr th:each="project:${projects}">
<td th:text="${project.pxprojectname}"></td>
<td th:text="${project.initialpendingid}"></td>
<td th:text="${project.currentpendingid}"></td>
<td th:text="${project.completionstatus}"></td>
<td th:text="${project.manager}"></td>
</tr>
My code can be found below:
User Class
@Entity
@Table(name = "pxuser")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "tabid")
private int tabid;
@Column(name = "username")
private String username;
@Column(name = "firstname")
private String firstName;
@Column(name = "lastname")
private String lastName;
@Column(name = "password")
private String password;
@Column(name = "active")
private boolean active;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "pxuser_role", joinColumns = @JoinColumn(name = "tabid"), inverseJoinColumns = @JoinColumn(name = "auth_role_id"))
private Set<Role> roles = new HashSet<>();
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "pxassignment", joinColumns = @JoinColumn(name = "usertabid"), inverseJoinColumns = @JoinColumn(name = "pxprojectid"))
private Set<Project> projects = new HashSet<>();
public User() {
}
public User(User user) {
this.tabid = user.getTabid();
this.username = user.getUsername();
this.firstName = user.getFirstName();
this.lastName = user.getLastName();
this.password = user.getPassword();
this.active = user.isActive();
this.roles = user.getRoles();
this.projects = user.getProjects();
}
public int getTabid() {
return tabid;
}
public void setTabid(int tabid) {
this.tabid = tabid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public Set<Project> getProjects() {
return projects;
}
public void setProjects(Set<Project> projects) {
this.projects = projects;
}
}
Project Class
@Entity
@Table(name = "pxproject")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "pxprojectid")
private String pxprojectid;
@Column(name = "pxprojectname")
private String pxprojectname;
@Column(name = "servername")
private String servername;
@Column(name = "initialpendingid")
private int initialpendingid;
@Column(name = "currentpendingid")
private int currentpendingid;
@Column(name = "completionstatus")
private String completionstatus;
@Column(name = "isactive")
private boolean isactive;
@Column(name = "sfengagementid")
private String sfengagementid;
@Column(name = "manager")
private String manager;
@ManyToMany(mappedBy = "projects")
private Set<User> user = new HashSet<>();
public Project() {
}
public Project(String pxprojectid, String pxprojectname, String servername, int initialpendingid, int currentpendingid, String completionstatus, boolean isactive, String sfengagementid, String manager, Set<User> users) {
this.pxprojectid = pxprojectid;
this.pxprojectname = pxprojectname;
this.servername = servername;
this.initialpendingid = initialpendingid;
this.currentpendingid = currentpendingid;
this.completionstatus = completionstatus;
this.isactive = isactive;
this.sfengagementid = sfengagementid;
this.manager = manager;
this.user = user;
}
public String getPxprojectid() {
return pxprojectid;
}
public void setPxprojectid(String pxprojectid) {
this.pxprojectid = pxprojectid;
}
public String getPxprojectname() {
return pxprojectname;
}
public void setPxprojectname(String pxprojectname) {
this.pxprojectname = pxprojectname;
}
public String getServername() {
return servername;
}
public void setServername(String servername) {
this.servername = servername;
}
public int getInitialpendingid() {
return initialpendingid;
}
public void setInitialpendingid(int initialpendingid) {
this.initialpendingid = initialpendingid;
}
public int getCurrentpendingid() {
return currentpendingid;
}
public void setCurrentpendingid(int currentpendingid) {
this.currentpendingid = currentpendingid;
}
public String getCompletionstatus() {
return completionstatus;
}
public void setCompletionstatus(String completionstatus) {
this.completionstatus = completionstatus;
}
public boolean isIsactive() {
return isactive;
}
public void setIsactive(boolean isactive) {
this.isactive = isactive;
}
public String getSfEngagementid() {
return sfengagementid;
}
public void setSfEngagementid(String sfengagementid) {
this.sfengagementid = sfengagementid;
}
public String getManager() {
return manager;
}
public void setManager(String manager) {
this.manager = manager;
}
public Set<User> getUser() {
return user;
}
public void setUser(Set<User> user) {
this.user = user;
}
}
Role Class
@Entity
@Table(name = "pxrole")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "auth_role_id")
private int auth_role_id;
@Column(name = "role_name")
private String role_name;
@Column(name = "role_desc")
private String role_desc;
public Role() {
}
public int getAuth_role_id() {
return auth_role_id;
}
public void setAuth_role_id(int auth_role_id) {
this.auth_role_id = auth_role_id;
}
public String getRole_name() {
return role_name;
}
public void setRole_name(String role_name) {
this.role_name = role_name;
}
public String getRole_desc() {
return role_desc;
}
public void setRole_desc(String role_desc) {
this.role_desc = role_desc;
}
}
Controller
@Controller
public class HomeController {
@Autowired
ProjectRepository projectRepository;
@Autowired
ProjectService projectService;
@Autowired
UserRepository userRepository;
@Autowired
CustomUserDetailsService customUserDetailsService;
@GetMapping("/")
public String index() {
return "index";
}
@GetMapping("/dashboard")
public String dashboard(Model model, String currentUser) {
List<Project> projectList = projectRepository.findByUser_Username(currentUser);
List<User> userList = customUserDetailsService.getAllUsersDetails();
User user = userRepository.findByUsername(currentUser);
model.addAttribute("projects", projectList);
model.addAttribute("allUsers", userList);
model.addAttribute("users", user);
return "index";
}
@GetMapping("/admin")
public String admin() {
return "admin";
}
// Login form
@GetMapping("/login")
public String login() {
return "login";
}
@GetMapping("/logout")
public String logout() {
return "login?logout";
}
@GetMapping("/dashboard/projects")
public String projects() {
return "projects";
}
@GetMapping("/error")
public String error() {
return "error";
}
}
User Repository
public interface UserRepository extends JpaRepository<User, Integer>{
public User findByUsername(String username);
}
Project Repository
public interface ProjectRepository extends JpaRepository<Project, Integer>{
List<Project> findByUser_Username(String username);
}
Custom User Details Service
@Service
public class CustomUserDetailsService implements UserDetailsService{
@Autowired
UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("Invalid username or password.");
}
return new CustomUserDetails (user);
}
public List<User> getAllUsersDetails(){
return userRepository.findAll();
}
}
Project Service
@Service
public class ProjectService {
@Autowired
private ProjectRepository projectRepository;
//Return List of All Projects
public List<Project> getProjects(){
return projectRepository.findAll();
}
//Return List of Projects by Username
public List<Project> getProjectsByUserUsername(String username){
return projectRepository.findByUser_Username(username);
}
}
Any help is much appreciated! Thanks!!!
In the controller for the GetMapping("/dashboard") currentUser variable holds the value null,
hence User user = userRepository.findByUsername(currentUser)
;
-> outputs the optional object.
List<Project> projectList = projectRepository.findByUser_Username(currentUser);
-> outputs the emptyList.
therefore unable to display the data.
by using the Authentication or Principle object we can take out the current logged in user.