当前位置: 首页 > 面试题库 >

原因:java.io.FileNotFoundException:无法打开ServletContext资源[/applicationContext.xml]

洪昊然
2023-03-14
问题内容

我试图通过使用spring
hibernatetemplate将我的hibernate示例移植到spring,但出现此错误,原因是:java.io.FileNotFoundException:无法打开ServletContext资源[/applicationContext.xml]。请建议我运行我的项目。我在公司里更新鲜

我的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>



 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>


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


</web-app>

我的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://my server/emp_app"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource"> <ref bean="dataSource" /> </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect
            </prop>
        </props>
    </property>
</bean>


    <bean id="d" class="login.HrDao">

    <property name="sessionFactory" ref="mysessionFactory"></property>

    </bean>

</beans>

我的HrDAO.java文件->

package login;

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class HrDao {

    HibernateTemplate template;

    public void setSessionFactory(SessionFactory factory) {

        template = new HibernateTemplate(factory);

    }

    public void saveStudent(HrModel e) {

        template.save(e);

    }

}

我的HrMain.java文件->

package login;
import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

public class HrMain {

    public static void main(String[] args) {

        Resource resource = new ClassPathResource("applicationContext.xml");

        BeanFactory factory = new XmlBeanFactory(resource);

        HrDao dao = (HrDao) factory.getBean("d");

    HrModel student = new HrModel();

        student.setEmployee_Name("Akash");

        student.setPassword("jaisiaram");

        dao.saveStudent(student);

    }

}

最后,我的查看页面->

<html>
<head>
<title></title>
</head>
<body>

<form action="HrMain" method="post">
<input type="text" name="Employee_Name"/>
<input type="text" name="password">
<input type="submit">
</form>
</body>
</html>

问题答案:

你在applicationContext.xml哪 您可以将applicationContext.xmlin web.xml称为:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>


 类似资料:
  • 问题内容: 这与一个年龄较大的人非常相似,但是解决方案对我不起作用。 我有一个WAR包。 在 在 但是得到这个: org.springframework.beans.factory.BeanInitializationException:无法加载属性。嵌套的异常是java.io.FileNotFoundException:无法打开ServletContext资源[/social.propertie

  • 问题内容: 好的,我是第500位用户问这个问题,我读了很多答案,但仍然没有运气。 父模块pom包含: 子模块有,我使用运行我的webapp模块。 定义标准的调度程序模块: 我在下有文件,但是启动失败: 怎么了?文档和所有人都说Spring MVC将搜索XX-servlet.xml,其中XX是servlet的名称。为什么要搜索? 问题答案: 有自己的上下文,所有servlet和过滤器都共享它。默认情

  • 我在下有文件,但启动失败的原因是: 怎么啦?所有人都说Spring MVC将搜索xx-servlet.xml,其中XX是servlet的名称。它为什么搜索?

  • 我的web.xml 我的调度器-servlet.xml 然后这个 编辑2 我创建了新的clean Spring MVC项目,但问题仍然存在。我想问题可能出在Weblogic上,我会试着重新安装它

  • 我试图为我的项目设置配置位置,但我一直得到以下错误: java.io。FileNotFoundException:无法打开类路径资源[main/resources/app context.xml],因为它不存在 我的项目设置如下: 我将我的代码设置为: 我该怎么解决这个问题?

  • 我的项目结构是下面的屏幕截图。实际上,我得到了一个文件找不到的错误,尽管文件是存在的,我将添加错误在下面。我是Intellej的新手,这是我的第一个项目,我试图通过使用MySQL连接到数据库来制作一个音乐商店。下面是我的applicaiton-context.xml,dispatcher和web.xml是正确的。 我的日志文件是 由:java.io.fileNotFoundException引起: