我想使用Hibernate将数据从JSP插入MySQL但是无法获得会话工厂的对象。
我成功创建了Hibernate util类的对象,但在获取数据时显示错误。
当我在JSP页面中输入数据并在DAO层中获取数据时。但在DAO层,我正在扩展会话工厂类。但我无法完成任务。
(Struts动作类)
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实用程序类)
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班)
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;
}
}
(获取数据)
JSP页面:
<%@ 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>
答案 0 :(得分:0)
当您尝试执行任务时,是否收到标题中提到的ClassNotFoundException? 如果是这样,那么您可能正在使用旧版本的struts2。 在struts-version 2.1.3之前我们使用的是
org.apache.struts2.dispatcher.FilterDispatcher
而不是
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
自struts-version 2.1.3。
以来,FilterDispatcher
已被弃用
答案 1 :(得分:0)
WEB-INF / lib下可能没有struts库。尝试手动复制最小的jar或configure eclipse to autodeploy jars将库添加为:
Project property->DeploymentAssembly->Add .