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

查看:1930
本文介绍了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

以下是如何该项目是结构化的。

Following are how the project is structured.


  1. web.xml在WEB-CONTENT下。

  2. abc是我的Dispatcher servlet的名称。所以配置文件将是abc-servlet.xml,它将包含所有
    命名空间和模式定义的bean标记。

  3. 我应该在哪里放置abc-servlet.xml文件?它应该在classes文件夹中还是在web.xml中?

  4. 是否因为spring配置文件的位置而引起异常?

  5. 另外,如果我将配置文件放在其他位置怎么样我怎么让项目知道它在项目的特定路径?

  1. web.xml is under WEB-CONTENT.
  2. abc is the name of my Dispatcher servlet. So config file will be abc-servlet.xml which will contain the bean tag with all the namespace and schemas defined.
  3. Where should i place the abc-servlet.xml file? Should it be in the classes folder or where the web.xml is?
  4. Is the exception arising because of the location of the spring config file?
  5. Also, what if i place the config file in other location how do i let the project know it is at that particular path in the project?

我在smaple项目中使用注释驱动的控制器。

I am using annotation driven controller in the smaple project.

推荐答案

从文档中:


在初始化DispatcherServlet时,Spring MVC在
Web应用程序的WEB-INF目录中查找名为[servlet-name] -servlet.xml的
文件,并创建在那里定义的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 中设置 contextConfigLocation init-param来更改调度程序servlet配置的位置和名称

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天全站免登陆