No mapping found for HTTP request (Spring MVC)

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

I'm trying to map a simple controller method to / account, and later 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 / home / 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 run the application in Tomcat, I see the following line 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 

For me, this means that the url: 8080 / account URL is correctly displayed and should at least give some result. 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 will be very grateful.

+8
spring spring-mvc
source share
2 answers

There is nothing wrong with your Spring configuration, it looks like your URI is correctly handled by the URI /account , and it correctly returns the name of the account view, which is resolved by your internal /views/account.jsp as the path to /views/account.jsp >.

Now, for some reason, this dispatch is happening incorrectly (due to the /* mapping for your Spring DispatcherServlet, it is assumed that Spring can handle this / views as well, which is probably why you see this specific error). You can do this, instead of placing the views in the /views folder, move it to the /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> 
+4
source

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"/> 
+4
source

All Articles