13

Here's my Web.xml

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/servlet-context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

my servlet-context.xml

<context:component-scan base-package="com.springexample.controller.impl" />
    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

And lastly the Handler class. which is under com.springexample.controller.impl

@Controller
public class IndexControllerImpl implements IndexController {

    @RequestMapping("/")
    public String index() {

        return "index";
    }
}

However on going to localhost:8080/projectname/

it returns a 404 error.

Jul 27, 2013 8:18:31 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/tasklist/WEB-INF/views/index.jsp] in DispatcherServlet with name 'dispatcherServlet'
Jul 27, 2013 8:18:37 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/tasklist/index] in DispatcherServlet with name '

Here is my project structure

Project Structure

R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77
user962206
  • 15,637
  • 61
  • 177
  • 270

7 Answers7

40

With the web.xml configured they way you have in the question, in particular:

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

ALL requests being made to your web app will be directed to the DispatcherServlet. This includes requests like /tasklist/, /tasklist/some-thing.html, /tasklist/WEB-INF/views/index.jsp.

Because of this, when your controller returns a view that points to a .jsp, instead of allowing your server container to service the request, the DispatcherServlet jumps in and starts looking for a controller that can service this request, it doesn't find any and hence the 404.

The simplest way to solve is to have your servlet url mapping as follows:

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Notice the missing *. This tells the container that any request that does not have a path info in it (urls without a .xxx at the end), should be sent to the DispatcherServlet. With this configuration, when a xxx.jsp request is received, the DispatcherServlet is not consulted, and your servlet container's default servlet will service the request and present the jsp as expected.

Hope this helps, I realize your earlier comments state that the problem has been resolved, but the solution CAN NOT be just adding method=RequestMethod.GET to the RequestMethod.

Akshay
  • 3,158
  • 24
  • 28
  • I think Spring's documentation is partly responsible for this confusion. They use this web.xml piece ` example /example/* ` as an example, without any warning that the resolved views might go back to the dispatcher again if servlet mapping is not carefully configured. Most people would just remove the `example` part and that's how they got this strange problem. – dnang Nov 04 '13 at 14:30
  • You're a god and I am not and I just thought that you should know. – asteri Jan 06 '15 at 19:22
  • You are too kind @asteri – Akshay Jan 07 '15 at 02:27
  • what if I want .json to be served by the dispatcher servlet. do I have to explicitly map the url pattern? – TV Nath Nov 11 '15 at 07:50
  • 1
    @TharinduVishwanath yes, you will need a url pattern if you want .json requests to be served by dispatcher servlet – Akshay Nov 11 '15 at 14:11
  • Crazy. Thank you @AkshaySinghal. You made my working day a bit shorter today. – Christian Oct 13 '17 at 20:28
  • @Christian happy to help. – Akshay Oct 14 '17 at 03:08
2

I have the same problem.... I change my project name and i have this problem...my solution was the checking project refences and use / in my web.xml (instead of /*)

1

First check whether the java classes are compiled or not in your [PROJECT_NAME]\target\classes directory.

If not you have some compilation errors in your java classes.

kishore
  • 137
  • 2
  • although it's not necessary that compiled classes should be there in target folder (unless you are using maven), but thanks for the hint!! – thekosmix Dec 09 '13 at 10:25
1

Try passing the Model object in your index method and it will work-

@RequestMapping("/")

public String index(org.springframework.ui.Model model) {

 return "index";

    }

Actually the spring container looks for a Model object in the mapping method. If it finds the same it will pass the returning String as view to the View resolver.

Hope this helps.

dwjohnston
  • 11,163
  • 32
  • 99
  • 194
0
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Hey Please use / in your web.xml (instead of /*)

Ashish
  • 45
  • 1
  • 1
  • 7
0
<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

change to:

<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
0

I added META-INF folder with context.xml contain

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/SpringGradleDemo"/>

SpringGradleDemo is my project name and it work. My servlet-mapping is "/" I read it here https://tomcat.apache.org/tomcat-5.5-doc/config/context.html