Ltcs
Ltcs

Reputation: 283

To get username in all the session

I want it to do, it is very simple but the result is not like I want.

I want to show the username in all app when the user are logged, but I show the username in the first page.

My code is:

Controller

...    
@SessionAttributes("username")
public class InitController
{
@RequestMapping(.. RequestMethod.GET)
public String request (ModelMap model, HttpServletRequest request)
{
   ...
   //model.addAttribute ("username", name);
   request.setAttribute("username", name);
   ...
   return "pageToShow";


 }
}

Page .jsp

<header>
    <div id="divHeader">
        <h1><fmt:message key="page.header"/></h1>
        <div class="divUser">
            <a href="direction?id=${username}" >${username}</a>
        </div>
    </div>
</header>

The .jsp page is always present because it´s header

Thanks.

Upvotes: 0

Views: 571

Answers (1)

Alexey
Alexey

Reputation: 2592

Replace

request.setAttribute("username", name);

with

model.addAttribute ("username", name);

and replace

<a href="direction?id=${username}" >${username}</a>

with

<a href="direction?id=${sessionScope.username}" >${sessionScope.username}</a>

When you use ${username} you print the username MVC model attribute which exists only till the end of the current request. ${sessionScope.username} will take username attribute from the session attributes where it was saved by your @SessionAttributes("username") annotation.

Upvotes: 1

Related Questions