user2274060
user2274060

Reputation: 904

spring - jstl - Error 404 - The requested resource is not available

When I want to access on my website, I've the following error :

The requested resource is not available.

I noticed that the problem is in the mvc-dispatcher-servlet.xml file on the line <mvc:resources mapping="/resources/**" location="../../resources/normalMode/"/> but I don't know why ?

Do you have any solutions ?

My project structure :

- src
  - main
    - java
      - com
        - myblog
          - controller
             -//all java files as controller
    - resources
       - normalMode
          - css
             - header.css
    - webapp
       - /WEB-INF
         - /pages 
           - mvc-dispatcher.xml
           - web.xml

mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd 
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="com.myblog.controller" />

  <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property value="org.springframework.web.servlet.view.JstlView"
              name="viewClass" />

    <property name="prefix">
      <value>/WEB-INF/pages/</value>
    </property>

    <property name="suffix">
      <value>.jsp</value>
    </property>
  </bean>

  <mvc:resources mapping="/resources/**" location="../../resources/normalMode/"/>
</beans>

web.xml

<!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>
     <display-name>Archetype Created Web Application</display-name>

     <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
     </servlet>

     <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
     </servlet-mapping>
</web-app>

IndexController.java

package com.myblog.controller;

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

@Controller
@RequestMapping(value={"","/","home"})
public class IndexController {

    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView init(){
         return new ModelAndView("index");
    }
}

Upvotes: 2

Views: 1693

Answers (2)

Ekansh Rastogi
Ekansh Rastogi

Reputation: 2526

First of all you need to load the .xml files if you do not have them on class path

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath:spring/application-config.xml,
   /WEB-INF/conf/mvc-config.xml 
  </param-value>
 </context-param>

now you need to do the following to enable the spring mvc using annotations

 <context:component-scan base-package="com.ekiras" />
 <mvc:annotation-driven />
 <tx:annotation-driven />

you need to enable the annotations and component scan to scan for all the annotations in the project.

See a demo project setup with SpringMVC + Maven + Hibernate + Sitemesh

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279960

You need to add

<mvc:annotation-driven/>

to your mvc-dispatcher-servlet.xml. With this configuration (and the component scan), Spring will scan and register your @Controller methods annotated with @RequestMapping.

With your current configuration, no such handlers were being registered and there was therefore nothing to handle your request to /.

Upvotes: 1

Related Questions