15

我使用 IntelliJ IDEA 创建了一个 Spring Mvc 应用程序,然后我将默认应用程序配置文件移动并重命名到另一个目录。现在我收到此错误:“未为此文件配置应用程序上下文”文件的新位置是 src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

该文件是这个:

<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>

有任何想法吗?谢谢你。

4

4 回答 4

1

在 web.xml 文件中检查 spring 的配置。

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

contextConfigLocation 参数配置spring的xml位置,检查web.xml是否正确。

如果你通过java代码加载xml,比如@skiabox,你可以忽略这个警告。

于 2017-12-12T03:18:17.857 回答
0

我已经从代码配置了应用程序上下文(spring 3.1 的一个新特性),所以我相信 IntelliJ 的想法会一直抱怨。这是代码。

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("/");

    }
}
于 2013-04-28T16:53:37.377 回答
0

Satur6ay 的评论对我有帮助。

但是 xml 文件被 Idea 涂成“红色”。我发现资源文件夹没有“资源”图标,但有标准的灰色文件夹图标。所以,我去了文件->项目结构->我的模块->在那里找到“资源”文件夹->“标记为”->资源。

web.xml 中的 xml-reference 变为有效,xml-spring-configs () 中的所有其他引用变为绿色有效

于 2018-11-16T10:11:54.997 回答
0

添加这个对我有用!谢谢satur6ay

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
    </param-value>
</context-param>
于 2021-01-14T13:17:55.403 回答