-2

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;

    ...
}
CDA the Programmer
  • 87
  • 1
  • 6
  • 20

5 Answers5

0

Try this. You should access it using profile.

${profile.email} 
Supun Dharmarathne
  • 1,138
  • 1
  • 10
  • 19
0

Well, looks like Profile and SiteUser are semantically very tight, you really should consider having only 1 bean with the information of both Profile and SiteUser.

Having said that, you can create a new class let's say ProfilePageModel with the information of both classes and use that in your view.

FrancescoM
  • 1,400
  • 11
  • 16
0

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>
Konrad Botor
  • 4,765
  • 1
  • 16
  • 26
0
    @RequestMapping(value = "/profile")
    public ModelAndView profile(final ModelAndView modelAndView, @ModelAttribute("user") final SiteUser user) {

At least when above method handles GET request, user is just instantiated and email is null.

You need fill user's properties to display ${user.email}.

see also:

0

Can you try adding modelAndView.getModel().put("user", webProfile.getUser());

Or you can try by adding <form:form modelAttribute="profile"> and when you are displaying it use ${profile.user.email}