0

How to get User Object details in JSP page? Below code works fine in Spring Framework 3 but gives error in Spring 4.

Code Block

<span class="header-label">${pageContext.request.userPrincipal.principal.firstName} ${pageContext.request.userPrincipal.principal.lastName}</span>

Error given in Spring 4 (Eclipse IDE) although works fine when deployed

Multiple annotations found at this line:
    - The method getPrincipal() is undefined for the type 
     Principal
    - javax.servlet.jsp.PageContext cannot be resolved to a 
     type
    - javax.servlet.jsp.JspException cannot be resolved to a 
     type
Harinder
  • 116
  • 1
  • 1
  • 11

1 Answers1

0

There is an error in your expression. At best, with the help of some javadocs (HttpServletRequest and Principal), the expression could be:

${pageContext.request.userPrincipal.name}

You can try this.

But a Principal is not the exact equivalent of a user. It's more of an authentication perspective of a Subject (which is roughly the user). It represents one of the means a subject has to sign in. For all we know, the name property could be the username or a credit card number.

It's really unlikely you'll get the first and last names from the principal though.

Arthur Noseda
  • 2,534
  • 19
  • 28
  • We are using custom user detail service for spring which retrieves all the attributes required for the custom UserDetails object. So i need to get the values using Principal which is equivalent to User in our case... – Harinder Aug 04 '16 at 19:18
  • Maybe these links can help: http://stackoverflow.com/questions/248562/when-using-spring-security-what-is-the-proper-way-to-obtain-current-username-i and http://stackoverflow.com/questions/8764545/how-to-get-active-users-userdetails – Arthur Noseda Aug 04 '16 at 19:47