整合 JSF+Spring+Hibernate的小例子

(1)UserInfo.java
package com.zhaoqingjie.service.hibernate;

import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;

public class UserInfo implements Serializable{
  
    private String id;
  
    private String username;
  
    private String email;
  
    public UserInfo(String id,String username,String email){
      
        this.id = id;
        this.username = username;
        this.email = email;
            
    }
  
    public UserInfo(){
      
    }
  
    public UserInfo(String id){
        this.id = id;   
    }
  
    public String getId(){
        return id;   
    }
  
    public void setId(String id){
        this.id = id;   
    }
  
    public String getEmail(){
        return email;   
    }
  
    public void setEmail(String email){
        this.email = emial;   
    }
  
    public String getUsername(){
        return username;   
    }   
  
    public void setUsername(String username){
        this.username = username;   
    }
  
    public String toString(){
        return new ToStringBuilder(this).append("id",getId()).toString();   
    }
  

}
(2)UserInfo.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
 
<hibernate-mapping>
<class name="com.zhaoqingjie.spring.service.hibernate.UserInfo" table="userinfo">
<meta attribute="class-description" inherit="false">
    @hibernate.class
    table="userinfo"
</meta>

<id name="id" type="java.lang.String" column="id">
<meta attribute="field-description">
    @hibernate.id
    generator-class="assigned"
    type="java.lang.String"
    column="id"
</meta>
<generator class="assigned"/>
</id>

<property name="username" type="java.lang.String" column="username" length="36">
<meta attribute="field-description">
    @hibernate.property
    column="username"
    length="36"
</meta>
</property>

<property name="email" type="java.lang.String" column="email" length="60">
<meta attribute="field-description">
    @hibernate.property
    column="email"
    length="60"
</meta>
</property>

</class>
</hibernate-mapping>

-------------------------------------------------------------------------------------------------------
(3)IUserInfoDAO.java
package com.zhaoqingjie.spring.service.dao;

import com.openv.spring.domainmodel.UserInfoVO;
import org.springframework.dao.DataAccessException;
import java.io.Serializable;

public interface IUserInfoDAO extends Serializable{
    public boolean setUserInfo(UserInfoVO userinfoVO)throws DataAccessException;
  
      
}

(4)UserInfoDAO.java
package com.zhaoqingjie.spring.service.dao.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.doomdark.uuid.UUIDGenerator;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import com.zhaoqingjie.spring.domainmodel.UserInfoVO;
import com.zhaoqingjie.spring.service.dao.IUserInfoDAO;
import com.zhaoqingjie.spring.service.hibernate.UserInfo;

public class UserInfoDAO extends HibernateDaoSupport implements IUserInfoDAO{
    private static final Log log = LogFactory.getLog(UserInfoDAO.class);
  
    public boolean setUserInfo(UserInfoVO userinfoVO)throws DataAccessException{
        if(userinfoVO == null){
              return false;   
        }   
      
        UserInfo ui = new UserInfo();
        ui.setId(getID());
        ui.setUsername(userinfoVO.getUsername().trim());
        ui.setEmail(userinfoVO.getEmail().trim());
        this.getHibernateTemplate().save(ui);
        return true;
      
    }   
  
    private String getID(){
        return UUIDGenerator.getInatance().generateTimeBaseUUID().toString();   
    }
}

--------------------------------------------------------------------------------------------------
(5)IExampleManager.java

package com.zhaoqingjie.spring.service;
import com.openv.spring.domainmodel.UserInfoVO;
import org.springframework.dao.DataAccessException;
import java.io.Serializable;


public interface IExampleManager extends Serializable {
  public boolean setUserInfo(UserInfoVO userinfoVO)
        throws DataAccessException;
}
(6)ExampleManagerImpl.java
package com.zhaoqingjie.spring.service.impl;

import com.openv.spring.domainmodel.UserInfoVO;
import com.openv.spring.service.IExample29Manager;
import com.openv.spring.service.dao.IUserInfoDAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.dao.DataAccessException;

public class ExampleManagerImpl implements IExampleManager {
  private static final Log log = LogFactory
        .getLog(ExampleManagerImpl.class);

  private IUserInfoDAO userinfo;

  public ExampleManagerImpl() {
    log.info("ExampleManagerImpl()...................");
  }

  public void setUserinfo(IUserInfoDAO userinfoDAO)
        throws DataAccessException {
    this.userinfo = userinfoDAO;
  }

