Я сделал Интеграцию Jpa Hibernate + Struts2 (UI) + Spring MVC. Я получаю Исключение, отправляющее событие, инициализированное контекстом, в экземпляр слушателя класса org.springframework.web.context.ContextLoaderListener error. ПОДРОБНАЯ ОШИБКА ЖУРНАЛА Я ПОСТАВИЛ ВНИЗ. Я использую Tomcat 6. И это мои ресурсы, которые я публикую ниже. Пожалуйста, предоставьте мне решение, в чем проблема с Java, конфигурацией, пользовательским интерфейсом ..
Account.java
package com.searchApp.entity;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the account database table.
*
*/
@Entity
@Table(name="account")
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="OrganizationNumber")
private int organizationNumber;
@Column(name="AccountName")
private String accountName;
@Column(name="Address1")
private String address1;
@Column(name="Address2")
private String address2;
@Column(name="City")
private String city;
@Column(name="Country")
private String country;
@Column(name="Credit")
private double credit;
@Column(name="Program")
private String program;
public Account() {
}
public int getOrganizationNumber() {
return this.organizationNumber;
}
public void setOrganizationNumber(int organizationNumber) {
this.organizationNumber = organizationNumber;
}
public String getAccountName() {
return this.accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getAddress1() {
return this.address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return this.address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return this.country;
}
public void setCountry(String country) {
this.country = country;
}
public double getCredit() {
return this.credit;
}
public void setCredit(double credit) {
this.credit = credit;
}
public String getProgram() {
return this.program;
}
public void setProgram(String program) {
this.program = program;
}
}
и
Следующим является IAccountDAO.java
package com.searchApp.Interface;
import java.util.List;
import com.searchApp.entity.Account;
public interface IAccountDAO {
Account findById(Integer id);
List<Account> findByProperty(String propertyName,
final Object value, final int... rowStartIdxAndCount);
List<Account> findByOrganizationnumber(Object organizationnumber,
int... rowStartIdxAndCount);
List<Account> findByAccountname(Object accountname,
int... rowStartIdxAndCount);
List<Account> findByAddress1(Object address1,
int... rowStartIdxAndCount);
List<Account> findByAddress2(Object address2,
int... rowStartIdxAndCount);
List<Account> findByCity(Object city,
int... rowStartIdxAndCount);
List<Account> findByCounrty(
Object country, int... rowStartIdxAndCount);
List<Account> findByProgram(Object program,
int... rowStartIdxAndCount);
List<Account> findByCredit(Object credit,
int...rowStartIdxAndCount);
public List<Account> findAll(final int... rowStartIdxAndCount);
}
следующий ресурс - AccountAction.java
package com.searchApp.action;
import org.apache.xpath.operations.String;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import antlr.collections.List;
import com.opensymphony.xwork2.ActionSupport;
import com.searchApp.Interface.IAccountDAO;
import com.searchApp.entity.Account;
@Transactional
public class AccountAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
public String accountName;
public int organizationNumber;
public String country;
public List<Account> accountList;
@Autowired
public IAccountDAO accountDAO;
public String findAccount(){
List<Account> accountList = accountDAO.findAll();
System.out.println("*** " + accountList);
for ( Account account : accountList ){
System.out.println("*** " + account.getAccountName());
System.out.println("*** " + account.getOrganizationNumber());
}
return SUCCESS;
}
/*
* Setter and Getter Methods
*/
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public int getOrganizationNumber() {
return organizationNumber;
}
public void setOrganizationNumber(int organizationNumber) {
this.organizationNumber = organizationNumber;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public IAccountDAO getAccountDAO() {
return accountDAO;
}
public void setAccountDAO(IAccountDAO accountDAO) {
this.accountDAO = accountDAO;
}
public List<Account> getAccountList() {
return accountList;
}
}
AccountDAO.java
package com.searchApp.DAO;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.searchApp.Interface.IAccountDAO;
import com.searchApp.entity.Account;
public class AccountDAO implements IAccountDAO {
// property constants
private static final Log logger = LogFactory.getLog(Account.class);
public static final String ORGANIZATIONNUMBER = "organizationnumber";
public static final String ACCOUNTNAME = "accountname";
public static final String ADDRESS1 = "address1";
public static final String ADDRESS2 = "address2";
public static final String CITY = "city";
public static final String COUNTRY = "country";
public static final String PROGRAM = "program";
public static final String CREDIT = "credit";
private EntityManager em;
@PersistenceContext(unitName="searchAPP")
public void setEntityManager(EntityManager em) {
this.em = em;
}
private EntityManager getEntityManager() {
return em;
}
public Account findById(Integer id) {
logger.info("finding Account instance with id: " + id);
try {
Account instance = getEntityManager().find(Account.class, id);
return instance;
} catch (RuntimeException re) {
logger.error("find failed", re);
throw re;
}
}
@SuppressWarnings("unchecked")
public List<Account> findByProperty(String propertyName,
final Object value, final int... rowStartIdxAndCount) {
logger.info("finding Account instance with property: "
+ propertyName + ", value: " + value);
try {
final String queryString = "select model from Account model where model."
+ propertyName + "= :propertyValue";
Query query = em.createQuery(queryString);
query.setParameter("propertyValue", value);
if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {
int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
if (rowStartIdx > 0) {
query.setFirstResult(rowStartIdx);
}
if (rowStartIdxAndCount.length > 1) {
int rowCount = Math.max(0, rowStartIdxAndCount[1]);
if (rowCount > 0) {
query.setMaxResults(rowCount);
}
}
}
return query.getResultList();
} catch (RuntimeException re) {
logger.error("find by property name failed", re);
throw re;
}
}
public List<Account> findByOrganizationnumber(Object organizationnumber,
int... rowStartIdxAndCount) {
return findByProperty(ORGANIZATIONNUMBER, organizationnumber, rowStartIdxAndCount);
}
public List<Account> findByAccountname(Object accountname,
int... rowStartIdxAndCount) {
return findByProperty(ACCOUNTNAME, accountname, rowStartIdxAndCount);
}
public List<Account> findByAddress1(Object address1,
int... rowStartIdxAndCount) {
return findByProperty(ADDRESS1, address1, rowStartIdxAndCount);
}
public List<Account> findByAddress2(Object address2,
int... rowStartIdxAndCount) {
return findByProperty(ADDRESS2, address2, rowStartIdxAndCount);
}
public List<Account> findByCity(Object city,
int... rowStartIdxAndCount) {
return findByProperty(CITY, city,
rowStartIdxAndCount);
}
public List<Account> findByCounrty(
Object country, int... rowStartIdxAndCount) {
return findByProperty(COUNTRY, country,
rowStartIdxAndCount);
}
public List<Account> findByProgram(Object program,
int... rowStartIdxAndCount) {
return findByProperty(PROGRAM, program, rowStartIdxAndCount);
}
public List<Account> findByCredit(Object credit,
int...rowStartIdxAndCount) {
return findByProperty(CREDIT, credit, rowStartIdxAndCount);
}
/**
* Find all Account entities.
*
* @param rowStartIdxAndCount
* Optional int varargs. rowStartIdxAndCount[0] specifies the the
* row index in the query result-set to begin collecting the
* results. rowStartIdxAndCount[1] specifies the the maximum
* count of results to return.
* @return List<Account> all M605garage entities
*/
@SuppressWarnings("unchecked")
public List<Account> findAll(final int... rowStartIdxAndCount) {
logger.info("finding all Account instances");
try {
final String queryString = "select model from Account model";
Query query = em.createQuery(queryString);
if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {
int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
if (rowStartIdx > 0) {
query.setFirstResult(rowStartIdx);
}
if (rowStartIdxAndCount.length > 1) {
int rowCount = Math.max(0, rowStartIdxAndCount[1]);
if (rowCount > 0) {
query.setMaxResults(rowCount);
}
}
}
return query.getResultList();
} catch (RuntimeException re) {
logger.error("find all failed", re);
throw re;
}
}
}
вот мой dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
xmlns:p="http://www.springframework.org/schema/p">
<!-- View Resolver for JSPs -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value>/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index.html">homeController</prop>
</props>
</property>
<property name="urlMap">
<map>
<entry key="/index.html">
<ref bean="helloController"/>
</entry>
</map>
</property>
</bean>
<bean id="helloController" class="com.searchApp.action.AccountAction"> </bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="hl"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
</beans>
Следующее - это мой struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="searchApp" extends="struts-default">
<action name="list" method="findAccount" class="com.searchApp.action.AccountAction">
<result>list.jsp</result>
<result name="input">list.jsp</result>
</action>
</package>
</struts>
пожалуйста, посмотрите даже persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/
persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="searchApp" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:derby://localhost:3306/searchApp"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="admin"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
это мой web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>searchApp</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Listeners -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
это моё приложениеContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"
default-lazy-init="false">
<!-- Add support to processes PersistenceUnit and PersistenceContext annotations -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- Configure EJB3 Entity Manager Factory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/xyz" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- Configure JPA Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Configure transaction annotation -->
<tx:annotation-driven transaction-manager="searchAppTransactionManager" />
<!-- DAOs wiring -->
<bean id="AccountDAO" class="com.searchApp.DAO.AccountDAO" />
</beans>
и пользовательский интерфейс - мой list.jsp
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body style="font-family:verdana;">
<s:form action="searchApp" name="com.searchApp.action.AccountAction" validate="true">
<table align="center" border=1>
<tr bgcolor="aqua">
<td>Organization Number <s:textfield name="organizationNumber"></s:textfield></td>
<td>Account Name <s:textfield name="accountName"></s:textfield></td>
<td>Country<s:textfield name="country"></s:textfield></td>
<td colspan="3"><s:submit value="searchAccount"></s:submit></td>
</tr>
<s:if test="totalPages>1">
<tr bgcolor="lightgrey">
<td colspan="5">Page No :
<s:iterator value="totalPages.{#this}" status="stat">
<s:submit value="%{#stat.count}" name="selectedPageNumber"></s:submit>
<s:reset name="reset"></s:reset>
</s:iterator>
</td>
</tr>
</s:if>
<s:if test="selectedAccountList != null && selectedAccountList.size> 0">
<tr bgcolor="aqua"/>
<th>Organizationnumber</th>
<th>AccountName</th>
<th>Address1</th>
<th>Address2</th>
<th>City</th>
<th>Country</th>
<th>Program</th>
<th>Credit</th>
</tr>
<s:iterator value="Accounts">
<tr id="row_<s:property value="organizationNumber"/>">
<td><s:property value="accountName" /></td>
<td><s:property value="address1" /></td>
<td><s:property value="address2" /></td>
<td><s:property value="city" /></td>
<td><s:property value="country" /></td>
<td><s:property value="program" /></td>
<td><s:property value="credit" /></td>
</tr>
</s:iterator>
</s:if>
<s:submit name="createnewInstance" label="CreateNewInstance"></s:submit>
<s:submit name="cancel" label="cancel"></s:submit>
</table>
</s:form>
</body>
</html>
вот идет моя ОШИБКА:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/applicationContext.xml]
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4336)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/applicationContext.xml]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:118)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 27 more
Mar 21, 2012 11:51:12 AM org.apache.catalina.core.StandardContext start
SEVERE: Error listenerStart
Mar 21, 2012 11:51:12 AM org.apache.catalina.core.StandardContext start
SEVERE: Context [/searchApp] startup failed due to previous errors
Mar 21, 2012 11:51:12 AM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Mar 21, 2012 11:51:12 AM org.apache.catalina.core.StandardContext listenerStop
SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1066)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1040)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:988)
at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:541)
at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:142)
at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:3866)
at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4502)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4366)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Mar 21, 2012 11:51:12 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-1010