熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

整合 JSF+Spring+Hibernate的小例子

2022-06-13   來源: Java開源技術 

  ()UserInfojava


   package comzhaoqingjieservicehibernate;

import javaioSerializable;
import monslangbuilderToStringBuilder;

public class UserInfo implements Serializable{
   
    private String id;
   
    private String username;
   
    private String email;
   
    public UserInfo(String idString usernameString email){
       
        thisid = id;
        thisusername = username;
        thisemail = email;
             
    }
   
    public UserInfo(){
       
    }
   
    public UserInfo(String id){
        thisid = id;    
    }
   
    public String getId(){
        return id;    
    }
   
    public void setId(String id){
        thisid = id;    
    }
   
    public String getEmail(){
        return email;    
    }
   
    public void setEmail(String email){
        thisemail = emial;    
    }
   
    public String getUsername(){
        return username;    
    }    
   
    public void setUsername(String username){
        thisusername = username;    
    }
   
    public String toString(){
        return new ToStringBuilder(this)append(idgetId())toString();    
    }
   

  ()UserInfohbmxml


   <?xml version=?>
<!DOCTYPE hibernatemapping PUBLIC
  //Hibernate/Hibernate Mapping DTD //EN
  mappingdtd >
 
<hibernatemapping>
<class name=comzhaoqingjiespringservicehibernateUserInfo table=userinfo>
<meta attribute=classdescription inherit=false>
    @hibernateclass
    table=userinfo
</meta>

<id name=id type=javalangString column=id>
<meta attribute=fielddescription>
    @hibernateid
    generatorclass=assigned
    type=javalangString
    column=id
</meta>
<generator class=assigned/>
</id>

<property name=username type=javalangString column=username length=>
<meta attribute=fielddescription>
    @hibernateproperty
    column=username
    length=
</meta>
</property>

<property name=email type=javalangString column=email length=>
<meta attribute=fielddescription>
    @hibernateproperty
    column=email
    length=
</meta>
</property>

</class>
</hibernatemapping> 
 

  ()IUserInfoDAOjava

   package comzhaoqingjiespringservicedao;

import comopenvspringdomainmodelUserInfoVO;
import orgspringframeworkdaoDataAccessException;
import javaioSerializable;

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

  ()UserInfoDAOjava

   package comzhaoqingjiespringservicedaoimpl;

import monsloggingLog;
import monsloggingLogFactory;
import orgdoomdarkuuidUUIDGenerator;
import orgspringframeworkdaoDataAccessException;
import orgspringframeworkormhibernatesupportHibernateDaoSupport;
import comzhaoqingjiespringdomainmodelUserInfoVO;
import comzhaoqingjiespringservicedaoIUserInfoDAO;
import comzhaoqingjiespringservicehibernateUserInfo;

public class UserInfoDAO extends HibernateDaoSupport implements IUserInfoDAO{
    private static final Log log = LogFactorygetLog(UserInfoDAOclass);
   
    public boolean setUserInfo(UserInfoVO userinfoVO)throws DataAccessException{
        if(userinfoVO == null){
              return false;    
        }    
       
        UserInfo ui = new UserInfo();
        uisetId(getID());
        uisetUsername(userinfoVOgetUsername()trim());
        uisetEmail(userinfoVOgetEmail()trim());
        thisgetHibernateTemplate()save(ui);
        return true;
       
    }    
   
    private String getID(){
        return UUIDGeneratorgetInatance()generateTimeBaseUUID()toString();    
    }

  ()IExampleManagerjava


   package comzhaoqingjiespringservice;
import comopenvspringdomainmodelUserInfoVO;
import orgspringframeworkdaoDataAccessException;
import javaioSerializable;


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

  ()ExampleManagerImpljava

   package comzhaoqingjiespringserviceimpl;

import comopenvspringdomainmodelUserInfoVO;
import comopenvspringserviceIExampleManager;
import comopenvspringservicedaoIUserInfoDAO;

import monsloggingLog;
import monsloggingLogFactory;

import orgspringframeworkdaoDataAccessException;

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

  private IUserInfoDAO userinfo;

  public ExampleManagerImpl() {
    (ExampleManagerImpl());
  }

  public void setUserinfo(IUserInfoDAO userinfoDAO)
        throws DataAccessException {
    thisuserinfo = userinfoDAO;
  }

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

  ()UserInfoVOjava

    package comzhaoqingjiespringdomainmodel;

import javaioSerializable;

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

  JSF表示層
()InfoBeanjava


   package comzhaoqingjiespringjsf;

import ntextFacesContext;

import monsloggingLog;
import monsloggingLogFactory;
import orgntextApplicationContext;
import orgspringframeworkwebjsfFacesContextUtils;

import comopenvspringdomainmodelUserInfoVO;
import comopenvspringserviceIExampleManager;

public class InfoBean {
  private static final Log log = LogFactorygetLog(InfoBeanclass);

