Home > Mobile >  Unexpected result in JSP when using header model attribute
Unexpected result in JSP when using header model attribute

Time:11-24

I wrote a controller with the following model attributes:

@RequestMapping("/ktSessions")
public String ktSessionsPage(Model model) {
    model.addAttribute("options","option1");
    model.addAttribute("header","header1");
    return "ktSessions";
}

And I added the following JSP:

<nav >
<header>Menu</header>
  <ul>
        <li><a href="#">${options } </a></li>
  </ul>
</nav>

<div >
    <p>Header is <c:out value="${header }"></c:out> </p>
</div>

When I run the applicaiton, I can see the "option1" string in the sidemenu, but in stead of the "header1" value I see this:

{sec-fetch-mode=navigate, referer=http://localhost:8080/SpringMVCProject/emailTemplates?header=Hi Henry,, sec-fetch-site=same-origin, accept-language=en, cookie=JSESSIONID=9BFFE52B821FB30447E53F04442C0B98, sec-fetch-user=?1, accept=text/html,application/xhtml xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9, sec-ch-ua="Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24", sec-ch-ua-mobile=?0, sec-ch-ua-platform="Windows", host=localhost:8080, upgrade-insecure-requests=1, connection=keep-alive, cache-control=max-age=0, accept-encoding=gzip, deflate, br, user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36, sec-fetch-dest=document}

Here's a screenshot of the webpage:

Link for the error

Why do I see this in stead of the "header1" model attribute?

CodePudding user response:

The problem is that the ${header} model is updated to contain the headers sent by the webbrowser (so it's not really an error). The easiest solution is to change the name of the model to something else, for example:

@RequestMapping("/ktSessions")
public String ktSessionsPage(Model model) {
    model.addAttribute("options","option1");
    model.addAttribute("headername","header1"); // Changed to "headername"
    return "ktSessions";
}

Don't forget to change it in your JSP as well:

<p>Header is <c:out value="${headername}"></c:out> </p>
  • Related