  public boolean setUserInfo(UserInfoVO userinfoVO)
        throws DataAccessException {
    return userinfo.setUserInfo(userinfoVO);
  }
}

-----------------------------------------------------------------------------
(7)UserInfoVO.java
package com.zhaoqingjie.spring.domainmodel;

import java.io.Serializable;

public class UserInfoVO implements Serializable{
  
    private String username;
    private String email;
  
    public     String getEmail(){
        return email;   
    }
  
    public void setEmail(String email){
        this.email = email;   
    }
  
    public String getUsername(){
        return username;   
    }
  
    public void setUsername(String username){
        this.username = username;   
    }
}

------------------------------------------------------------------------------------------
JSF表示层
(8)InfoBean.java
package com.zhaoqingjie.spring.jsf;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.openv.spring.domainmodel.UserInfoVO;
import com.openv.spring.service.IExampleManager;

public class InfoBean {
  private static final Log log = LogFactory.getLog(InfoBean.class);

  private String username = null;

  private String email = null;

  private String response = null;

  private long maximum = 0;

  private boolean maximumSet = false;

  private long minimum = 0;

  private boolean minimumSet = false;
 

  public InfoBean() {
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }
  public String getUsername() {
    return username;
  }
  public void setUsername(String username) {
    this.username = username;
  }

  public String submitPersonInfo() {
    log.info(username);
    log.info(email);

    ApplicationContext ac = FacesContextUtils
          .getWebApplicationContext(FacesContext.getCurrentInstance());
    IExampleManager em = (IExampleManager) ac
          .getBean("exampleService");
    UserInfoVO uiVO = new UserInfoVO();
    uiVO.setUsername(username);
    uiVO.setEmail(email);

    boolean flag = em.setUserInfo(uiVO);


    if (flag) {
        setResponse("注册成功");

        return "success";
    } else {
        setResponse("注册失败");

        return "failure";
    }
  }

  public void setResponse(String response) {
    this.response = response;
  }

  public String getResponse() {
    return null;
  }

  public long getMaximum() {
    return (this.maximum);
  }

  public void setMaximum(long maximum) {
    this.maximum = maximum;
    this.maximumSet = true;
  }

  public long getMinimum() {
    return (this.minimum);
  }

  public void setMinimum(long minimum) {
    this.minimum = minimum;
    this.minimumSet = true;
  }
}
(9)index.jsp
<html>
<head>
</head>
<body>
<jsp:forward page="/example/home.jsp" />
</body>
</html>
(10)home.jsp
<%@ page contentType="text/html; charset=gbk" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>
用户注册
</title>
</head>
<br>
  <f:view>
  <h:form id="helloForm" >
        <table border="10" align="center"
            bordercolor="#0099CC" cellpadding="6" bordercolorlight="#999999">
              <tr>
                  <td colspan="2" bgcolor="#66CCFF">输入用户注册信息:</td>
              </tr>
              <tr>
                <td>
                  <div align="right">用户名</div>
                </td>
                <td>
                        <h:inputText id="username" value="#{InfoBean.username}">
                        <f:validateLength minimum="#{InfoBean.minimum}"
                            maximum="#{InfoBean.maximum}" />
                    </h:inputText>
                </td>
              </tr>
              <tr>
                <td>
                  <div align="right">E_mail</div>
                </td>
                <td>
                        <h:inputText id="email" value="#{InfoBean.email}"/>
                </td>
              </tr>
              <tr>
                  <td colspan="2" bgcolor="#FFFF40">
                      <span>   
                            <h:message id="message"
                                for="username"/></span>
                  </td>
              </tr>
              <tr>
                  <td align="center" colspan="2">
                        <h:commandButton id="submit"
                            action="#{InfoBean.submitPersonInfo}" value="提交" />
                  </td>
              </tr>   
        </table>
  </h:form>
  </f:view>
</html>

(11)success.jsp
<%@ page contentType="text/html; charset=gbk" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>
用户注册成功
</title>
</head>
<body bgcolor="white">
  <f:view>
    <h:form id="responseForm">
        <h:graphicImage id="successImg"
            url="images/form-success.jpg" alt="注册成功!"/>
        <h2>
        <h:outputText id="result"
                  value="#{InfoBean.response}"/></h2> 
            <h:commandButton id="back"
                value="返回" action="su"/>
            <p>
      </h:form>
  </f:view>