  private String username = null;

  private String email = null;

  private String response = null;

  private long maximum = ;

  private boolean maximumSet = false;

  private long minimum = ;

  private boolean minimumSet = false;
 

  public InfoBean() {
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    thisemail = email;
  }
  public String getUsername() {
    return username;
  }
  public void setUsername(String username) {
    thisusername = username;
  }

  public String submitPersonInfo() {
    (username);
    (email);

    ApplicationContext ac = FacesContextUtils
          getWebApplicationContext(FacesContextgetCurrentInstance());
    IExampleManager em = (IExampleManager) ac
          getBean(exampleService);
    UserInfoVO uiVO = new UserInfoVO();
    uiVOsetUsername(username);
    uiVOsetEmail(email);

    boolean flag = emsetUserInfo(uiVO);


    if (flag) {
        setResponse(注冊成功);

        return success;
    } else {
        setResponse(注冊失敗);

        return failure;
    }
  }

  public void setResponse(String response) {
    thisresponse = response;
  }

  public String getResponse() {
    return null;
  }

  public long getMaximum() {
    return (thismaximum);
  }

  public void setMaximum(long maximum) {
    thismaximum = maximum;
    thismaximumSet = true;
  }

  public long getMinimum() {
    return (thisminimum);
  }

  public void setMinimum(long minimum) {
    thisminimum = minimum;
    thisminimumSet = true;
  }

  ()indexjsp


   <html>
<head>
</head>
<body>
<jsp:forward page=/example/homejsp />
</body>
</html> 

  ()homejsp

   <%@ page contentType=text/html; charset=gbk %>
<%@ taglib uri= prefix=h %>
<%@ taglib uri= prefix=f %>
<html>
<head>
<title>
用戶注冊
</title>
</head>
<br>
  <f:view>
  <h:form id=helloForm >
        <table border= align=center
            bordercolor=#CC cellpadding= bordercolorlight=#>
              <tr>
                  <td colspan= bgcolor=#CCFF>輸入用戶注冊信息</td>
              </tr>
              <tr>
                <td>
                  <div align=right>用戶名</div>
                </td>
                <td>
                        <h:inputText id=username value=#{InfoBeanusername}>
                        <f:validateLength minimum=#{InfoBeanminimum}
                            maximum=#{InfoBeanmaximum} />
                    </h:inputText>
                </td>
              </tr>
              <tr>
                <td>
                  <div align=right>E_mail</div>
                </td>
                <td>
                        <h:inputText id=email value=#{InfoBeanemail}/>
                </td>
              </tr>
              <tr>
                  <td colspan= bgcolor=#FFFF>
                      <span>    
                            <h:message id=message
                                for=username/></span>
                  </td>
              </tr>
              <tr>
                  <td align=center colspan=>
                        <h:commandButton id=submit
                            action=#{InfoBeansubmitPersonInfo} value=提交 />
                  </td>
              </tr>    
        </table>
  </h:form>
  </f:view>
</html> 

  ()successjsp

   <%@ page contentType=text/html; charset=gbk %>
<%@ taglib uri= prefix=h %>
<%@ taglib uri= prefix=f %>
<html>
<head>
<title>
用戶注冊成功
</title>
</head>
<body bgcolor=white>
  <f:view>
    <h:form id=responseForm>
        <h:graphicImage id=successImg
            url=images/formsuccessjpg alt=注冊成功!/>
        <h>
        <h:outputText id=result
                  value=#{InfoBeanresponse}/></h>  
            <h:commandButton id=back
                value=返回 action=su/>
            <p>
      </h:form>
  </f:view>
</html> 

  ()failurejsp


   <%@ page contentType=text/html; charset=gbk %>
<%@ taglib uri= prefix=h %>
<%@ taglib uri= prefix=f %>
<html>
<head>
<title>
用戶注冊失敗
</title>
</head>
<body bgcolor=white>
  <f:view>
    <h:form id=responseForm>
        <h:graphicImage id=successImg
            url=images/formerrorjpg alt=注冊失敗!/>
        <h>
        <h:outputText id=result
                  value=#{InfoBeanresponse}/></h>  
            <h:commandButton id=back
                value=返回 action=su/>
            <p>
      </h:form>
  </f:view>
</html>  

  ()webxml

   <?xml version=?>
<webapp xmlns=
  xmlns:xsi=instance
  xsi:schemaLocation=
    app__xsd
  version=>
 
  <displayname>example</displayname>

    <contextparam>    
        <paramname>contextConfigLocation</paramname>
        <paramvalue>/WEBINF/applicationContextxml</paramvalue>
    </contextparam>

