I am developing a Java Spring Boot Web App, and on one of the app's pages ("profile.jsp"), I am trying to display part of the current logged-in user's information (their email). This is my first time developing using Java Spring Boot, however, and so I'm struggling with using the Model Attribute capability to retrieve the user's email. Below is the profile.jsp page:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<c:url var="editAccount" value="/editAccount" />
<div id="profileAbout">
<div class="row">
<form:form modelAttribute="user">
<div style="justify-content: center; align-items: center;">
<div style="text-align: center; font-weight: bold; font-size: x-large; margin-bottom: 2%; margin-top: 2%;">Username: ${user.email}</div>
<div style="text-align:center;">
<a href="${editAccount}" id="passwordReset">Reset Password</a>
</div>
</div>
</form:form>
</div>
</div>
And here is the function in the controller:
@RequestMapping(value="/profile")
public ModelAndView profile(ModelAndView modelAndView, @ModelAttribute("user") SiteUser user) {
SiteUser userVal = getUser();
Profile profile = profileService.getUserProfile(userVal);
if(profile == null) {
profile = new Profile();
profile.setUser(userVal);
profileService.save(profile);
}
Profile webProfile = new Profile();
webProfile.safeCopyFrom(profile);
modelAndView.getModel().put("profile", webProfile);
modelAndView.getModel().put("user", user);
modelAndView.setViewName("app.profile");
return modelAndView;
}
And then here you can see finally that the email(username) is not actually displaying, which I'm unsure of as to why this is happening. If I put a string in modelAndView.getModel.put("user", "RANDOM STRING");, this will actually display, but for some reason I'm not actually retrieving the object of the user. Any help as to why the email isn't displaying would be appreciated, thank you.
Also, here is the profile entity:
@Entity
@Table(name="Profile")
public class Profile {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="profileId")
private long profileId;
@OneToOne(targetEntity=SiteUser.class)
@JoinColumn(name="user_id", nullable=false)
private SiteUser user;
@Column(name="about", length=1000)
@Size(max=1000, message="{edit.profile.about.size}")
private String about;
...
}
And the SiteUser entity:
@Entity
@Table(name="Users")
@PasswordMatch(message="{register.repeatPassword.mismatch}")
public class SiteUser {
@Id
@Column(name="userId")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long userId;
@Column(name="email", unique=true)
@Email(message="{register.email.invalid}")
@NotBlank(message="{register.email.invalid}")
private String email;
...
}
