oracle 11g和hibernate spring和jsf的集成 [英] oracle 11g and integration of hibernate spring and jsf
问题描述
我使用jsf 2,hibernate 4.1.4,spring 3.1,oracle 11g和maven 3.
但它无法连接到oracle并出错。我下载了最新版本的ojdbc6。
applicationContext.xml
<?xml version =1.0encoding =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:context =http://www.springframework.org/schema/context
xmlns:lang =http://www.springframework.org/schema/lang
xmlns:task =http://www.springframework.org/schema/task
xmlns :tx =http://www.springframework.org/schema/tx
xmlns:util =http://www.springframework.org/schema/util
xsi:schemaLocation = http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/ aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema /context/spring-context-3.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schem a / lang / spring-lang-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org / schema / util http://www.springframework.org/schema/util/spring-util-3.1.xsd\">
<! - 豆类声明 - >
< bean id =customerclass =model.Customer/>
<! - CustomerBo声明 - >
< bean id =CustomerBoImplclass =bo.CustomerBoImpl>
< property name =customerDaoImplref =CustomerDaoImpl/>
< / bean>
<! - 客户DAO声明 - >
< bean id =CustomerDaoImplclass =dao.CustomerDaoImpl>
< property name =sessionFactoryref =SessionFactory/>
< / bean>
<! - 数据来源声明 - >
< bean id =DataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSourcedestroy-method =close>
< property name =driverClassvalue =oracle.jdbc.driver.OracleDriver/>
< property name =uservalue =system/>
< property name =passwordvalue =123/>
< property name =maxPoolSizevalue =10/>
< property name =maxStatementsvalue =0/>
< property name =minPoolSizevalue =5/>
< / bean>
<! - 会话工厂声明 - >
< bean id =SessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =DataSource/>
< property name =annotatedClasses>
< list>
< value> model.Customer< / value>
< / list>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.Oracle10gDialect< / prop>
< prop key =hibernate.show_sql> true< / prop>
< /道具>
< / property>
< / bean>
<! - 启用基于注释的交易行为配置 - >
< tx:annotation-driven transaction-manager =txManager/>
<! - 事务管理器已定义 - >
< bean id =txManagerclass =org.springframework.orm.hibernate4.HibernateTransactionManager>
< property name =sessionFactoryref =SessionFactory/>
< / bean>
< / beans>
faces-config
<?xml version =1.0encoding =UTF-8?>
< faces-config
xmlns =http://java.sun.com/xml/ns/javaee
xmlns:xsi =http:// www .w3.org / 2001 / XMLSchema-instance
xsi:schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee /web-facesconfig_2_0.xsd
version =2.0>
< application>
< el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
< / el-resolver>
< / application>
< / faces-config>
<?xml version =1.0encoding =UTF-8?>
< web-app xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns =http://java.sun.com/xml/ns/javaeexmlns :web =http://java.sun.com/xml/ns/javaee/web-app_2_5.xsdxsi:schemaLocation =http://java.sun.com/xml/ns/javaee http:// java.sun.com/xml/ns/javaee/web-app_3_0.xsdversion =3.0>
< display-name> jsfspringhiber< / display-name>
< listener>
< listener-class>
org.springframework.web.context.ContextLoaderListener
< / listener-class>
< / listener>
< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value>
/WEB-INF/applicationContext.xml
< / param-value>
< / context-param>
< listener>
< listener-class>
org.springframework.web.context.request.RequestContextListener
< / listener-class>
< / listener>
< context-param>
< param-name> javax.faces.PROJECT_STAGE< / param-name>
<参数值>开发< /参数值>
< / context-param>
< welcome-file-list>
< welcome-file> default.xhtml< / welcome-file>
< / welcome-file-list>
< servlet>
< servlet-name> Faces Servlet< / servlet-name>
< servlet-class> javax.faces.webapp.FacesServlet< / servlet-class>
1< / load-on-startup>
< / servlet>
< servlet-mapping>
< servlet-name> Faces Servlet< / servlet-name>
< url-pattern> *。xhtml< / url-pattern>
< url-pattern> / faces / *< / url-pattern>
< / servlet-mapping>
< context-param>
< description>状态保存方法:'client'或'server'(= default)。请参阅JSF规范2.5.2< / description>
< param-name> javax.faces.STATE_SAVING_METHOD< / param-name>
< param-value>客户端< /参数值>
< / context-param>
< context-param>
< param-name> javax.servlet.jsp.jstl.fmt.localizationContext< / param-name>
< param-value> resources.application< / param-value>
< / context-param>
< listener>
< listener-class> com.sun.faces.config.ConfigureListener< / listener-class>
< / listener>
< / web-app>
CustomerDaoImpl.java
public class CustomerDaoImpl implements ICustomerDao {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory(){
return sessionFactory;}
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
public void addCustomer(Customer customer){
sessionFactory.openSession();
getSessionFactory()。getCurrentSession()。save(customer);
}
public void updateCustomer(Customer customer){
sessionFactory.openSession();
getSessionFactory()。getCurrentSession()。update(customer);
}
public void deleteCustomer(Customer customer){
sessionFactory.openSession();
getSessionFactory()。getCurrentSession()。delete(customer);
}
公共列表< Customer> findAllCustomer(){
sessionFactory.openSession();
List list = getSessionFactory()。getCurrentSession
$ b $().createQuery(from CUSTOMER)。
返回列表;
$ b
CustomerBoImpl.java
public class CustomerBoImpl implements ICustomerBo {
ICustomerDao customerDaoImpl;
public ICustomerDao getCustomerDaoImpl(){
return customerDaoImpl;
}
public void setCustomerDaoImpl(ICustomerDao customerDaoImpl){
this.customerDaoImpl = customerDaoImpl;
}
public void addCustomer(Customer customer){
getCustomerDaoImpl()。addCustomer(customer);
$ b $ public void updateCustomer(Customer customer){
getCustomerDaoImpl()。updateCustomer(customer);
}
public void deleteCustomer(Customer customer){
getCustomerDaoImpl()。deleteCustomer(customer);
}
public List< Customer> findAllCustomer(){
return getCustomerDaoImpl()。findAllCustomer();
$ / code $ / pre>
Customermanagedbean.java
@ManagedBean(name =CustomerMB)
@RequestScoped
public class Customermanagedbean实现Serializable {
@ManagedProperty(value = #{CustomerBoImpl})
ICustomerBo customerBoImpl;
列表< Customer> CustomerList;
public int customerId;
公共字符串名称;
公共字符串地址;
public String createdDate;
public ICustomerBo getCustomerBoImpl(){
return customerBoImpl;
}
public void setCustomerBoImpl(ICustomerBo customerBoImpl){
this.customerBoImpl = customerBoImpl;
}
公共列表< Customer> getCustomerList(){
返回CustomerList;
}
public void setCustomerList(List< Customer> customerList){
CustomerList = customerList;
}
public int getCustomerId(){
return customerId;
}
public void setCustomerId(int 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 String getCreatedDate(){
return createdDate;
}
public void setCreatedDate(String createdDate){
this.createdDate = createdDate;
}
//将新的客户数据添加到数据库
public String addCustomer(){
Customer cust = new Customer();
cust.setCustomerId(getCustomerId());
cust.setName(getName());
cust.setAddress(getAddress());
cust.setCreatedDate(getCreatedDate());
customerBoImpl.addCustomer(cust);
clearForm();
return;
}
//清除表单值
private void clearForm(){
setName();
setAddress();
}
}
Customer.java
@Entity
@Table(name =CUSTOMER)
公共类Customer {
public int customerId;
公共字符串名称;
公共字符串地址;
public String createdDate;
@Id
@Column(name =CUSTOMER_ID,unique = true,nullable = false)
public int getCustomerId(){
return customerId;
}
public void setCustomerId(int customerId){
this.customerId = customerId;
}
@Column(name =NAME)
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
@Column(name =ADDRESS)
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address = address;
}
@Column(name =CREATED_DATE)
public String getCreatedDate(){
return createdDate;
}
public void setCreatedDate(String createdDate){
this.createdDate = createdDate;
}
}
default.xhtml
<?xml version =1.0encoding =UTF-8?>
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional。 DTD>
xmlns:h =http://java.sun.com/jsf/html
xmlns:f =http://java.sun.com/jsf/core
>
< h:头>
< h:outputStylesheet library =cssname =table-style.css/>
< / h:头>
< h:body>
< h1> JSF 2.0 + Spring + Hibernate示例< / h1>
styleClass =order-table
headerClass =order- table-header
rowClasses =order-table-odd-row,order-table-even-row
>
< h:栏>
< f:facet name =header>
客户ID
< / f:facet>
#{c.customerId}
< / h:列>
< h:栏>
< f:facet name =header>
名称
< / f:facet>
#{c.name}
< / h:列>
< h:栏>
< f:facet name =header>
地址
< / f:facet>
#{c.address}
< / h:列>
< h:栏>
< f:facet name =header>
创建日期
< / f:facet>
#{c.createdDate}
< / h:列>
< / h:dataTable>
< h2>新增客户< / h2>
< h:form>
< h:panelGrid columns =3>
客户编号:
< h:inputText id =customerIdvalue =#{CustomerMB.customerId}
size =20required =true
label =customerId>
< / h:inputText>
< h:message for =customerIdstyle =color:red/>
名称:
< h:inputText id =namevalue =#{CustomerMB.name}
size =20required =true
label = 名称>
< / h:inputText>
< h:message for =namestyle =color:red/>
地址:
< h:inputTextarea id =addressvalue =#{CustomerMB.address}
cols =30rows =10required = true
label =地址>
< / h:inputTextarea>
创建日期:
< h:inputTextarea id =createdDatevalue =#{CustomerMB.createdDate}
size =20required =true
label =createdDate>
< / h:inputTextarea>
< / h:panelGrid>
< h:commandButton value =Submitaction =#{CustomerMB.addCustomer()}/>
< / h:表格>
< / h:body>
< / html>
当我运行default.xhtml时:
我看不到表中的记录。
when我点击提交按钮我有这些错误:
pre $ SEVERE:org.hibernate.HibernateException:找不到当前线程的会话
javax.faces.el.EvaluationException:org.hibernate.HibernateException:没有找到当前线程的会话
在javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
at com。 sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)在javax.faces.component.UICommand.broadcast处的
(UICommand.java:311)在javax.faces.component.UIViewRoot处为
。 broadcastEvents(UIViewRoot.java:781)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java: 77)
在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:9 7)
在com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
在javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache .catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator .AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(Standard EngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java: 999)
at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java: 309)
在java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知源)$ b $在java.lang.Thread .run(Unknown Source)
引起:org.hibernate.HibernateException:没有找到当前线程的会话
在org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
at dao.CustomerDaoImpl.addCustomer(CustomerDaoImpl.java:18)
a牛逼bo.CustomerBoImpl.addCustomer(CustomerBoImpl.java:23)
在managedbean.Customermanagedbean.addCustomer(Customermanagedbean.java:70)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
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.el.parser.AstValue.invoke(AstValue.java:264)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces .facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
... 24 more
Jul 02,2012 4:40:48 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
警告:#{CustomerMB.addCustomer()}:org.hibernate.HibernateException:没有找到会话r当前线程
javax.faces.FacesException:#{CustomerMB.addCustomer()}:org.hibernate.HibernateException:未找到当前线程的会话
at com.sun.faces.application.ActionListenerImpl.processAction (ActionListenerImpl.java:114)
在javax.faces.component.UICommand.broadcast(UICommand.java:311)
在javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
at com。 sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
位于com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
位于javax.faces.webapp。 FacesServlet.service(FacesServlet.java:308)
在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
在org.apache.catalina.core.ApplicationFilterChain.doFilter( Applicati onFilterChain.java:210)
在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java: 169)
在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)$ b在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
$ b在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
。在组织.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
在org.apache.coyote .http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
在org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:565)
在org.apache.tomc at.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:309)$ java.util.concurrent.ThreadPoolExecutor.runWorker中的
(未知源)$ java.util.concurrent.ThreadPoolExecutor
$ Worker .run(未知源)
在java.lang.Thread.run(未知源)
导致:javax.faces.el.EvaluationException:org.hibernate.HibernateException:找不到当前线程的会话
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
... 23 more
引起:org.hibernate.HibernateException:没有找到当前线程的会话
在org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
在org .hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
at dao.CustomerDaoImpl.addCustomer(CustomerDaoImpl.j ava:18)
at bo.CustomerBoImpl.addCustomer(CustomerBoImpl.java:23)
at managedbean.Customermanagedbean.addCustomer(Customermanagedbean.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (本机方法)
在sun.reflect.NativeMethodAccessorImpl.invoke(来源不明)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(来源不明)
在java.lang.reflect.Method.invoke (未知源)
at org.apache.el.parser.AstValue.invoke(AstValue.java:264)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
.. 。24 more
我导入org.springframework.transaction。 annotation.Transactional在我的CustomerBoImpl.java
import java.util.List;
导入bo.ICustomerBo;
导入dao.ICustomerDao;
导入模型。客户;
import org.springframework.transaction.annotation.Transactional;
@Transactional(readOnly = true)
public class CustomerBoImpl implements ICustomerBo {
ICustomerDao customerDaoImpl;
public ICustomerDao getCustomerDaoImpl(){
return customerDaoImpl;
}
public void setCustomerDaoImpl(ICustomerDao customerDaoImpl){
this.customerDaoImpl = customerDaoImpl;
}
@Transactional(readOnly = false)
@Override
public void addCustomer(Customer customer){
getCustomerDaoImpl()。 addCustomer(客户);
$ b @Transactional(readOnly = false)
@Override
public void updateCustomer(Customer customer){
getCustomerDaoImpl()。 updateCustomer(客户);
}
@Transactional(readOnly = false)
@Override
public void deleteCustomer(Customer customer){
getCustomerDaoImpl()。deleteCustomer(customer) ;
}
@Override
public List< Customer> findAllCustomer(){
return getCustomerDaoImpl()。findAllCustomer();
}
}
i am using jsf 2,hibernate 4.1.4,spring 3.1,oracle 11g and maven 3. but it can not connect to oracle and has error. i downloaded the latest version ojdbc6. applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<!-- Beans Declaration -->
<bean id="customer" class="model.Customer"/>
<!-- CustomerBo Declaration -->
<bean id="CustomerBoImpl" class="bo.CustomerBoImpl">
<property name="customerDaoImpl" ref="CustomerDaoImpl" />
</bean>
<!-- Customer DAO Declaration -->
<bean id="CustomerDaoImpl" class="dao.CustomerDaoImpl">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<!-- Data Source Declaration -->
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@Mohsen-PC:1521:mydb" />
<property name="user" value="system" />
<property name="password" value="123" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="0" />
<property name="minPoolSize" value="5" />
</bean>
<!-- Session Factory Declaration -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>model.Customer</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- Transaction Manager is defined -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory"/>
</bean>
</beans>
faces-config
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
</faces-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>jsfspringhiber</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>default.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
CustomerDaoImpl.java
public class CustomerDaoImpl implements ICustomerDao{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void addCustomer(Customer customer){
sessionFactory.openSession();
getSessionFactory().getCurrentSession().save(customer);
}
public void updateCustomer(Customer customer){
sessionFactory.openSession();
getSessionFactory().getCurrentSession().update(customer);
}
public void deleteCustomer(Customer customer){
sessionFactory.openSession();
getSessionFactory().getCurrentSession().delete(customer);
}
public List<Customer> findAllCustomer(){
sessionFactory.openSession();
List list = getSessionFactory().getCurrentSession
().createQuery("from CUSTOMER").list();
return list;
}
}
CustomerBoImpl.java
public class CustomerBoImpl implements ICustomerBo{
ICustomerDao customerDaoImpl;
public ICustomerDao getCustomerDaoImpl() {
return customerDaoImpl;
}
public void setCustomerDaoImpl(ICustomerDao customerDaoImpl) {
this.customerDaoImpl = customerDaoImpl;
}
public void addCustomer(Customer customer){
getCustomerDaoImpl().addCustomer(customer);
}
public void updateCustomer(Customer customer){
getCustomerDaoImpl().updateCustomer(customer);
}
public void deleteCustomer(Customer customer){
getCustomerDaoImpl().deleteCustomer(customer);
}
public List<Customer> findAllCustomer(){
return getCustomerDaoImpl().findAllCustomer();
}
}
Customermanagedbean.java
@ManagedBean(name="CustomerMB")
@RequestScoped
public class Customermanagedbean implements Serializable{
@ManagedProperty(value="#{CustomerBoImpl}")
ICustomerBo customerBoImpl;
List<Customer> CustomerList;
public int customerId;
public String name;
public String address;
public String createdDate;
public ICustomerBo getCustomerBoImpl() {
return customerBoImpl;
}
public void setCustomerBoImpl(ICustomerBo customerBoImpl) {
this.customerBoImpl = customerBoImpl;
}
public List<Customer> getCustomerList() {
return CustomerList;
}
public void setCustomerList(List<Customer> customerList) {
CustomerList = customerList;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int 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 String getCreatedDate() {
return createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
//add a new customer data into database
public String addCustomer(){
Customer cust = new Customer();
cust.setCustomerId(getCustomerId());
cust.setName(getName());
cust.setAddress(getAddress());
cust.setCreatedDate(getCreatedDate());
customerBoImpl.addCustomer(cust);
clearForm();
return "";
}
//clear form values
private void clearForm(){
setName("");
setAddress("");
}
}
Customer.java
@Entity
@Table(name="CUSTOMER")
public class Customer{
public int customerId;
public String name;
public String address;
public String createdDate;
@Id
@Column(name="CUSTOMER_ID", unique = true, nullable = false)
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="ADDRESS")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name="CREATED_DATE")
public String getCreatedDate() {
return createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
}
default.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<h:head>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>
<h:body>
<h1>JSF 2.0 + Spring + Hibernate Example</h1>
<h:dataTable value="#{CustomerMB.getCustomerList()}" var="c"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>
<h:column>
<f:facet name="header">
Customer ID
</f:facet>
#{c.customerId}
</h:column>
<h:column>
<f:facet name="header">
Name
</f:facet>
#{c.name}
</h:column>
<h:column>
<f:facet name="header">
Address
</f:facet>
#{c.address}
</h:column>
<h:column>
<f:facet name="header">
Created Date
</f:facet>
#{c.createdDate}
</h:column>
</h:dataTable>
<h2>Add New Customer</h2>
<h:form>
<h:panelGrid columns="3">
Customer ID :
<h:inputText id="customerId" value="#{CustomerMB.customerId}"
size="20" required="true"
label="customerId" >
</h:inputText>
<h:message for="customerId" style="color:red" />
Name :
<h:inputText id="name" value="#{CustomerMB.name}"
size="20" required="true"
label="Name" >
</h:inputText>
<h:message for="name" style="color:red" />
Address :
<h:inputTextarea id="address" value="#{CustomerMB.address}"
cols="30" rows="10" required="true"
label="Address" >
</h:inputTextarea>
<h:message for="address" style="color:red" />
created Date :
<h:inputTextarea id="createdDate" value="#{CustomerMB.createdDate}"
size="20" required="true"
label="createdDate" >
</h:inputTextarea>
</h:panelGrid>
<h:commandButton value="Submit" action="#{CustomerMB.addCustomer()}" />
</h:form>
</h:body>
</html>
when i run the default.xhtml: i can not see the records in the table.
when i click the submit button i have these errors:
SEVERE: org.hibernate.HibernateException: No Session found for current thread
javax.faces.el.EvaluationException: org.hibernate.HibernateException: No Session found for current thread
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
at javax.faces.component.UICommand.broadcast(UICommand.java:311)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
at dao.CustomerDaoImpl.addCustomer(CustomerDaoImpl.java:18)
at bo.CustomerBoImpl.addCustomer(CustomerBoImpl.java:23)
at managedbean.Customermanagedbean.addCustomer(Customermanagedbean.java:70)
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.el.parser.AstValue.invoke(AstValue.java:264)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
... 24 more
Jul 02, 2012 4:40:48 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: #{CustomerMB.addCustomer()}: org.hibernate.HibernateException: No Session found for current thread
javax.faces.FacesException: #{CustomerMB.addCustomer()}: org.hibernate.HibernateException: No Session found for current thread
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:114)
at javax.faces.component.UICommand.broadcast(UICommand.java:311)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.faces.el.EvaluationException: org.hibernate.HibernateException: No Session found for current thread
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
... 23 more
Caused by: org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
at dao.CustomerDaoImpl.addCustomer(CustomerDaoImpl.java:18)
at bo.CustomerBoImpl.addCustomer(CustomerBoImpl.java:23)
at managedbean.Customermanagedbean.addCustomer(Customermanagedbean.java:70)
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.el.parser.AstValue.invoke(AstValue.java:264)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
... 24 more
i import org.springframework.transaction.annotation.Transactional in my CustomerBoImpl.java
import java.util.List;
import bo.ICustomerBo;
import dao.ICustomerDao;
import model.Customer;
import org.springframework.transaction.annotation.Transactional;
@Transactional(readOnly = true)
public class CustomerBoImpl implements ICustomerBo{
ICustomerDao customerDaoImpl;
public ICustomerDao getCustomerDaoImpl() {
return customerDaoImpl;
}
public void setCustomerDaoImpl(ICustomerDao customerDaoImpl) {
this.customerDaoImpl = customerDaoImpl;
}
@Transactional(readOnly = false)
@Override
public void addCustomer(Customer customer){
getCustomerDaoImpl().addCustomer(customer);
}
@Transactional(readOnly = false)
@Override
public void updateCustomer(Customer customer){
getCustomerDaoImpl().updateCustomer(customer);
}
@Transactional(readOnly = false)
@Override
public void deleteCustomer(Customer customer){
getCustomerDaoImpl().deleteCustomer(customer);
}
@Override
public List<Customer> findAllCustomer(){
return getCustomerDaoImpl().findAllCustomer();
}
}
这篇关于oracle 11g和hibernate spring和jsf的集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!