I have develop this example basing uppon the Spring MVC Showcase example.
The result I want to achieve is as follows: a web application that manage 2 kinds of HTTP Request:
FIRST HTTP Request type:: HTTP request towards resources having a structure [/b]/folder[/b]
SECOND HTTP Request type: HTTP request towards resources having a structure [/b]/file.html[/b]
The showcase example manage by default the first type of HTTP Request, so I have add an other DispatcherServlet in my web.xml configuration file to manage the HTTP Request towards *.html pattern but I am finding some problem to handle the second type of request
So this is my web.xml code:
As you can see in this web.xml fine I have defined 2 DispatcherServlet: the first one that manage the first type of HTTP Request and the second one that manage the second type.
Ok, the DispatcherServlet that manage the HTTP Request towards resources having form: file.html have servlet-name=appServlet2 and it is configured by the servlet2-context.xml configuration file.
This is the code of servlet2-context.xml:
this is a "copy" of the servlet-context.xml file that configure the original DispatcherServlet of the example, the only difference, made by me, is that I import a different file: controller2.xml
This file simply specify the package in wich are actived the component scan managed by this servlet:
So, for this servlet is actived the component scan that search annotation in the following package: org.springframework.samples.mvc2.simple
Ok...In this package I have created the following controller class:
As you can see the sayHello method is annoted by @RequestMapping("/hello") annotation and, in theory, have to manage HTTP Request towards /hello.html file !!!
But don't work well !!!
Adding in my home.jsp view I add these links:
When I click on these links I have the following error message:
HTTP Status 404
- description The requested resource is not available.
And this is my stacktrace:
So it is clear that Spring have loaded the appServlet2 DispatcherServlet (the DispatcherServlet that is configured to manage HTTP Request toward filename.html resources) but don't find a mapping for the hello.html resources...
If I change the @RequestMapping("/hello") annotation in @RequestMapping("/hello.html") for my method (inside my HelloController class) the example run well !!!
Using the "/hello.html" pattern inside the @RequestMapping annotation, Spring find the mapping with my sayHello() method and all run well !!!
Infact this is the relative stack trace (the HTTP reuqest is is properly managed by appServlet2 DispatcherServlet and the sayHello() method is called)
I think that this behavior is very very strange because the appServlet2 is configured to handle automatically HTTP request toward resources shaped as: "filename.html" because I have mapped this kind of resources in my web.xml file by the tag:
But if I don't specify that the .html in the @RequestMapping annotation don't work...
The strange thing is that I have created an other simplify Spring project (outside the Spring MVC Showcase Project) and in this one I have not this mapping problem...
Why? Someone can help me to understand this thing? I am going crazy
Prasad Krishnegowda wrote:whenever a request is made for "*.html" it invokes the servlet with url-pattern "/", which is your dispatcherservlet1, in this xml you don't have request mapping so this exception is thrown..
Already, when you map "/" (All) requests to spring dispatcher servlet, what's the need creating a new dispatcher servlet for *.html?
mmm...maybe I have not totaly understand what you want explain me...
what do you mean when you say me that: "in this xml you don't have request mapping so this exception is thrown? Which file you mean?
If you speak about web.xml I think that in this file I have specify the mapping for "*.html" HTTP Request that that must be managed by my SECOND DispatcherServlet named appServlet2, infact in my web.xml file I have declared these 2 tags:
The first one to create a new instance for DispatcherServlet named appServlet2
The second one to map towards this DispatcherServlet all the HTTP Request having pattern "*.html"
You also say me that: whenever a request is made for "*.html" it invokes the servlet with url-pattern "/" (that is the first one named appServlet) but this is not true !!!
When an HTTP request is made for "*.html" it is not invoked the first DispatcherServlet but seems that is invoked the second one (appServlet2) that is configured to handle exactly "*.html" HTTP Request, infact if I click on my link: "Get HELLO.html" in my stack trace I have:
In the first line you can see that the request direct to: "/spring-mvc-showcase/hello.html" is managed by my second DispatcherServlet named "appServlet2" that is configured to handle "*.html" http request....and even more...if I delete the previus 2 tags (to create the instance and the mapping for appServlet2 DispatcherServlet), if click on this links the application can not manage "*.html" request and goes into error...
So, seems that use the right DispatcherServlet for request having form "*.html" but the strange thing is that in this example to work well I have to use @RequestMapping("/hello.html") to annotate my method, and so run well and the method is mapped for my hello.html http request.
If I use this form it is all ok...but I think that it is very strange and have to work using @RequestMapping("/hello")
I am doing this "experiment" to understand well the mapping logic of Spring MVC framework...it is just an experiment to learn how Spring MVC work...