-1
私はhibernateとstruts2を統合しようとしています。エラーはありません。しかし、セッションは全く作成されていません。 CustomerAction.javaセッションセッション= sessionFactory.openSession();休止状態で動作していません
public String addCustomer(Customer customer) throws Exception{
//get hibernate session from the servlet context
SessionFactory sessionFactory =
(SessionFactory) ServletActionContext.getServletContext()
.getAttribute(HibernateListener.KEY_NAME);
System.out.println("session factory accessed");
Session session = sessionFactory.openSession();
System.out.println("session created");
//save it
session.beginTransaction();
System.out.println("Transaction begin");
session.save(customer);
session.getTransaction().commit();
}
HibernateListener.java
public class HibernateListener implements ServletContextListener{
private Configuration config;
private SessionFactory factory;
private String path = "/hibernate.cfg.xml";
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 {
SessionFactory sessionFactory =
new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
System.out.println("session factory created");
//save the Hibernate session factory into serlvet context
event.getServletContext().setAttribute(KEY_NAME, factory);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
struts.xml
<struts>
<constant name="struts.devMode" value="false" />
<package name="default" namespace="/" extends="struts-default">
<action name="addCustomer"
class="com.mkyong.customer.action.CustomerAction">
<result name="success">WEB-INF/pages/customer.jsp</result>
</action>
</package>
</struts>
コードは、エラーを与えていないが、データがデータベースに挿入されていません。
感謝。しかし、私はhttp://www.mkyong.com/struts2/struts-2-hibernate-integration-example/の助けを借りました。上記のコードはこれだけのようです。 –
この回答があなたの投票に役立ち、それを受け入れるならば。 –