    <listener>
      <listenerclass>orgsprntextContextLoaderListener</listenerclass>
      </listener>

    <servlet>
        <displayname>FacesServlet</displayname>
        <servletname>FacesServlet</servletname>
        <servletclass>javaxfaceswebappFacesServlet</servletclass>
        <loadonstartup></loadonstartup>
  </servlet>

  <servletmapping>
      <servletname>FacesServlet</servletname>
      <urlpattern>/example/*</urlpattern>
    </servletmapping>
     
</webapp> 

  ()applicationContextxml

   <?xml version= encoding=UTF?>
<!DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN
    beansdtd>
<beans>

    <bean id=dataSource
        class=orgspringframeworkjndiJndiObjectFactoryBean>
    <property name=jndiName>
        <value>java:/MySqlDS</value>
    </property>
  </bean>

    <bean id=sessionFactory
        class=orgspringframeworkormhibernateLocalSessionFactoryBean>
        <property name=dataSource>
              <ref local=dataSource/>
        </property>
        <property name=mappingResources>
              <list>
                  <value>
                      com/openv/spring/service/hibernate/UserInfohbmxml
                  </value>
              </list>
        </property>
        <property name=hibernateProperties>
              <props>
                  <prop key=hibernatedialect>
                      netsfhibernatedialectMySQLDialect
                  </prop>
                  <prop key=hibernateshow_sql>
                      true
                  </prop>
              </props>
        </property>
    </bean>

    <bean id=transactionManager
        class=orgspringframeworkormhibernateHibernateTransactionManager>
        <property name=sessionFactory>
              <ref local=sessionFactory/>
        </property>
    </bean>

    <bean id=exampleServiceTarget
        class=comopenvspringserviceimplExampleManagerImpl>
        <property name=userinfo>
              <ref local=userinfoDAO/>
        </property>
    </bean>    
   
    <bean id=exampleService
        class=orgspringframeworktransactioninterceptorTransactionProxyFactoryBean>
        <property name=transactionManager>
              <ref local=transactionManager/>
        </property>
        <property name=target>
              <ref local=exampleServiceTarget/>
        </property>
        <property name=transactionAttributes>
              <props>
                  <prop key=get*>
                      PROPAGATION_REQUIREDreadOnly
                  </prop>
                  <prop key=set*>
                      PROPAGATION_REQUIRED
                  </prop>
              </props>
        </property>
    </bean>
   
    <bean id=userinfoDAO
        class=comopenvspringservicedaoimplUserInfoDAO>
        <property name=sessionFactory>
              <ref local=sessionFactory/>
        </property>
    </bean>
   
</beans> 

  ()facesconfigxml


   <?xml version= encoding=UTF?>
<!DOCTYPE facesconfig PUBLIC
//Sun Microsystems Inc//DTD JavaServer Faces Config //EN
facesconfig__dtd>
<facesconfig>

<application>
  <localeconfig>
    <defaultlocale>zh_CN</defaultlocale>
  </localeconfig>
</application>

<navigationrule>
  <description>
      JSF Home Page
  </description>
  <fromviewid>/homejsp</fromviewid>
  <navigationcase>
    <description>
          success
    </description>
    <fromoutcome>success</fromoutcome>
    <toviewid>/successjsp</toviewid>
  </navigationcase>
  <navigationcase>
    <description>
          failure
    </description>
    <fromoutcome>failure</fromoutcome>
    <toviewid>/failurejsp</toviewid>
  </navigationcase>
</navigationrule>

<navigationrule>
  <description>
  </description>
  <fromviewid>/successjsp</fromviewid>
  <navigationcase>
    <description>
    </description>
    <fromoutcome>su</fromoutcome>
    <toviewid>/homejsp</toviewid>
  </navigationcase>
</navigationrule>

<navigationrule>
  <description>
  </description>
  <fromviewid>/failurejsp</fromviewid>
  <navigationcase>
    <description>
    </description>
    <fromoutcome>su</fromoutcome>
    <toviewid>/homejsp</toviewid>
  </navigationcase>
</navigationrule>

<managedbean>
  <description>
      InfoBean
  </description>
  <managedbeanname>InfoBean</managedbeanname>
  <managedbeanclass>
      comopenvspringjsfInfoBean
  </managedbeanclass>
 
  <managedbeanscope>session</managedbeanscope>
  <managedproperty>
    <propertyname>minimum</propertyname>
    <propertyclass>long</propertyclass>
    <value></value>
  </managedproperty>
 
  <managedproperty>
    <propertyname>maximum</propertyname>
    <propertyclass>long</propertyclass>
    <value></value>
  </managedproperty>
</managedbean>

</facesconfig> 


From:http://tw.wingwit.com/Article/program/Java/ky/201311/27861.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.