16

I have created a Spring Mvc application using IntelliJ IDEA and then I moved and renamed the default application-config file to another directory. Now I am getting this error : 'Application context not configured for this file' The new place of the file is src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

The file is this one:

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

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven/>

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

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jspx"/>
    </bean>

    <context:component-scan base-package="com.apress.prospring3.ch17.web.controller"/>

</beans>

Any ideas? Thank you.

skiabox
  • 3,449
  • 12
  • 63
  • 95

4 Answers4

1

Check the config of spring in the web.xml file.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
    </param-value>
</context-param>

The contextConfigLocation parameter config the xml location about spring, Check you web.xml is correct.

If you load the xml by java code,like @skiabox, you can ignore this warning.

Satur6ay
  • 121
  • 2
  • 2
0

I've configured application context from code (a new feature of spring 3.1) so I believe that IntelliJ idea will keep complaining. Here is the code.

package com.apress.prospring3.ch17.web.init;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;


public class MyWebAppInitializer implements WebApplicationInitializer{

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        XmlWebApplicationContext appContext = new XmlWebApplicationContext();

        appContext.setConfigLocation("/WEB-INF/spring/appServlet/servlet-context.xml");

        ServletRegistration.Dynamic dispatcher = container.addServlet("appServlet", new DispatcherServlet(appContext));

        MultipartConfigElement multipartConfigElement = new MultipartConfigElement(null, 5000000, 5000000, 0);
        dispatcher.setMultipartConfig(multipartConfigElement);

        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

    }
}
skiabox
  • 3,449
  • 12
  • 63
  • 95
0

Satur6ay's comment helps me particularry.

But xml-file was coloured "red" by Idea. I found thar resources folder had not "resource"-icon, but had standard gray folder icon. So, I went to File -> Project Structure -> my module -> found there "resorces" folder -> "Mark as" -> Resources.

xml-reference in web.xml become valid and all other references in xml-spring-configs ()become green-valid

0

Adding this worked for me!! thx to satur6ay

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
    </param-value>
</context-param>