Question

I am facing very common issue while setting up Spring MVC + tiles project

I am getting "No mapping found for HTTP request with URI [//home.htm] in DispatcherServlet with name

My web.xml is

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID"
    version="3.0"
>
    <servlet>
        <servlet-name><PROJECT_NAME></servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name><PROJECT_NAME></servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/<PROJECT_NAME>-servlet.xml</param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>     
            home.htm     
        </welcome-file>
    </welcome-file-list>
</web-app>

my tiles xml is

<!DOCTYPE tiles-definitions PUBLIC
        "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
 <definition name="baseLayout" template="/WEB-INF/jsp/layout.jsp">
  <put-attribute name="title" value="Sample Title" />
  <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
  <put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />
  <put-attribute name="body" value="" />
  <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
 </definition>

 <definition name="home" extends="baseLayout">
  <put-attribute name="title" value="Home" />
  <put-attribute name="body" value="/WEB-INF/jsp/home.jsp" />
 </definition>

  <definition name="page" extends="baseLayout">
  <put-attribute name="title" value="Page" />
  <put-attribute name="body" value="/WEB-INF/jsp/page.jsp" />
 </definition>
</tiles-definitions>  

project_name-servlet.xml is

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
    </bean>

    <bean
        id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"
    >
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>
</beans>

application-context.xml is

<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
    <context:component-scan base-package="com.project" />
    <context:annotation-config />

    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.context.request.async.StandardServletAsyncWebRequest"/>
</beans>

homecontroller.java is

package com.project.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller    
public class HomeController {    


     @RequestMapping("/home.htm")    
     public String myhome() {    
         System.out.println("INSIDE MY HOME");  
         return "home";    
     }    

     @RequestMapping("/page.htm")    
     public String page(@RequestParam(value="pageNo") String pageNo,HttpServletRequest request) {  
         System.out.println("PageNo: " + pageNo);  
         request.setAttribute("pageNo", pageNo);  
         return "page";    
     }    
}  

I found lots of solutions in internet, but nothing worked. I have tried following things:

  1. checked server deployment directory also, everything is deploying properly.
  2. in web.xml, in servlet url pattern, I have tried /, /,.htm
  3. In controller, in request mapping I tried /home.htm, /project_name/home.htm, /
  4. I have tried to beans in application-context.xml for different spring classes.

But nothing worked.

I m hitting http://localhost:8080/PROJECT-NAME/home.htm

Was it helpful?

Solution

Get rid of this

<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.context.request.async.StandardServletAsyncWebRequest"/>

in your application context file and put them into your servlet context file with the appropriate component scanning.


On a kind of related note, don't use DefaultAnnotationHandlerMapping. mvc:annotation-driven already registers an appropriate HandlerMapping, a RequestMappingHandlerMapping.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top