Hello,
I am studing the Spring MVC showcase dowloaded from STS dashboard and I am finding some difficulties with mapping an url to a controller method.
At the moment I am refering only at the "Simple" tab of the Spring MVC Showcases.
In the div having id="simple" of home.jsp page I have this link:
When I click on this link an HTTP Request towards /spring-mvc-showcase/simple resources is generated, this HTTP request is managed from simple() method of org.springframework.samples.mvc.simple.SimpleContr oller:Code:<a id="simpleLink" class="textLink" href="<c:url value="/simple" />">GET /simple</a>
Ok, this is very clear for me, as you can see this controller manage the http request towards "/simple" using the annotation @RequestMapping("/simple")Code:package org.springframework.samples.mvc.simple; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class SimpleController { @RequestMapping("/simple") public @ResponseBody String simple() { return "Hello world!"; } }
So I have try to add another link, this one:
So, now I am generating an HTTP request towards a specific hello.html file and not towards a folder as in the previus exampleCode:<li> <a href="hello.html">Say Hello</a> </li>
Now (as I did in this other "Hello World" example: http://viralpatel.net/blogs/spring-3...-spring-3-mvc/) I have created an other controller class int the same package, the HelloController class, this one:
As you can see in this class I have used the annotation @RequestMapping("/hello") that as specified in the tutorial: The @RequestMapping annotation tells Spring that this Controller should process all requests beginning with /hello in the URL path. That includes /hello/* and /hello.html.Code:package org.springframework.samples.mvc.simple; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @RequestMapping("/hello") public @ResponseBody String sayHello() { System.out.println("Sono dentro HelloController sayHello()"); return "My Hello world!"; } }
So I thought that Spring could map the hello.html request with the sayHello() method but it don't work....if I execute the prroject and I click on the "Say Hello" link, it don't work and give me the followong error message: HTTP Status 404, The requested resource is not available.
And in the stacktrace I have the following line:
12:15:16 [tomcat-http--26] DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/spring-mvc-showcase/hello.html]
12:15:16 [tomcat-http--26] RequestMappingHandlerMapping - Looking up handler method for path /hello.html
12:15:16 [tomcat-http--26] RequestMappingHandlerMapping - Did not find handler method for [/hello.html]
12:15:16 [tomcat-http--26] PageNotFound - No mapping found for HTTP request with URI [/spring-mvc-showcase/hello.html] in DispatcherServlet with name 'appServlet'
12:15:16 [tomcat-http--26] DispatcherServlet - Successfully completed request
So...seem that my DispatcherServlet named appServlet can not manage the HTTP Request towards /spring-mvc-showcase/hello.html because don't file an handerl method for path /hello.html
But why? In the linked tutorial (that I have tryied and work well) use the annotation @RequestMapping("/hello") to mapp this path...
The only difference I noticed is that in the web.xml configuration file of the MVC Showcases project I have that my DispatcherServlet map request towards "/" pattern:
And in the other "Hello World" example, in web.xml configuration file I have that map DispatcherServlet with url pattern *.htmlCode:<servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
depends on it? or what?Code:<servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
Perhaps I did not understand what exactly is mapped by <url-pattern>/</url-pattern>
Thank you very much
Andrea


Reply With Quote