</html>
(12)failure.jsp
<%@ page contentType="text/html; charset=gbk" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>
用户注册失败
</title>
</head>
<body bgcolor="white">
  <f:view>
    <h:form id="responseForm">
        <h:graphicImage id="successImg"
            url="images/form-error.jpg" alt="注册失败!"/>
        <h2>
        <h:outputText id="result"
                  value="#{InfoBean.response}"/></h2> 
            <h:commandButton id="back"
                value="返回" action="su"/>
            <p>
      </h:form>
  </f:view>
</html>
(13)web.xml
<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">
 
  <display-name>example</display-name>

    <context-param>   
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

    <servlet>
        <display-name>FacesServlet</display-name>
        <servlet-name>FacesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
      <servlet-name>FacesServlet</servlet-name>
      <url-pattern>/example/*</url-pattern>
    </servlet-mapping>
    
</web-app>
(14)applicationContext.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="dataSource"
        class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
        <value>java:/MySqlDS</value>
    </property>
  </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
        <property name="dataSource">
              <ref local="dataSource"/>
        </property>
        <property name="mappingResources">
              <list>
                  <value>
                      com/openv/spring/service/hibernate/UserInfo.hbm.xml
                  </value>
              </list>
        </property>
        <property name="hibernateProperties">
              <props>
                  <prop key="hibernate.dialect">
                      net.sf.hibernate.dialect.MySQLDialect
                  </prop>
                  <prop key="hibernate.show_sql">
                      true
                  </prop>
              </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate.HibernateTransactionManager">
        <property name="sessionFactory">
              <ref local="sessionFactory"/>
        </property>
    </bean>

    <bean id="exampleServiceTarget"
        class="com.openv.spring.service.impl.Example29ManagerImpl">
        <property name="userinfo">
              <ref local="userinfoDAO"/>
        </property>
    </bean>   
  
    <bean id="exampleService"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager">
              <ref local="transactionManager"/>
        </property>
        <property name="target">
              <ref local="exampleServiceTarget"/>
        </property>
        <property name="transactionAttributes">
              <props>
                  <prop key="get*">
                      PROPAGATION_REQUIRED,readOnly
                  </prop>
                  <prop key="set*">
                      PROPAGATION_REQUIRED
                  </prop>
              </props>
        </property>
    </bean>
  
    <bean id="userinfoDAO"
        class="com.openv.spring.service.dao.impl.UserInfoDAO">
        <property name="sessionFactory">
              <ref local="sessionFactory"/>
        </property>
    </bean>
  
</beans>
(15)faces-config.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>
  <locale-config>
    <default-locale>zh_CN</default-locale>
  </locale-config>
</application>

<navigation-rule>
  <description>
      JSF Home Page
  </description>
  <from-view-id>/home.jsp</from-view-id>
  <navigation-case>
    <description>
          success
    </description>
    <from-outcome>success</from-outcome>
    <to-view-id>/success.jsp</to-view-id>
  </navigation-case>
  <navigation-case>
    <description>
          failure
    </description>
    <from-outcome>failure</from-outcome>
    <to-view-id>/failure.jsp</to-view-id>
  </navigation-case>
</navigation-rule>

<navigation-rule>
  <description>
  </description>
  <from-view-id>/success.jsp</from-view-id>
  <navigation-case>
    <description>
    </description>
    <from-outcome>su</from-outcome>
    <to-view-id>/home.jsp</to-view-id>
  </navigation-case>
</navigation-rule>

<navigation-rule>
  <description>
  </description>
  <from-view-id>/failure.jsp</from-view-id>
  <navigation-case>
    <description>
    </description>
    <from-outcome>su</from-outcome>
    <to-view-id>/home.jsp</to-view-id>
  </navigation-case>
</navigation-rule>

<managed-bean>
  <description>
      InfoBean
  </description>
  <managed-bean-name>InfoBean</managed-bean-name>
  <managed-bean-class>
      com.openv.spring.jsf.InfoBean
  </managed-bean-class>
 
  <managed-bean-scope>session</managed-bean-scope>
  <managed-property>
    <property-name>minimum</property-name>
    <property-class>long</property-class>
    <value>6</value>
  </managed-property>
 
  <managed-property>
    <property-name>maximum</property-name>
    <property-class>long</property-class>
    <value>18</value>
  </managed-property>
</managed-bean>

</faces-config>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值