javaspringspring-bootmodelattribute

Java Spring Boot: Retrieving Model Attribute


I'm developing a Java Spring Boot Web App and am having trouble retrieving a model attribute that I want to display on one of my pages. The page is "/profile" and here is the profile.jsp page:

<c:url var="editAccount" value="/editAccount" />
   
<div id="profileAbout">

    <div class="row">
    
        <form:form modelAttribute="user">
    
            <div class="col-md-10 col-md-offset-1">
                    
                <div style="text-align: center; font-weight: bold; font-size: x-large; margin: 1%;">Username: <c:out value="${user.email}" /></div>
    
            </div>
                
            <div class="profile-about-edit">
                <a href="${editAccount}">Edit</a>
            </div>
        
        </form:form>
        
    </div>
    
</div>

Below this is the controller method:

    @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.setViewName("app.profile");
        
        return modelAndView;
    }

So, as you can see in profile.jsp, I'm trying to display user.email, but am unable to for some reason. I have this working for other model attributes, but am unsure what to do with this one. I've also attached a picture just to illustrate my issue:profile

Here is the entity for Profile:

@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 here is the entity for the SiteUser:

@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;

    ...
}

Solution

  • Why do you need a form if what you want is to display data? I’m assuming all the editing will be done on page /edit-profile.

    Also your model attribute for this page seems to be profile, not user.

    Taking all of the above into account, I think your page should look like this:

    <c:url var="editAccount" value="/editAccount" />
    <div id="profileAbout">
        <div class="row">
            <div class="col-md-10 col-md-offset-1">
                <div style="text-align: center; font-weight: bold; font-size: x-large; margin: 1%;">Username: <c:out value="${profile.user.email}" /></div>
                </div>
                <div class="profile-about-edit">
                    <a href="${editAccount}">Edit</a>
                </div>
                </div>
         </div>
    </div>