9

Im new in java and spring. I'm trying to make hello world app and don't get what i'm doing wrong.

here is my directory structure:

test_app
-pom.xml
-src
--main
---java
----com.example.web
-----IndexController.java
---webapp
----static
-----img
------example.jpg
----WEB-INF
-----web.xml
-----dispatcher-servlet.xml
-----jsp
------index.jsp

and sources: web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>Movie Reminder WebApp</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
</web-app>

dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.example.web"/>
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">

        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

IndexController.java

package com.example.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {
    @RequestMapping(value = "/")
    public ModelAndView index() {
        return new ModelAndView("index");
    }
}

index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Hello Spring!</title>
</head>
<body>
<img src="<c:url value="/static/img/example.jpg" />" />
</body>
</html>

when i deploy my app and request , i get 404 on image request

http://localhost:8081/  --- http 200 ok 
http://localhost:8081/static/img/example.jpg - http 404 not found

when i add mvc:resources to dispatcher-servlet.xml

<context:annotation-config/>
<context:component-scan base-package="com.example.web"/>
<mvc:resources mapping="/static/**" location="/static/" />

and recompile im getting 404 to / request

http://localhost:8081/  --- http 404 not foundok 
http://localhost:8081/static/img/example.jpg - http 200 ok

Help me please to figure out what i'm doing wrong

3
  • Im using Apache Tomcat7 on windows 7, spring-mvc 3.2.1.RELEASE Commented Feb 7, 2013 at 13:15
  • java version 1.7.0_11-b21 Commented Feb 7, 2013 at 13:32
  • add <mvc:annotation-driven /> in your spring config file Commented Jan 11, 2014 at 11:56

5 Answers 5

6

Just add both these lines to your dispatcher-servlet.xml

<mvc:resources mapping="/static/**" location="/static/" />
<mvc:default-servlet-handler />

Here is what the documentation for default-servlet-handler says:

Configures a handler for serving static resources by forwarding to the Servlet container's default Servlet. Use of this handler allows using a "/" mapping with the DispatcherServlet while still utilizing the Servlet container to serve static resources. This handler will forward all requests to the default Servlet. Therefore it is important that it remains last in the order of all other URL HandlerMappings. That will be the case if you use the "annotation-driven" element or alternatively if you are setting up your customized HandlerMapping instance be sure to set its "order" property to a value lower than that of the DefaultServletHttpRequestHandler, which is Integer.MAX_VALUE.

Sign up to request clarification or add additional context in comments.

2 Comments

i added <mvc:default-servlet-handler />to dispatcher-servlet.xml after <mvc:resources mapping="/static/**" location="/static/" />. I still got localhost:8081 --- http 404 not found , localhost:8081/static/img/example.jpg - http 200 ok
See stackoverflow.com/editing-help#comment-formatting. Don't use HTML tags on StackOverflow.
0

It's because of the servlet mapping. All requests coming in are being routed to the servlet. But the servlet doesn't know how to honor the requests for static resources. You need to add a mapping for static resources. There's a few different approaches:

  1. Use the way supplied by the web server. Unfortunately this varies a bit depending on what server you have.

  2. Use a servlet that can serve static resources.

What web server are you using?

1 Comment

Im using Tomcat7 on Windows7 , Spring MVC 3.2
0

As of Spring 3.0 and higher you need to add following to your Spring configuration:

<mvc:resources mapping="/static/**" location="/static/" />

This tells your dispatcher servlet how to resolve static resources. I hope this helps.

Comments

0

Use this , it will work always.Good luck :)

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/public-resources/").setCachePeriod(31556926);`enter code here`
    }
}

Comments

0

I configure the default servlet in web.xml to serve all the static assets, so just route request will reach the controllers:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.