论坛首页 Java企业应用论坛

三个JAVA文件完成jsf+spring+hibernate的集成.

浏览 6069 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-07-16  
呵呵,本文章只贡献给哪些走捷径的coder,
开发工具:myeclise5.5.GA(最好是)
环境:spring 1.2 hibernate 3.2 jsf 1.1
如何搭建这个应用,应该是比如简单吧.
1)新建立一个myeclipse的web project
2)使用myeclise database explorer 建立一个数据库的连接.
3)操作   项目->右键->myeclise->add hibernate capabilities....,使用刚才的数据源,版本选择3.2
4)操作   项目->右键->myeclise->add spring capabilities...., 版本选择1.2
5)操作   项目->右键->myeclise->add jsf capabilities...., 版本选择1.1,
(不要因为好奇心乱加版本呀,我可是经过多次试验得出的结果)
引用"10元RMB"里的话做的开场:唉呀唉呀,天睛了,雨停了,你又觉得你行了.
web.xml
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  5.     <servlet>  
  6.         <servlet-name>context</servlet-name>  
  7.         <servlet-class>  
  8.             org.springframework.web.context.ContextLoaderServlet  
  9.         </servlet-class>  
  10.         <load-on-startup>1</load-on-startup>  
  11.     </servlet>  
  12.   
  13.     <context-param>  
  14.         <param-name>contextConfigLocation</param-name>  
  15.         <param-value>classpath:applicationContext.xml</param-value>  
  16.     </context-param>  
  17.     <context-param>  
  18.         <param-name>javax.faces.CONFIG_FILES</param-name>  
  19.         <param-value>/WEB-INF/faces-config.xml</param-value>  
  20.     </context-param>  
  21.     <servlet>  
  22.         <servlet-name>Faces Servlet</servlet-name>  
  23.         <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>  
  24.         <load-on-startup>0</load-on-startup>  
  25.     </servlet>  
  26.     <servlet-mapping>  
  27.         <servlet-name>Faces Servlet</servlet-name>  
  28.         <url-pattern>*.faces</url-pattern>  
  29.     </servlet-mapping>  
  30.     <welcome-file-list>  
  31.         <welcome-file>index.jsp</welcome-file>  
  32.     </welcome-file-list>  
  33. </web-app>  
userdao接口
java 代码
 
  1. package com.antbee.report.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.antbee.report.hibernate.model.SysUser;  
  6.   
  7. public interface UserDao {  
  8.     public List getUsers();  
  9.     public SysUser getUser(String userId);  
  10.     public void saveUser(SysUser user);  
  11.     public void removeUser(String userId);  
  12. }  
dao的实现类:
java 代码
 
  1. package com.antbee.report.daoimpl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  6.   
  7. import com.antbee.report.dao.UserDao;  
  8. import com.antbee.report.hibernate.model.SysUser;  
  9.   
  10. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {  
  11.   
  12.     public SysUser getUser(String userId) {  
  13.         // TODO Auto-generated method stub  
  14.         return null;  
  15.     }  
  16.   
  17.     public List getUsers() {          
  18.         return this.getHibernateTemplate().find("from SysUser as a");  
  19.     }  
  20.   
  21.     public void removeUser(String userId) {  
  22.         // TODO Auto-generated method stub  
  23.   
  24.     }  
  25.   
  26.     public void saveUser(SysUser user) {  
  27.         // TODO Auto-generated method stub  
  28.   
  29.     }  
  30.   
  31. }  
类似于struts的web应用,也就是action吧.
java 代码
 
  1. package com.antbee.report.action;  
  2.   
  3.   
  4. import javax.faces.model.DataModel;  
  5. import javax.faces.model.ListDataModel;  
  6.   
  7. import com.antbee.report.dao.UserDao;  
  8. import com.antbee.report.hibernate.model.SysUser;  
  9.   
  10. public class UserBean {  
  11.     private UserDao userDao;  
  12.     private SysUser sysuser = new SysUser();  
  13.     private DataModel users=new ListDataModel();  
  14.       
  15.       
  16.     public DataModel getUsers() {  
  17.         users.setWrappedData(this.userDao.getUsers());   
  18.         return users;  
  19.     }  
  20.     public void setUsers(DataModel users) {       
  21.         this.users = users;  
  22.     }  
  23.       
  24.     public UserDao getUserDao() {  
  25.         return userDao;  
  26.     }  
  27.     public void setUserDao(UserDao userDao) {  
  28.         this.userDao = userDao;  
  29.     }  
  30.     public SysUser getSysuser() {  
  31.         return sysuser;  
  32.     }  
  33.     public void setSysuser(SysUser sysuser) {        
  34.         this.sysuser = sysuser;  
  35.     }  
  36. }  
