Sep 24, 2013 12:25:54 AM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Initializing Sun's JavaServer Faces implementation (1.2_03-20070706-NIGHTLY)
for context '/BookShop'
url isnull
log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
null
Структура каталога:
[img]https://i .sstatic.net/7ugdb.png[/img]

Файл действий AuthorAction.java:
package com.author.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.author.listener.HibernateListener;
import com.author.model.Author;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings({ "serial", "unchecked" })
public class AuthorAction extends ActionSupport implements ModelDriven{
Author author = new Author();
List authorList = new ArrayList();
public String execute() throws Exception {
return SUCCESS;
}
public Object getModel() {
return author;
}
public List getAuthorList() {
return authorList;
}
public void setAuthorList(List authorList) {
this.authorList = authorList;
}
//save customer
public String addAuthor() throws Exception{
//get hibernate session from the servlet context
SessionFactory sessionFactory =
(SessionFactory) ServletActionContext.getServletContext()
.getAttribute(HibernateListener.KEY_NAME);
Session session = sessionFactory.openSession();
//save it
author.setCreatedDate(new Date());
session.beginTransaction();
System.out.println("the value which is to be saved is"+author);
session.save(author);
session.getTransaction().commit();
//reload the customer list
authorList = null;
authorList = session.createQuery("from author").list();
System.out.println(" the author list is"+authorList);
return SUCCESS;
}
//list all customers
public String listAuthor() throws Exception{
//get hibernate session from the servlet context
SessionFactory sessionFactory =
(SessionFactory) ServletActionContext.getServletContext()
.getAttribute(HibernateListener.KEY_NAME);
Session session = sessionFactory.openSession();
authorList = session.createQuery("from author").list();
return SUCCESS;
}
}
Файл прослушивателя спящего режима:
package com.author.listener;
import java.net.URL;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateListener implements ServletContextListener{
private Configuration config;
private SessionFactory factory;
private String path = "src/resources/hibernate.cfg.xml";
@SuppressWarnings("unchecked")
private static Class clazz = HibernateListener.class;
public static final String KEY_NAME = clazz.getName();
public void contextDestroyed(ServletContextEvent event) {
//
}
public void contextInitialized(ServletContextEvent event) {
try {
URL url = HibernateListener.class.getResource(path);
System.out.println("url is"+url);
config = new Configuration().configure(url);
factory = config.buildSessionFactory();
//save the Hibernate session factory into serlvet context
event.getServletContext().setAttribute(KEY_NAME, factory);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Мой файл модели Author.java:
package com.author.model;
import java.util.Date;
public class Author{
private Long customerId;
private String name;
private String address;
private Date createdDate;
public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
}
hibernate.cfg.xml:
false
root
jdbc:mysql://localhost:3306/bookshop
root
org.hibernate.dialect.MySQLDialect
true
true
false
Файл сопоставления Hibernate Author.hbm.xml:
Файл JSP Author.jsp:
Struts 2 + Hibernate integration example
Add Customer
All Customers
Customer Id
Name
Address
Created Date
struts.xml:
pages/Author.jsp
pages/Author.jsp
web.xml:
Bookshop
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
com.author.listener.HibernateListener
Подробнее здесь: https://stackoverflow.com/questions/189 ... ibernate-3
Мобильная версия