Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No mapping found for HTTP request (Spring MVC)

This is probably the millionth question about this Spring MVC error, but I can't get it to work still.

I am trying to map a simple controller method to /account and later on I want to add /account/{id}, but I can't even get /account to work.

Here is my web.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <display-name>My Spring MVC web application</display-name>

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

    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:application-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param>
</web-app> 

The contents of application-context.xml:

<mvc:annotation-driven />
<context:component-scan base-package="org.example" />

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

AccountController.java :

@Controller
public class AccountController  {

    @RequestMapping(value="/account", method = RequestMethod.GET)
    public ModelAndView showAccount() throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("account");
        mav.addObject("someText", "Hello World!");
        return mav;
    }   
}

src/main/webapps/views/account.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<h1>${someText}</h1>

When I start the application in Tomcat, I see the following line appear in the log:

[localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/account], methods=[GET], params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.example.springmvc.controller.AccountController.showAccount() throws java.lang.Exception

To me, that suggests the url localhost:8080/account is properly mapped and should at least give some output. But when I visit localhost:8080/account I get a 404 error and the log says:

No mapping found for HTTP request with URI [/views/account.jsp] in DispatcherServlet with name 'springDispatcherServlet'
No mapping found for HTTP request with URI [/favicon.ico] in DispatcherServlet with name 'springDispatcherServlet'

You help will be much appreciated.

like image 661
Julius Avatar asked Jul 09 '12 10:07

Julius


2 Answers

There is nothing wrong with your Spring configuration, it looks like the /account URI is being correctly handled by your Controller and it is returning the view name account correctly, which is being resolved by your InternalViewResolver as a path to /views/account.jsp>.

Now for some reason this dispatch is what is going wrong(because of /* mapping for your Spring DispatcherServlet, the assumption is that Spring can handle this /views also, which is probably why you are seeing this specific error). Can you do this, instead of placing the views in /views folder, move it to /WEB-INF/views folder and change your viewresolver to:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>
like image 102
Biju Kunjummen Avatar answered Oct 16 '22 20:10

Biju Kunjummen


Try adding the following beans to application-context.xml

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
like image 35
aseychell Avatar answered Oct 16 '22 21:10

aseychell