简体   繁体   English

无法从 src/main/resources 加载 dispatcher-servlet.xml

[英]Cannot load dispatcher-servlet.xml from src/main/resources

My spring mvc webapp had this structure (Maven project):我的 spring mvc webapp 有这个结构(Maven 项目):

src
- main
 -- java
 -- webapp 
  --- WEB-INF
   --- web.xml
   --- mvc-dispatcher-servlet.xml

Than I decided to write unit tests(junit).比我决定编写单元测试(junit)。 In an article I read that I should move the mvc-dispatcher-servlet.xml to src/main/resources so that I can access it here in my test in the ContextConfiguration annotation:在我读到的一篇文章中,我应该将 mvc-dispatcher-servlet.xml 移动到 src/main/resources 以便我可以在我的测试中的 ContextConfiguration 注释中访问它:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/mvc-dispatcher-servlet.xml")
public class UserDaoTest {

    @Autowired
    private UserDao userDao;

    @Test
    public void testGetUserByUsername() throws Exception {
        User admin = userDao.getUserByUsername("admin");
        Assert.assertNotNull(admin);
    }
}

That worked fine and my unit test was running successfull.效果很好,我的单元测试运行成功。 Than I started my application server to implement further features but than I got exceptions.比我启动我的应用程序服务器来实现更多功能,但我遇到了异常。 I figured out, that after moving the mvc-dispatcher-servlet.xml from WEB-INF to src/main/resources I forgot to make the following change in my web.xml:我发现,在将 mvc-dispatcher-servlet.xml 从 WEB-INF 移动到 src/main/resources 后,我忘记在 web.xml 中进行以下更改:

<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>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <!-- changed from /WEB-INF/mvc-dispatcher-servlet.xml -->
    <param-value>classpath:mvc-dispatcher-servlet.xml</param-value>
</context-param>

But I still get the following exception:但我仍然收到以下异常:

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]

What did I do wrong?我做错了什么?

Please, consider this possible solution to your problem.请考虑这个可能的解决方案来解决您的问题。 Unfortunately, I can't conclude what was wrong with your way, but that solution should work.不幸的是,我无法断定您的方式有什么问题,但该解决方案应该有效。

Move the description of contextConfigLocation from context-param tag to init-param tag inside servlet tag as follows:将 contextConfigLocation 的描述从 context-param 标签移动到 servlet 标签内的 init-param 标签,如下所示:

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!-- changed from /WEB-INF/mvc-dispatcher-servlet.xml -->
        <param-value>classpath:mvc-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改dispatcher-servlet.xml的位置不起作用 - Changing location of dispatcher-servlet.xml not working Spring dispatcher-servlet.xml不起作用 - spring dispatcher-servlet.xml not working 在dispatcher-servlet.xml中添加了“ mvc:resources”标签以添加js和css,它的HTTP状态为404 - Added “mvc:resources” tag to dispatcher-servlet.xml to add js and css, it is giving HTTP Status 404 Dispatcher-Servlet.xml找不到名称为Bean的类[org.springframework.web.servlet.mvc.support] - Dispatcher-Servlet.xml Cannot find class [org.springframework.web.servlet.mvc.support] for bean with name dispatcher-servlet.xml和application-context.xml - dispatcher-servlet.xml and application-context.xml 编译器错误调度程序-servlet.xml Spring MVC + Hibernate - Compiler error dispatcher-servlet.xml spring mvc + hibernate Spring - 构建路径不完整。 在dispatcher-servlet.xml中找不到org / springframework / beans / factory / Aware的类文件 - Spring - Build path is incomplete. Cannot find class file for org/springframework/beans/factory/Aware in dispatcher-servlet.xml FileNotFoundException: 类路径资源 [WEB-INF/dispatcher-servlet.xml] 无法打开,因为它确实存在时不存在 - FileNotFoundException: class path resource [WEB-INF/dispatcher-servlet.xml] cannot be opened because it does not exist when it does exist 如何解决 BeanDefinitionStoreException: IOException 解析来自 ServletContext 资源 [/WEB-INF/dispatcher-servlet.xml] 的 XML 文档? - How to resolve BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]? 如何设置web.xml和dispatcher-servlet.xml进行映射? - How to set up web.xml and dispatcher-servlet.xml for mapping?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM