创建实体类
package com.pojo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "users", catalog = "test") public class Users implements java.io.Serializable { // Fields private Integer id; private String user; private String pwd; private String email; // Constructors /** default constructor */ public Users() { } /** full constructor */ public Users(String user, String pwd, String email) { this.user = user; this.pwd = pwd; this.email = email; } // Property accessors @Id @GeneratedValue @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "user", nullable = false, length = 10) public String getUser() { return this.user; } public void setUser(String user) { this.user = user; } @Column(name = "pwd", nullable = false, length = 10) public String getPwd() { return this.pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Column(name = "email", nullable = false, length = 50) public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } }
创建dao类
import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.LockMode; import org.springframework.context.ApplicationContext; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class UsersDAO extends HibernateDaoSupport { private static final Log log = LogFactory.getLog(UsersDAO.class); // property constants public static final String USER = "user"; public static final String PWD = "pwd"; public static final String EMAIL = "email"; protected void initDao() { // do nothing } public void save(Users transientInstance) { log.debug("saving Users instance"); try { getHibernateTemplate().save(transientInstance); log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } } public void delete(Users persistentInstance) { log.debug("deleting Users instance"); try { getHibernateTemplate().delete(persistentInstance); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public Users findById(java.lang.Integer id) { log.debug("getting Users instance with id: " + id); try { Users instance = (Users) getHibernateTemplate().get( "com.pojo.Users", id); return instance; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } @SuppressWarnings("unchecked") public List findByExample(Users instance) { log.debug("finding Users instance by example"); try { List results = getHibernateTemplate().findByExample(instance); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } } @SuppressWarnings("unchecked") public List findByProperty(String propertyName, Object value) { log.debug("finding Users instance with property: " + propertyName + ", value: " + value); try { String queryString = "from Users as model where model." + propertyName + "= ?"; return getHibernateTemplate().find(queryString, value); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } @SuppressWarnings("unchecked") public List findByUser(Object user) { return findByProperty(USER, user); } @SuppressWarnings("unchecked") public List findByPwd(Object pwd) { return findByProperty(PWD, pwd); } @SuppressWarnings("unchecked") public List findByEmail(Object email) { return findByProperty(EMAIL, email); } @SuppressWarnings("unchecked") public List findAll() { log.debug("finding all Users instances"); try { String queryString = "from Users"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } public Users merge(Users detachedInstance) { log.debug("merging Users instance"); try { Users result = (Users) getHibernateTemplate().merge( detachedInstance); log.debug("merge successful"); return result; } catch (RuntimeException re) { log.error("merge failed", re); throw re; } } public void attachDirty(Users instance) { log.debug("attaching dirty Users instance"); try { getHibernateTemplate().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public void attachClean(Users instance) { log.debug("attaching clean Users instance"); try { getHibernateTemplate().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public static UsersDAO getFromApplicationContext(ApplicationContext ctx) { return (UsersDAO) ctx.getBean("UsersDAO"); } }
创建seam的bean控制
package com.bean; import java.io.Serializable; import java.util.List; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.Factory; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; import org.jboss.seam.annotations.datamodel.DataModel; import org.jboss.seam.annotations.datamodel.DataModelSelection; import com.pojo.Users; import com.pojo.UsersDAO; @Name("userList") @Scope(ScopeType.CONVERSATION) public class UserList implements Serializable { private static final long serialVersionUID = -3803430546141700023L; private int scrollerPage = 1; @In("#{UsersDAO}") private UsersDAO usersDAO = null; @DataModel private List<Users> users = null; @DataModelSelection private Users u; @Factory("users") public void init(){ this.users = usersDAO.findAll(); } public void resetpwd(){ this.u.setPwd("kenter@grace"); this.usersDAO.save(this.u); } public void delete(){ this.usersDAO.delete(this.u); } public void setScrollerPage(int scrollerPage) { this.scrollerPage = scrollerPage; } public int getScrollerPage() { return scrollerPage; } }
创建页面
<?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:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich"> <head> <title>用户管理</title> <meta http-equiv="keywords" content="enter,your,keywords,here" /> <meta http-equiv="description" content="A short description of this page." /> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> </head> <body> <ui:composition> <h:form> <rich:datascroller align="left" for="userList" maxPages="20" id="sc1" /> <rich:spacer height="30" /> <rich:dataTable width="483" id="userList" rows="10" columnClasses="col" value="#{users}" var="user"> <f:facet name="header"> <rich:columnGroup> <h:column> <h:outputText styleClass="headerText" value="ID" /> </h:column> <h:column> <h:outputText styleClass="headerText" value="账号" /> </h:column> <h:column> <h:outputText styleClass="headerText" value="密码" /> </h:column> <h:column> <h:outputText styleClass="headerText" value="操作" /> </h:column> </rich:columnGroup> </f:facet> <h:column> <h:outputText value="#{user.id}" /> </h:column> <h:column> <h:outputText value="#{user.user}" /> </h:column> <h:column> <h:outputText value="#{user.pwd}" /> </h:column> <h:column> <h:commandLink value="修改密码" action="#{userList.resetpwd}"></h:commandLink> <h:commandLink value="删除" action="#{userList.delete}"></h:commandLink> </h:column> </rich:dataTable> </h:form> </ui:composition> </body> </html>
然后运行工程预览