6

I'm getting 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 this

/*
* 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 hit this controller with the URL http://localhost:8080/project1/app/Scheduling.htm The Scheduling_main.jsp gets displayed but the images are not displayed properly. Also the js and css file are not getting rendered.

I'm accessing the images like this

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

If I change the URL mapping in the servlet definition to *.htm, the images get displayed fine. Can you point out where I'm missing out.

Here is 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'

Thanks a lot. Ravi

Ravi
  • 7,939
  • 14
  • 40
  • 43
  • Here is 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' – Ravi Oct 02 '09 at 17:11

3 Answers3

8

I'm think it happens because you try get your image though servlet (mapped as /app/*) You need get static content without handle it with your servlet, for example set image source to

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

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

Alexey Sviridov
  • 3,360
  • 28
  • 33
  • Thanks a lot, it worked. Just got to learn more about URL mappings. – Ravi Oct 02 '09 at 18:03
  • saved my day. Would you mind explaining a little more. when I prefix the file path with '../' does it indirectly become absoultepath? and so that the handler is not invoked? – kalyan Mar 10 '11 at 14:35
  • @kalyan No, ".." not mean to be absolute path, ".." mean goto upper directory. So what actually happens: when you set image src to ../jquerylib/images/save_32x32.png your full path will be /mavenproject1/app/../jquerylib/images/save_32x32.png and will be transform to /mavenproject1/jquerylib/images/save_32x32.png by your browser. But your servet handles only /mavenproject1/app/* urls, so /mavenproject1/jquerylib/images/save_32x32.png will be handlet by tomcat itself and returned as is, as usual static content. Sorry for my bad english – Alexey Sviridov Mar 10 '11 at 15:07
  • I got your point. This way it just avoids the handler url path. But when I access the jsp page statically and not through the view rendering, i still get the images correctly. So the link ../jquerylib/images/save_32x32.png should become /mavenproject1/../jquerylib/images/save_32x32.png, how does this handled? – kalyan Mar 10 '11 at 15:12
  • actually the same. /mavenproject1/../jquerylib/images/save_32x32.png becomes /jquerylib/images/save_32x32.png, so you run out of your webapp context (mavenproject1) , and this path will be handled by the ROOT web application – Alexey Sviridov Mar 10 '11 at 17:38
  • Awesome!!You saved my day!! I had been so much solutions with no effect!! – Allan Ruin Feb 19 '14 at 13:06
2

I just add three rules before spring default rule (/**) to tuckey's urlrewritefilter (urlrewrite.xml) to 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?

Community
  • 1
  • 1
2

Add this to springweb-servlet.xml

<mvc:default-servlet-handler/>

Below text is extracted from Spring reference

This tag allows for mapping the DispatcherServlet to "/" (thus overriding the mapping of the container's default Servlet), while still allowing static resource requests to be handled by the container's default Servlet. It configures a DefaultServletHttpRequestHandler with a URL mapping (given a lowest precedence order) of "/**". This handler will forward all requests to the default Servlet.

bnguyen82
  • 6,048
  • 5
  • 30
  • 39