-1

I want to insert data from JSP to MySQL using Hibernate but am unable to get the object of session factory.

I am successfully creating the object of Hibernate util class, but while fetching the data it shows an error.

While I am entering data in JSP page and getting the data in DAO layer. But in DAO layer I am extending the session factory class. But I am unable to achieve the task.

(Struts action class)

package org.sachin.action;

import java.util.List;

import org.sachin.hibernate.AdminCreate;
import org.sachin.hibernate.AdminManager;   

import com.opensymphony.xwork2.ActionSupport;


public class Addad extends ActionSupport {

private AdminCreate admincreate;
private List<AdminCreate> adminList;
private Long id;

   AdminManager adminmanager;
   public Addad() {
       AdminManager   adminmanager=new AdminManager();
   }

   public String execute() {
       return SUCCESS;
   }

public String add() {
      AdminManager adm=new AdminManager();
       System.out.println(getadmin());
       try {
           adm.add(getadmin());
       } catch (Exception e) {
           e.printStackTrace();
       }
       return SUCCESS;
   }

   public AdminCreate getadmin() {
       return admincreate;
   }

   public List<AdminCreate> getadminList() {
       return adminList;
   }

   public void setadmin(AdminCreate adminCreate) {
       this.admincreate = adminCreate;
   }

   public void setadminList(List<AdminCreate> adminList) {
       this.adminList = adminList;
   }

   public Long getId() {
       return id;
   }

   public void setId(Long id) {
       this.id = id;
   }
}

(Hibernate utility class)

package org.sachin.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

(DAO class)

package org.sachin.hibernate;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

public class AdminManager extends HibernateUtil {
    public  AdminCreate add(AdminCreate admincreate) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(admincreate);
        session.getTransaction().commit();
        return admincreate;
    }

    public List<AdminCreate> list() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<AdminCreate> admin = null;
        try {
            admin = (List<AdminCreate>)session.createQuery("from admin").list();
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return admin;
    }
}

(Fetching DATA)
JSP page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
     <%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="NewFile.css">
<title>Edit Details</title>
<script type="text/javascript" src="/javascript/confirm.js">
</script>
</head>
<body>
<center><div style="color:#FF0000: #FF0000; border-radius: 5px 5px 0px 0px; padding: 15px;"><span style="font-family: verdana,arial; color: #FF0000; font-size: 2.00em; font-weight:bold;">Edit Details<br><br> </span></div></center>

<s:form action="/tut/admincreate" id="login" onsubmit="return confirm()">
<s:textfield label="UserName:" name="admincreate.username"></s:textfield>
<s:textfield label="Firstname:" name="admincreate.firstname"></s:textfield>
<s:textfield label="LastName:" name="admincreate.lastname"></s:textfield>
<s:password label="Password:" ID="password"  name="admincreate.password"></s:password>
<!--<s:password label="Confirm Password:" ID="cpassword" name="cpassword" key="admincreate.cpassword"></s:password>-->
<s:submit value="Change My Details"></s:submit>

</s:form>

</body>
</html>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Sachin Sharma
  • 41
  • 2
  • 10

2 Answers2

0

Are you getting the ClassNotFoundException, mentioned in the title, when you try to perform the task? If so, it may be, that you are using an old version of struts2. Before struts-version 2.1.3 we were using the

org.apache.struts2.dispatcher.FilterDispatcher

instead of the

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

The FilterDispatcher is deprecated since struts-version 2.1.3.

aubium77
  • 118
  • 1
  • 6
0

Probably struts library is not present under the WEB-INF/lib . Try copying minimal jars manually or configure eclipse to autodeploy jars by adding your library as:

Project property->DeploymentAssembly->Add .
Community
  • 1
  • 1
Puneet S. Chauhan
  • 745
  • 1
  • 8
  • 17