服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

整合 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="http://www.itus.cnimages/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="http://www.itus.cnimages/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> 

扫描关注微信公众号