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

阅读更多
呵呵,本文章只贡献给哪些走捷径的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>contextservlet-name>  
  7.         <servlet-class>  
  8.             org.springframework.web.context.ContextLoaderServlet  
  9.         servlet-class>  
  10.         <load-on-startup>1load-on-startup>  
  11.     servlet>  
  12.   
  13.     <context-param>  
  14.         <param-name>contextConfigLocationparam-name>  
  15.         <param-value>classpath:applicationContext.xmlparam-value>  
  16.     context-param>  
  17.     <context-param>  
  18.         <param-name>javax.faces.CONFIG_FILESparam-name>  
  19.         <param-value>/WEB-INF/faces-config.xmlparam-value>  
  20.     context-param>  
  21.     <servlet>  
  22.         <servlet-name>Faces Servletservlet-name>  
  23.         <servlet-class>javax.faces.webapp.FacesServletservlet-class>  
  24.         <load-on-startup>0load-on-startup>  
  25.     servlet>  
  26.     <servlet-mapping>  
  27.         <servlet-name>Faces Servletservlet-name>  
  28.         <url-pattern>*.facesurl-pattern>  
  29.     servlet-mapping>  
  30.     <welcome-file-list>  
  31.         <welcome-file>index.jspwelcome-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. >  
  3. <beans>  
  4.       
  5.     <bean id="SessionFactory"  
  6.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  7.         <property name="configLocation">  
  8.             <value>classpath:hibernate.cfg.xmlvalue>  
  9.         property>  
  10.     bean>  
  11.       
  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.       
  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>truevalue>  
  29.         property>  
  30.         <property name="transactionAttributes">  
  31.             <props>  
  32.                 <prop key="save*">PROPAGATION_REQUIREDprop>  
  33.                 <prop key="remove*">PROPAGATION_REQUIREDprop>  
  34.                 <prop key="*">PROPAGATION_REQUIREDprop>  
  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. >  
  3.   
  4. <faces-config>  
  5.     <application>  
  6.         <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolvervariable-resolver>  
  7.     application>  
  8.     <managed-bean>  
  9.         <managed-bean-name>userBeanmanaged-bean-name>  
  10.         <managed-bean-class>com.antbee.report.action.UserBeanmanaged-bean-class>  
  11.         <managed-bean-scope>sessionmanaged-bean-scope>  
  12.         <managed-property>  
  13.             <property-name>userDaoproperty-name>  
  14.             <value>#{userDao}value>  
  15.         managed-property>  
  16.     managed-bean>  
  17.     <navigation-rule>  
  18.         <from-view-id>/index.jspfrom-view-id>  
  19.         <navigation-case>  
  20.             <to-view-id>/browse.jspto-view-id>              
  21.         navigation-case>  
  22.     navigation-rule>  
  23. faces-config>  
呵呵 ,写个页面看看效果.
index.jsp
java 代码
 
  1.   
  2.       
  3.        Index  
  4.       
  5.       
  6.        "browse.faces">Login  
  7.       
  8.   
显示所有用户 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.   
  6.       
  7.           </span></li> <li class=""><span>            Browse User  </span></li> <li class="alt"><span>          
  8.       
  9.       
  10.           
  11.               
  12.                 "table" value="#{userBean.users}" var="u" border="1" width="80%">  
  13.                       
  14.                     "header">  
  15.                             "true">ID  
  16.                           
  17.                         "#{u.id}" />  
  18.                   
  19.                       
  20.                     "header">  
  21.                             "true">User Name  
  22.                           
  23.                         "username" value="#{u.userName}">  
  24.                           
  25.                   
  26.                       
  27.                         "header">  
  28.                             "true">Password  
  29.                           
  30.                         "#{u.passWordMd5}" />  
  31.                   
  32.                   
  33.                     "header">  
  34.                             "true">Operation  
  35.                                                 
  36.                   
  37.                   
  38.               
  39.             
                  
  40.           
  41.       
  42.   

大功告成,如果你没有配置成功,请你检查一下你的版本,如果还有问题,请给你留言,呵呵.

你可能感兴趣的:(Java,JSF,Spring,Hibernate,Bean)