spring 配置文件和 web.xml 的确切位置在哪里? [英] Where is the exact location of spring config file and web.xml?

查看:131
本文介绍了spring 配置文件和 web.xml 的确切位置在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在呈现 WEB-CONTENT/WEB-INF/jsp/index.jsp 中的 index.jsp 时获取 DispatcherServlet 的类未找到异常

Getting the Class Not Found Exception for DispatcherServlet while rendering index.jsp which is in WEB-CONTENT/WEB-INF/jsp/index.jsp

以下是项目的结构.

  1. web.xml 在 WEB-CONTENT 下.
  2. abc 是我的 Dispatcher servlet 的名称.所以配置文件将是 abc-servlet.xml,它将包含带有所有定义了命名空间和架构.
  3. 我应该将 abc-servlet.xml 文件放在哪里?它应该在 classes 文件夹中还是 web.xml 所在的位置?
  4. 是不是因为spring配置文件的位置导致的异常?
  5. 另外,如果我将配置文件放在其他位置怎么办,我如何让项目知道它位于项目中的特定路径?

我在 smaple 项目中使用注解驱动的控制器.

I am using annotation driven controller in the smaple project.

推荐答案

来自文档:

在 DispatcherServlet 初始化时,Spring MVC 会寻找一个在您的 WEB-INF 目录中创建名为 [servlet-name]-servlet.xml 的文件web 应用程序并创建在那里定义的 bean,覆盖在全局中使用相同名称定义的任何 bean 的定义范围.

Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

因此将 abc-servlet.xml 放在 WEB-INF 中应该允许调度程序 servlet 获取您的配置.

So placing abc-servlet.xml within WEB-INF should allow the dispatcher servlet to pick up your configuration.

如果您不希望您的调度程序 servlet 使用默认名称或希望它驻留在除 WEB-INF 之外的另一个目录中,您可以在 web.xml 中指定此配置.可以通过在 DispatcherServlet

If you didn't want your dispatcher servlet to use the default name or wanted it to reside in another directory besides WEB-INF you would specify this configuration in web.xml. The location and name of the dispatcher servlets configuration can be changed by setting the contextConfigLocation init-param within the DispatcherServlet

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

可以在 Spring 文档

这篇关于spring 配置文件和 web.xml 的确切位置在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