`

jsf+spring+hibernate

阅读更多
<!--

鼓励转载,请务必注明出处!

最近项目中用到jsf+spring+hibernate, 顺便做了个小例子,我已经调试通过了,分享给大家.

1.InfoBean

package com.jsf;

import com.vo.UserVO;
import com.web.bean.UserBean;
import com.bo.UserService;
import com.framework.web.bean.BaseManagedBean;

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

private static final long serialVersionUID = 1L;

private UserBean userBean = null;

private String username = null;

private String email = null;

private String response = null;

private long maximum = 0;

private long minimum = 0;

private boolean maximumSet = false;

private boolean minimumSet = false;

public InfoBean() {
}

public String submitPersonInfo() {

  // log.info(username);
  // log.info(email);

  //jsf-spring 1 ok
//  UserService userService = (UserService)getbean("userService");
 
  //jsf-spring 2 ok
  UserService userService = (UserService)getUserBean().getUserService();

 
  UserVO userVO = new UserVO();
  userVO.setUsername(username);
  userVO.setEmail(email);

  boolean flag = userService.saveUser(userVO);

  if (flag) {
   setResponse("register success");
   return "success";
  } else {
   setResponse("register failed");
   return "failure";
  }
}

public UserBean getUserBean() {
 
  if (userBean == null) {
   userBean = (UserBean)getWebbean("UserBean");
  }
  return userBean;
}

public void setUserBean(UserBean userBean) {
  this.userBean = userBean;
}

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

public void setMinimum(long minimum) {
  this.minimum = minimum;
  this.minimumSet = true;
}

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

public String getResponse() {
  return null;
}

public long getMaximum() {
  return maximum;
}

public long getMinimum() {
  return minimum;
}

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;
}
}


2.UserService

package com.bo;

import com.vo.UserVO;
import org.springframework.dao.DataAccessException;
import java.io.Serializable;


public interface UserService extends Serializable {
   public boolean saveUser(UserVO userVO) throws DataAccessException;
}


3.UserServiceImpl

package com.bo.impl;

import com.bo.UserService;
import com.dao.UserDAO;
import com.vo.UserVO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;

public class UserServiceImpl implements UserService {

private static final long serialVersionUID = 1L;

private static final Log log = LogFactory.getLog(UserServiceImpl.class);

private UserDAO userDAO;

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

public void setUserDAO(UserDAO userDAO) {
  this.userDAO = userDAO;
}

public boolean saveUser(UserVO userVO) throws DataAccessException {
  return userDAO.saveUser(userVO);
}

public UserDAO getUserDAO() {
  return userDAO;
}
}


4.UserDAO

package com.dao;

import org.springframework.dao.DataAccessException;
import com.vo.UserVO;
import java.io.Serializable;

public interface UserDAO extends Serializable{
    public boolean saveUser(UserVO userVO)throws DataAccessException;
   
       
}


5.UserDAOImpl

package com.dao.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.vo.UserVO;
import com.dao.UserDAO;
import com.hibernate.UserInfo;
//import org.doomdark.uuid.UUIDGenerator;

public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {

    private static final Log log = LogFactory.getLog(UserDAOImpl.class);

    public boolean saveUser(UserVO userVO) throws DataAccessException{
        if(userVO == null){
              return false;    
        }    
       
        UserInfo userInfo = new UserInfo();
        //ui.setId(getID());
        userInfo.setId("4");
        userInfo.setUsername(userVO.getUsername().trim());
        userInfo.setEmail(userVO.getEmail().trim());
        this.getHibernateTemplate().save(userInfo);
        return true;
       
    }
   
//    private String getID(){
//        return UUIDGenerator.getInatance().generateTimeBaseUUID().toString();    
//    }
}

6.UserVO

package com.vo;

import java.io.Serializable;

public class UserVO 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;    
    }
}


7.BaseManagedBean

package com.framework.web.bean;

import java.io.Serializable;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

public class BaseManagedBean implements Serializable{

public static HttpServletRequest request;

public static HttpSession session;

static{
  FacesContext context =FacesContext.getCurrentInstance();
  request = (HttpServletRequest)context.getExternalContext().getRequest();
  session = (HttpSession) context.getExternalContext().getSession(false);
}

public Object getbean(String beanName){
  ApplicationContext ac = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
  return ac.getBean(beanName);
}


public Object getWebbean(String beanname){
  FacesContext context =FacesContext.getCurrentInstance();
  Application app = context.getApplication();
  ValueBinding binding = app.createValueBinding("#{" + beanname + "}");
  Object object = binding.getValue(context);
  return object;
}


public String getExternalParameter(String parameter){
  FacesContext context =FacesContext.getCurrentInstance();
  return context.getExternalContext().getInitParameter(parameter);
}
}


8.UserBean

package com.web.bean;

import com.bo.UserService;


public class UserBean {

private UserService userService = null;

public UserService getUserService() {
  return userService;
}

public void setUserService(UserService userService) {
  this.userService = userService;
}

}


9.UserInfo

package com.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 = email;    
    }
   
    public String getUsername(){
        return username;    
    }    
   
    public void setUsername(String username){
        this.username = username;    
    }
   
//    public String toString(){
//        return new ToStringBuilder(this).append("id",getId()).toString();    
//    }
   
}

10.UserInfo.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.hibernate.UserInfo" table="user">
  <id name="id" type="java.lang.String" column="id">
   <generator class="assigned" />
  </id>
  <property name="username" type="java.lang.String"
   column="username" length="36">
  </property>
  <property name="email" type="java.lang.String" column="email"
   length="60">
  </property>
</class>
</hibernate-mapping>

11.web.xml

<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                         "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>             
<display-name>TestFrame</display-name>

<context-param>
  <param-name>javax.faces.CONFIG_FILES</param-name>
  <param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
  <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</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>
<login-config>
  <auth-method>BASIC</auth-method>
</login-config>
</web-app>


12.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
  class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
  <property name="driverClassName">
   <value>org.hsqldb.jdbcDriver</value>
  </property>
  <property name="url">
   <value>jdbc:hsqldb:hsql://localhost</value>
  </property>
  <property name="username">
   <value>sa</value>
  </property>
  <property name="password">
   <value/>
  </property>
</bean>
<bean
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" id="sessionFactory">
  <property name="dataSource">
   <ref local="dataSource"/>
  </property>
  <property name="mappingResources">
   <list>
    <value>com/hibernate/UserInfo.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
</bean>
<bean
  class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="transactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
</bean>
<bean
  class="org.springframework.transaction.interceptor.TransactionInterceptor" id="txInterceptor">
  <property name="transactionManager">
   <ref bean="transactionManager"/>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="set*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
</bean>
<bean abstract="true"
  class="org.springframework.aop.framework.ProxyFactoryBean" id="baseUserService">
  <property name="interceptorNames">
   <list>
    <value>txInterceptor</value>
   </list>
  </property>
</bean>
<bean id="userService" parent="baseUserService">
  <property name="proxyInterfaces">
   <value>com.bo.UserService</value>
  </property>
  <property name="target">
   <bean class="com.bo.impl.UserServiceImpl">
    <property name="userDAO">
     <ref bean="userDAO"/>
    </property>
   </bean>
  </property>
</bean>
<bean class="com.dao.impl.UserDAOImpl" id="userDAO">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
</bean>
</beans>


13.faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
                              "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<managed-bean>
  <description>InfoBean</description>
  <managed-bean-name>InfoBean</managed-bean-name>
  <managed-bean-class>com.jsf.InfoBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
  <managed-property>
   <property-name>userBean</property-name>
   <property-class>com.web.bean.UserBean</property-class>
   <value>#{UserBean}</value>
  </managed-property>
  <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>
<managed-bean>
  <managed-bean-name>UserBean</managed-bean-name>
  <managed-bean-class>com.web.bean.UserBean</managed-bean-class>
  <managed-bean-scope>application</managed-bean-scope>
  <managed-property>
   <property-name>userService</property-name>
   <property-class>com.bo.UserService</property-class>
   <value>#{userService}</value>
  </managed-property>
</managed-bean>
<navigation-rule>
  <description>JSF Home Page</description>
  <from-view-id>/example/home.jsp</from-view-id>
  <navigation-case>
   <description>success</description>
   <from-outcome>success</from-outcome>
   <to-view-id>/example/success.jsp</to-view-id>
  </navigation-case>
  <navigation-case>
   <description>failure</description>
   <from-outcome>failure</from-outcome>
   <to-view-id>/example/failure.jsp</to-view-id>
  </navigation-case>
</navigation-rule>
<navigation-rule>
  <from-view-id>/example/success.jsp</from-view-id>
  <navigation-case>
   <from-outcome>su</from-outcome>
   <to-view-id>/example/home.jsp</to-view-id>
  </navigation-case>
</navigation-rule>
<navigation-rule>
  <from-view-id>/example/failure.jsp</from-view-id>
  <navigation-case>
   <from-outcome>su</from-outcome>
   <to-view-id>/example/home.jsp</to-view-id>
  </navigation-case>
</navigation-rule>
<application>
  <variable-resolver>
   org.springframework.web.jsf.DelegatingVariableResolver
  </variable-resolver>
  <locale-config>
   <default-locale>zh_CN</default-locale>
  </locale-config>
</application>
</faces-config>

14.faces-config.xml.bak_1


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
                              "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<managed-bean>
  <description>InfoBean</description>
  <managed-bean-name>InfoBean</managed-bean-name>
  <managed-bean-class>com.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>
<navigation-rule>
  <description>JSF Home Page</description>
  <from-view-id>/example/home.jsp</from-view-id>
  <navigation-case>
   <description>success</description>
   <from-outcome>success</from-outcome>
   <to-view-id>/example/success.jsp</to-view-id>
  </navigation-case>
  <navigation-case>
   <description>failure</description>
   <from-outcome>failure</from-outcome>
   <to-view-id>/example/failure.jsp</to-view-id>
  </navigation-case>
</navigation-rule>
<navigation-rule>
  <from-view-id>/example/success.jsp</from-view-id>
  <navigation-case>
   <from-outcome>su</from-outcome>
   <to-view-id>/example/home.jsp</to-view-id>
  </navigation-case>
</navigation-rule>
<navigation-rule>
  <from-view-id>/example/failure.jsp</from-view-id>
  <navigation-case>
   <from-outcome>su</from-outcome>
   <to-view-id>/example/home.jsp</to-view-id>
  </navigation-case>
</navigation-rule>
<application>
  <variable-resolver>
   org.springframework.web.jsf.DelegatingVariableResolver
  </variable-resolver>
  <locale-config>
   <default-locale>zh_CN</default-locale>
  </locale-config>
</application>
</faces-config>


15.home.jsp

<%@ page contentType="text/html; charset=gbk" %> 
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<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>
16.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>

17.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>

18.index.jsp

<html>
<head>
</head>
<body>
<jsp:forward page="/example/home.faces" />
</body>
</html>

-->
分享到:
评论

相关推荐

    JSF+Spring+Hibernate(框架整合)详细过程

    在这个场景中,我们关注的是将JavaServer Faces (JSF),Spring,以及Hibernate这三大框架进行整合。这三者分别是用于构建用户界面、管理应用上下文和服务、以及处理持久化的强大工具。以下是对"JSF+Spring+Hibernate...

    JSF+Spring+Hibernate小例子

    **JSF+Spring+Hibernate整合应用详解** 在Java Web开发中,JSF(JavaServer Faces)、Spring和Hibernate是三个常用的技术栈,它们分别负责视图层、业务逻辑层和服务数据持久化层。这个"JSF+Spring+Hibernate小例子...

    JSF+Spring+Hibernate jar lib

    JSF+Spring+Hibernate jar文件压缩包,hibernate最小配置,Spring 2.0 jar, richfaces

    JSF+Spring+Hibernate 分页显示

    本主题将深入探讨如何使用JavaServer Faces (JSF)、Spring框架和Hibernate ORM工具来实现SQL Server数据库中的数据分页显示。 **JavaServer Faces (JSF)** JSF是一个Java标准,用于构建企业级的Web应用程序。它提供...

    jsf+spring+hibernate架构的网上商店

    **JSF+Spring+Hibernate 架构的网上商店** 在当今的互联网开发中,JavaScript 面向服务器框架(JSF)、Spring 框架和 Hibernate ORM 工具的组合被广泛用于构建复杂的Web应用程序,尤其是电子商务平台。这种架构模式...

    JSF+Spring+Hibernate的实例讲解.doc

    JavaServer Faces (JSF)、Spring Framework 和 Hibernate 是构建现代 Web 应用程序的三个关键技术。JSF 是一种用户界面框架,适用于基于 Model-View-Controller (MVC) 架构的应用程序,提供了丰富的组件库和事件驱动...

    jsf+spring+hibernate实例

    **JSF、Spring和Hibernate是Java开发中的三大核心技术,它们分别负责用户界面、业务层管理和数据持久化。本文将深入探讨这三者如何在实际项目中整合,以及它们各自的关键功能和相互作用。** **JavaServer Faces ...

    ajax+jsf+spring+hibernate

    **Ajax、JSF、Spring和Hibernate是四种在Java Web开发中广泛应用的技术,它们共同构建了高效、灵活且功能强大的Web应用程序。** **Ajax(Asynchronous JavaScript and XML)** 是一种在无需重新加载整个网页的情况...

    基于jsf+spring+hibernate+ajax的网络文件管理系统

    本系统是基于jsf+spring+hibernate+ajax实现的一个网络文件管理系统.运行环境 WEB服务:TOMCAT6 数据库:SQLSERVER2005 JDK1.4以上 本系统采用了基于角色的权限管理

    JSF+Spring+Hibernate 配置

    具体的配置看这的WORD文档 博文链接:https://luxiangdong.iteye.com/blog/215634

    JSF+Spring+Hibernate相关技术文档

    在IT领域,JavaScript Server Faces(JSF)、Spring框架和Hibernate持久化框架是构建企业级Web应用程序的常用技术栈。这个压缩包文件包含了关于如何利用这些技术进行工程搭建以及基础技术介绍的相关文档,对于初学者...

    JSF+Spring+hibernate整合网站例子

    **JSF、Spring和Hibernate整合详解** JSF(JavaServer Faces)是Java平台上的一个用于构建用户界面的组件模型框架,它提供了丰富的UI组件和事件处理机制。Spring框架则是一个全面的企业级应用开发框架,核心功能...

    jsf+spring+hibernate+ajax4jsf

    在IT行业中,构建高效、可扩展的Web应用程序是至关重要的,而"jsf+spring+hibernate+ajax4jsf"的组合提供了一个强大的框架集合来实现这一目标。这个整合涉及四个关键组件:JavaServer Faces (JSF),Spring框架,...

    jsf+spring2.5+jpa(hibernate)的jar包

    这是jsf+spring2.5+jpa(hibernate)的jar包,很多人为了jsj环境而配置半天,在此提供jar包共享。注:除了ajax4jsf和tomahawk-1.1.3.jar,因为csdn只让我上传20mb,大家自己可以下一下自己试试。

    JSF第一步--JSF+Spring+ Hibernate+AJAX编程实践 试读

    在"JSF第一步--JSF+Spring+Hibernate+AJAX编程实践 试读"这本书中,读者可以期待学习如何设置这些技术的集成环境,创建JSF组件,配置Spring容器,理解Hibernate的映射和查询机制,以及如何在JSF中使用AJAX进行异步...

Global site tag (gtag.js) - Google Analytics