gpt4 book ai didi

java - 由 : java. io.FileNotFoundException : Could not open ServletContext resource [/applicationContext. xml 引起]

转载 作者:行者123 更新时间:2023-12-01 23:00:22 25 4
gpt4 key购买 nike

我正在尝试使用 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在哪里?您可以将 web.xml 中的 applicationContext.xml 引用为:

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

关于java - 由 : java. io.FileNotFoundException : Could not open ServletContext resource [/applicationContext. xml 引起],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23440573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com