I am trying to read properties from a properties file in Spring Framework I amusing SpringToolSuite MVC template my code as follows
here is my project directory structure
I am using the java platform JDK 1.8
spring framework 4.0.1
jstl 1.2.1
i am give the details of the project below.
I am getting
when run the project spring MVC hibernate shows error .
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Initialization of bean failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentController': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: sample.dao.studentDao sample.controller.studentController.stDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory sample.dao.studentDao.sessionFactory; nested exception is java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
this is dispatcher-servelt.xml program
<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"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<context:component-scan base-package="sample"></context:component-scan>
<mvc:annotation-driven/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<!-- <bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />-->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/hibernatemvc"/>
<property name="username" value="root"/>
<property name="password" value="mysql"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="sample"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
this code is studentcontroller.java file
package sample.controller;
import com.google.gson.Gson;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sample.dao.studentDao;
import sample.entity.Details;
/**
*
* @author admin
*/
@Controller
public class studentController {
@Autowired
studentDao stDao;
@RequestMapping(value ="index.htm", method = RequestMethod.GET)
public String index()
{
System.out.println("at index");
return "index";
}
@RequestMapping(value ="insert.htm", method = RequestMethod.POST)
public @ResponseBody String insert
(
@RequestParam("id") Integer id,
@RequestParam("name") String name,
@RequestParam("email") String email,
@RequestParam("fee") String fee,
@RequestParam("sname") String sname,
@RequestParam("contactno") String contactno
)
{
Details ei= new Details();
ei.setName(name);
ei.setEmail(email);
ei.setFee(fee);
ei.setSname(sname);
ei.setContactno(contactno);
stDao.save(ei);
Gson ob = new Gson();
return "success";
}
@RequestMapping(value="fetch.htm", method = RequestMethod.GET)
public @ResponseBody String display()
{
List<Details> listdetails = (List<Details>) stDao.display();
return new Gson().toJson(listdetails);
}
@RequestMapping(value = "delete.htm")
public @ResponseBody String delete(@RequestParam("id") Integer id)
{
stDao.delete(id);
return "successful";
}
@RequestMapping(value = "edit.htm")
public @ResponseBody String edit(@RequestParam("id") Integer id)
{
List editlist;
editlist = stDao.getDetailsForEdit(id);
return new Gson().toJson(editlist);
}
@RequestMapping(value = "update.htm", method = RequestMethod.POST)
public @ResponseBody String update
(
@RequestParam(value="id",required=false) Integer id,
@RequestParam("name") String name,
@RequestParam("email") String email,
@RequestParam("fee") String fee,
@RequestParam("sname") String sname,
@RequestParam("contactno") String contactno
)
{
Details st = new Details();
st.setName(name);
st.setEmail(email);
st.setFee(fee);
st.setSname(sname);
st.setContactno(contactno);
System.out.println("st");
stDao.update(st);
Gson ob = new Gson();
return "success";
}
}
this is studentDao.java file
package sample.dao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import sample.entity.Details;
/**
*
* @author admin
*/
@Transactional
@Repository
public class studentDao {
@Autowired
private SessionFactory sessionFactory;
public void save(Details stdinfo) {
Session session = sessionFactory.openSession();
session.save(stdinfo);
session.close();
}
public List display() {
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Details.class);
criteria.setProjection(Projections.projectionList().add(Projections.property("id"), "id")
.add(Projections.property("name"), "name")
.add(Projections.property("email"), "email")
.add(Projections.property("fee"), "fee")
.add(Projections.property("sname"), "sname")
.add(Projections.property("contactno"), "contactno"));
criteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List list = criteria.list();
session.close();
return list;
}
public void delete(Integer id){
Session session = sessionFactory.openSession();
Transaction trx = session.beginTransaction();
Details stdtl = new Details(id);
session.delete(stdtl);
trx.commit();
session.close();
}
public List getDetailsForEdit(Integer id) {
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Details.class);
criteria.setProjection(Projections.projectionList()
.add(Projections.property("id"), "id")
.add(Projections.property("name"), "name")
.add(Projections.property("email"), "email")
.add(Projections.property("fee"), "fee")
.add(Projections.property("sname"), "sname")
.add(Projections.property("contactno"), "contactno")
);
criteria.add(Restrictions.eq("id", id));
criteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List list = criteria.list();
session.close();
return list;
}
public void update(Details Stdinfo){
Session session = sessionFactory.openSession();
Transaction trx = session.beginTransaction();
session.update(Stdinfo);
trx.commit();
session.close();
}
}
this is web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd"
metadata-complete="true">
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>