Reputation: 43077
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
Upvotes: 1
Views: 556
Reputation: 25613
As you can find in the src/main/webapp/WEB-INF/views/home.jsp
file at the end, the link showing mapping by header, is using Ajax that sets the FooHeader
to foo
.
This is how the two methods can be called.
If you don't set the header (meaning a simple HTML link), the method byHeaderNegation
gets called, but if you set the Header (using Ajax in this case), then the method byHeader
gets called because FooHeader=foo
is true
$("#byHeader").click(function(){
var link = $(this);
$.ajax({ url: this.href, dataType: "text", beforeSend: function(req) { req.setRequestHeader("FooHeader", "foo"); }, success: function(form) { MvcUtil.showSuccessResponse(form, link); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, link); }});
return false;
});
Upvotes: 3