Spring's DispatcherServlet noHandlerFound
I've got a simple application and I can't get it to work. I have HomeController in com.springinaction.training.mvc that looks like
Code:
public class HomeController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
return new ModelAndView("home", "message", greeting);
}
private String greeting;
public void setGreeting(String greeting) {
this.greeting = greeting;
}
}
Now for my web.xml, I have
Code:
<web-app>
<servlet>
<servlet-name>training</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>training</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
and for training-servlet.xml, I have
Code:
<bean name="/home.htm"
class="com.springinaction.training.mvc.HomeController">
<property name="greeting">
<value>Welcome to Spring Training!</value>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
and my home.jsp is
Code:
<html>
<head><title></title></head>
<body>
<h2>${message}</h2>
</body>
</html>
What am I missing here? Coz, I get a
Code:
WARNING: No mapping for [/SpringInAction/home.htm] in DispatcherServlet with name 'training'
Re: Spring's DispatcherServlet noHandlerFound
You don't seem to have a RequestMapper (or whatever it's called) defined. Check the book again.
Re: Spring's DispatcherServlet noHandlerFound
But CB, it's stated in the book that
Quote:
One thing that may have struck you as odd is that instead of specifying a bean id for the HomeController bean, we’ve specified a name. And to make things even weirder, instead of giving it a real name, we’ve given it a URL pattern of “/home.htm”. Here the name attribute is serving double duty as both the name of the bean and a URL pattern for requests that should be handled by this controller. Because the URL pattern has special characters that are not valid in an XML id attribute—specifically, the slash (/) character—the name attribute had to be used instead of id.
When a request comes to DispatcherServlet with a URL that ends with “/home.htm”, DispatcherServlet will dispatch the request to HomeController for handling. Note, however, that the only reason that the bean’s name attribute is used as the URL pattern is because we haven’t configured a handler mapping bean. The default handler mapping used by DispatcherServlet is BeanNameUrlHandlerMapping, which uses the base name as the URL pattern.
I don't know if I understand it write that if the base URL lasted with /home.htm, the Dispatcher will dispatch it to homeController as the default UrlHandler is BeanNameUrlHanderMapping. I'm sorry for bothers, this may not be a case of Spring Framework but how well I understand English. I hardly speak/understand english btw, so sorry again.
Re: Spring's DispatcherServlet noHandlerFound
Opps, sorry again. But I recently edited the servlet.xml to have a SimpleUrlHandlerMapping but I assume to be BeanNameUrlHanderMapping. I commented the
Code:
<!-- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">springAppController</prop>
<prop key="/home.htm">homeController</prop>
</props>
</property>
</bean> -->
and it works now. Thanks CB, that directs me to look into handlerMapping.