呵呵,本文章只贡献给哪些走捷径的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 代码
- <?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" version="2.4"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <servlet-name>context</servlet-name>
- <servlet-class>
- org.springframework.web.context.ContextLoaderServlet
- </servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- <context-param>
- <param-name>javax.faces.CONFIG_FILES</param-name>
- <param-value>/WEB-INF/faces-config.xml</param-value>
- </context-param>
- <servlet>
- <servlet-name>Faces Servlet</servlet-name>
- <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Faces Servlet</servlet-name>
- <url-pattern>*.faces</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
userdao接口
java 代码
- package com.antbee.report.dao;
-
- import java.util.List;
-
- import com.antbee.report.hibernate.model.SysUser;
-
- public interface UserDao {
- public List getUsers();
- public SysUser getUser(String userId);
- public void saveUser(SysUser user);
- public void removeUser(String userId);
- }
dao的实现类:
java 代码
- package com.antbee.report.daoimpl;
-
- import java.util.List;
-
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
-
- import com.antbee.report.dao.UserDao;
- import com.antbee.report.hibernate.model.SysUser;
-
- public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
-
- public SysUser getUser(String userId) {
-
- return null;
- }
-
- public List getUsers() {
- return this.getHibernateTemplate().find("from SysUser as a");
- }
-
- public void removeUser(String userId) {
-
-
- }
-
- public void saveUser(SysUser user) {
-
-
- }
-
- }
类似于struts的web应用,也就是action吧.
java 代码
- package com.antbee.report.action;
-
-
- import javax.faces.model.DataModel;
- import javax.faces.model.ListDataModel;
-
- import com.antbee.report.dao.UserDao;
- import com.antbee.report.hibernate.model.SysUser;
-
- public class UserBean {
- private UserDao userDao;
- private SysUser sysuser = new SysUser();
- private DataModel users=new ListDataModel();
-
-
- public DataModel getUsers() {
- users.setWrappedData(this.userDao.getUsers());
- return users;
- }
- public void setUsers(DataModel users) {
- this.users = users;
- }
-
- public UserDao getUserDao() {
- return userDao;
- }
- public void setUserDao(UserDao userDao) {
- this.userDao = userDao;
- }
- public SysUser getSysuser() {
- return sysuser;
- }
- public void setSysuser(SysUser sysuser) {
- this.sysuser = sysuser;
- }
- }
Spring的配置:
xml 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
-
- <bean id="SessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- </bean>
-
- <bean id="TransactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager"
- abstract="false" singleton="true" lazy-init="default"
- autowire="default" dependency-check="default">
- <property name="sessionFactory">
- <ref bean="SessionFactory" />
- </property>
- </bean>
-
- <bean id="BaseTransactionProxy"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
- abstract="true">
- <property name="transactionManager">
- <ref bean="TransactionManager" />
- </property>
- <property name="proxyTargetClass">
- <value>true</value>
- </property>
- <property name="transactionAttributes">
- <props>
- <prop key="save*">PROPAGATION_REQUIRED</prop>
- <prop key="remove*">PROPAGATION_REQUIRED</prop>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
-
- <bean id="userDao" class="com.antbee.report.daoimpl.UserDaoImpl">
- <property name="sessionFactory">
- <ref local="SessionFactory" />
- </property>
- </bean>
- <bean id="userDaoProxy" parent="BaseTransactionProxy"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <property name="target">
- <ref local="userDao" />
- </property>
- </bean>
-
- </beans>
face-config.xml
xml 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
-
- <faces-config>
- <application>
- <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
- </application>
- <managed-bean>
- <managed-bean-name>userBean</managed-bean-name>
- <managed-bean-class>com.antbee.report.action.UserBean</managed-bean-class>
- <managed-bean-scope>session</managed-bean-scope>
- <managed-property>
- <property-name>userDao</property-name>
- <value>#{userDao}</value>
- </managed-property>
- </managed-bean>
- <navigation-rule>
- <from-view-id>/index.jsp</from-view-id>
- <navigation-case>
- <to-view-id>/browse.jsp</to-view-id>
- </navigation-case>
- </navigation-rule>
- </faces-config>
呵呵 ,写个页面看看效果.
index.jsp
java 代码
- <html>
- <head>
- <title>Index</title>
- </head>
- <body>
- <a href="browse.faces">Login</a>
- </body>
- </html>
显示所有用户 browse.jsp
java 代码
- <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
- <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ page language="java" pageEncoding="UTF-8"%>
- <html>
- <head>
- <title>
- Browse User
- </title>
- </head>
- <body>
- <f:view>
- <h:form>
- <h:dataTable id="table" value="#{userBean.users}" var="u" border="1" width="80%">
- <h:column>
- <f:facet name="header">
- <f:verbatim escape="true">ID</f:verbatim>
- </f:facet>
- <h:outputText value="#{u.id}" />
- </h:column>
- <h:column>
- <f:facet name="header">
- <f:verbatim escape="true">User Name</f:verbatim>
- </f:facet>
- <h:commandLink id="username" value="#{u.userName}">
- </h:commandLink>
- </h:column>
- <h:column>
- <f:facet name="header">
- <f:verbatim escape="true">Password</f:verbatim>
- </f:facet>
- <h:outputText value="#{u.passWordMd5}" />
- </h:column>
- <h:column>
- <f:facet name="header">
- <f:verbatim escape="true">Operation</f:verbatim>
- </f:facet>
- </h:column>
- </h:dataTable>
- </h:form>
- <br>
- </f:view>
- </body>
- </html>
大功告成,如果你没有配置成功,请你检查一下你的版本,如果还有问题,请给你留言,呵呵.