In this period I am studing the Spring MVC Showcase example dowlodable from the STS dashboard
I have a doubt related to the functioning of the HTTP Request mapping by header.
In the showcase example I have the following 2 links:
<li>
<a id="byHeader" href="<c:url value="/mapping/header" />">By presence of header</a>
</li>
<li>
<a id="byHeaderNegation" class="textLink" href="<c:url value="/mapping/header" />">By absence of header</a>
</li>
So, as you can see, clicking on the first link I am creating an HTTP request toward the “/mapping/header” folder and clicking on the second link I am creating an HTTP request toward the same “/mapping/header” folder
Now these HTTP requests are managed by two methods in my controller class.
The first one is handled by the following method:
@RequestMapping(value="/mapping/header", method=RequestMethod.GET, headers="FooHeader=foo")
public @ResponseBody String byHeader() {
System.out.println("Sono dentro byHeder()");
return "Mapped by path + method + presence of header! (MappingController)";
}
And the second one is handled by the following method:
@RequestMapping(value="/mapping/header", method=RequestMethod.GET, headers="!FooHeader")
public @ResponseBody String byHeaderNegation() {
System.out.println("Sono dentro byHeaderNegation");
return "Mapped by path + method + absence of header! (MappingController)";
}
I can not understand why the two requests are handled by two different methods since both links will generate an HTTP request towards the same folder: “/mapping/header”
Why the first one have header and the second one have not?
I’m probably missing something, can you help me to understand this thing?
Thank you very much
Andrea
As you can find in the
src/main/webapp/WEB-INF/views/home.jspfile at the end, the link showing mapping by header, is using Ajax that sets theFooHeadertofoo.This is how the two methods can be called.
If you don’t set the header (meaning a simple HTML link), the method
byHeaderNegationgets called, but if you set the Header (using Ajax in this case), then the methodbyHeadergets called becauseFooHeader=fooistrue