No mapping found for HTTP request with URI: in Spring MVC application

I get this error.

my web.xml has this

<servlet> <servlet-name>springweb</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/web-application-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springweb</servlet-name> <url-pattern>/app/*</url-pattern> </servlet-mapping> 

I have this in my web application-config.xml

 <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> </bean> <bean name="/Scheduling.htm" class="com.web.SchedulingController"/> 

my com.web.SchedulingController looks like

 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class SchedulingController implements Controller{ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView modelAndView = new ModelAndView("/jsp/Scheduling_main.jsp"); modelAndView.addObject("message","Hello World MVC!!"); return modelAndView; } } 

When I find this controller with the URL http: // localhost: 8080 / project1 / app / Scheduling.htm the Scheduling_main.jsp file is displayed, but the images are not displayed correctly. Also js and css file do not get render.

I am accessing images like these

 <img src="jquerylib/images/save_32x32.png" title="Save Appointment"> 

If I change the URL mapping in the servlet definition to * .htm, the images will be displayed in order. Can you indicate where I'm missing.

Here is the complete error message

 WARN [PageNotFound] No mapping found for HTTP request with URI [/mavenproject1/app/jquerylib/images/save_32x32.png] in DispatcherServlet with name 'springweb' 

Many thanks. Ravi

+6
spring mvc
source share
4 answers

I think this is happening because you are trying to get the image though a servlet (displayed as / app / *). You need to get static content without processing it with a servlet, for example, set the image source to

<img src="../jquerylib/images/save_32x32.png" title="Save Appointment">

then the real URI of your image will be / mavenproject 1 / jquerylib / images / save_32x32.png and it will be returned by your tomcat as is, without any processing.

+7
source

I will just add three rules until spring default rules (/ **) so tuckey urlrewritefilter (urlrewrite.xml) solve the problem

 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd"> <urlrewrite default-match-type="wildcard"> <rule> <from>/</from> <to>/app/welcome</to> </rule> <rule> <from>/scripts/**</from> <to>/scripts/$1</to> </rule> <rule> <from>/styles/**</from> <to>/styles/$1</to> </rule> <rule> <from>/images/**</from> <to>/images/$1</to> </rule> <rule> <from>/**</from> <to>/app/$1</to> </rule> <outbound-rule> <from>/app/**</from> <to>/$1</to> </outbound-rule> </urlrewrite> 

How to handle static content in spring MVC?

+2
source

Add this to springweb-servlet.xml

<mvc:default-servlet-handler/>

Below text is extracted from Spring link

This tag allows you to map DispatcherServlet to "/" (thus overriding the default display of servlets in the container), while allowing static resource requests to handle the default Servlet for the container. It sets the DefaultServletHttpRequestHandler with the URL mapping (given the lowest order of priority) to "/ **". This handler will redirect all requests to the servlet by default.

+2
source
0
source

All Articles