Spring的配置:
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.     <!-- SessionFactory配置 -->  
  5.     <bean id="SessionFactory"  
  6.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  7.         <property name="configLocation">  
  8.             <value>classpath:hibernate.cfg.xml</value>  
  9.         </property>  
  10.     </bean>  
  11.     <!-- TransactionManager事务管理 -->  
  12.     <bean id="TransactionManager"  
  13.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"  
  14.         abstract="false" singleton="true" lazy-init="default"  
  15.         autowire="default" dependency-check="default">  
  16.         <property name="sessionFactory">  
  17.             <ref bean="SessionFactory" />  
  18.         </property>  
  19.     </bean>  
  20.     <!-- BaseDAO基础DAO配置 -->  
  21.     <bean id="BaseTransactionProxy"  
  22.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
  23.         abstract="true">  
  24.         <property name="transactionManager">  
  25.             <ref bean="TransactionManager" />  
  26.         </property>  
  27.         <property name="proxyTargetClass">  
  28.             <value>true</value>  
  29.         </property>  
  30.         <property name="transactionAttributes">  
  31.             <props>  
  32.                 <prop key="save*">PROPAGATION_REQUIRED</prop>  
  33.                 <prop key="remove*">PROPAGATION_REQUIRED</prop>  
  34.                 <prop key="*">PROPAGATION_REQUIRED</prop>  
  35.             </props>  
  36.         </property>  
  37.     </bean>  
  38.   
  39.     <bean id="userDao" class="com.antbee.report.daoimpl.UserDaoImpl">  
  40.         <property name="sessionFactory">  
  41.             <ref local="SessionFactory" />  
  42.         </property>  
  43.     </bean>  
  44.     <bean id="userDaoProxy" parent="BaseTransactionProxy"  
  45.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  46.         <property name="target">  
  47.             <ref local="userDao" />  
  48.         </property>  
  49.     </bean>  
  50.   
  51. </beans>  
face-config.xml
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">  
  3.   
  4. <faces-config>  
  5.     <application>  
  6.         <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>  
  7.     </application>  
  8.     <managed-bean>  
  9.         <managed-bean-name>userBean</managed-bean-name>  
  10.         <managed-bean-class>com.antbee.report.action.UserBean</managed-bean-class>  
  11.         <managed-bean-scope>session</managed-bean-scope>  
  12.         <managed-property>  
  13.             <property-name>userDao</property-name>  
  14.             <value>#{userDao}</value>  
  15.         </managed-property>  
  16.     </managed-bean>  
  17.     <navigation-rule>  
  18.         <from-view-id>/index.jsp</from-view-id>  
  19.         <navigation-case>  
  20.             <to-view-id>/browse.jsp</to-view-id>              
  21.         </navigation-case>  
  22.     </navigation-rule>  
  23. </faces-config>  
呵呵 ,写个页面看看效果.
index.jsp
java 代码
 
  1. <html>  
  2.     <head>  
  3.        <title>Index</title>  
  4.     </head>  
  5.     <body>  
  6.        <a href="browse.faces">Login</a>  
  7.     </body>  
  8. </html>  
显示所有用户 browse.jsp
java 代码
 
  1. <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>  
  2. <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  4. <%@ page language="java" pageEncoding="UTF-8"%>  
  5. <html>  
  6.     <head>  
  7.         <title>  
  8.             Browse User  
  9.         </title>  
  10.     </head>  
  11.     <body>  
  12.         <f:view>  
  13.             <h:form>  
  14.                 <h:dataTable id="table" value="#{userBean.users}" var="u" border="1" width="80%">  
  15.                     <h:column>  
  16.                     <f:facet name="header">  
  17.                             <f:verbatim escape="true">ID</f:verbatim>  
  18.                         </f:facet>  
  19.                         <h:outputText value="#{u.id}" />  
  20.                 </h:column>  
  21.                     <h:column>  
  22.                     <f:facet name="header">  
  23.                             <f:verbatim escape="true">User Name</f:verbatim>  
  24.                         </f:facet>  
  25.                         <h:commandLink id="username" value="#{u.userName}">  
  26.                         </h:commandLink>  
  27.                 </h:column>  
  28.                     <h:column>  
  29.                         <f:facet name="header">  
  30.                             <f:verbatim escape="true">Password</f:verbatim>  
  31.                         </f:facet>  
  32.                         <h:outputText value="#{u.passWordMd5}" />  
  33.                 </h:column>  
  34.                 <h:column>  
  35.                     <f:facet name="header">  
  36.                             <f:verbatim escape="true">Operation</f:verbatim>  
  37.                         </f:facet>                        
  38.                 </h:column>  
  39.                 </h:dataTable>  
  40.             </h:form>  
  41.             <br>              
  42.         </f:view>  
  43.     </body>  
  44. </html>  

大功告成,如果你没有配置成功,请你检查一下你的版本,如果还有问题,请给你留言,呵呵.
   发表时间:2007-07-24  
hibernate.cfg.xml 能否写出来看看,dataSource 你是配在hibernate.cfg.xml里面吗?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics