Monday 23 December 2013

Spring jsf Hibernate Integration

 Spring jsf Hibernate Integration 

Hello Friends.i am going to demonstrate you Spring Hibernate jsf integration project.here in this example i am follwing DAO pattern to interact with database.
You need to add jars associated with spring ,hibernate and jsf .
As it does not include any thorough understanding the concept i am moving towards code ,the code is as given below :


 Java/Reources->org.integration.model
package org.integration.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
public class User
{
       
   
    @Id
    private int id;
    @Column(nullable=false)
    private String username;
    @Column(nullable=false)
    private String password;
   
   
    public int getId()
    {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
   
   
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
   
   
}
under Java Resources->org.integration.dao

package org.integration.dao;

import org.integration.model.User;

public interface UserDao
{
   
    public void addUser(User u);
    public void updateUser(int id);
    public void deleteUser(int id);
   

}
under Java Resources->org.integration.dao

package org.integration.dao;

import org.hibernate.SessionFactory;
import org.integration.model.User;

public class UserDaoImpl implements UserDao
{
   
    private SessionFactory sessionfactory;
   

    public SessionFactory getSessionfactory() {
        return sessionfactory;
    }

    public void setSessionfactory(SessionFactory sessionfactory) {
        this.sessionfactory = sessionfactory;
    }

    @Override
    public void addUser(User u)
    {
       
        getSessionfactory().getCurrentSession().save(u);
       
       
    }

    @Override
    public void updateUser(int id)
    {
        getSessionfactory().getCurrentSession().update(id);
    }

    @Override
    public void deleteUser(int id)
    {
        getSessionfactory().getCurrentSession().delete(id);
       
       
    }

}

under Java Resources->org.integration.service
package org.integration.service;

import org.integration.model.User;

public interface UserService
{
   
    public void addUser(User u);
    public void updateUser(int id);
    public void deleteUser(int id);

}

 under Java Resources->org.integration.service

package org.integration.service;

import org.integration.dao.UserDao;
import org.integration.model.User;

public class UserServiceImpl implements UserService
{
    private UserDao userDAO;

    public UserDao getUserDAO() {
        return userDAO;
    }

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

    @Override
    public void addUser(User u)
    {
       
       
    getUserDAO().addUser(u);
   
       
    }

    @Override
    public void updateUser(int id)
    {

    getUserDAO().updateUser(id);
   
       
    }

    @Override
    public void deleteUser(int id)
    {
       
    getUserDAO().deleteUser(id);
   
    }
 
   
}
 under Java Resources->org.integration.beanManager

package org.integration.beanManager;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import org.integration.model.User;
import org.integration.service.UserService;
import org.springframework.dao.DataAccessException;

@ManagedBean
public class UserHandler implements Serializable
{
/**
     *
     */
    private static final long serialVersionUID = 1L;

@ManagedProperty(value = "#{userservice}")
private UserService userservice;

private int id;
private String name;
private String password;

public UserHandler()
{
   
}

public UserService getUserservice() {
    return userservice;
}
public void setUserservice(UserService userservice) {
    this.userservice = userservice;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

public String add()
{
    try
    {
User u=new  User();

u.setId(getId());
u.setUsername(getName());
u.setPassword(getPassword());

getUserservice().addUser(u);
    }
    catch(DataAccessException d)
    {
      
    }
return "Success";

}
public String update(int id)
{
    getUserservice().updateUser(id);
   
    return "Update";
   
}
public String  delete(int id)
{

    getUserservice().deleteUser(id);

    return "Delete";
}



   
}


 index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
     xmlns:h="http://java.sun.com/jsf/html"
     xmlns:f="http://java.sun.com/jsf/core"
     xmlns:p="http://primefaces.org/ui">
    <h:head><title>Spring hibernate jsf integration</title></h:head>
 <body>
     <h:form>
         <table>
             <tr>
                <td><h:outputLabel for="id" value="id" /></td>
                <td><p:inputText id="id" value="#{UserHandler.id}"/></td>
              
             </tr>
             <tr>
                <td><h:outputLabel for="name" value="name:" /></td>
                <td><p:inputText id="name" value="#{UserHandler.name}"/> </td>
             </tr>
             <tr>
                <td><p:commandButton id="submit" value="Save" action="#{UserHandler.save}" ajax="false"/></td>
                <td><p:commandButton id="reset" value="Reset" action="#{UserHandler.reset}" ajax="false"/></td>
             </tr>
         </table>
     </h:form>
</body>
</html> 
    
 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringHibernateJSF</display-name>

 <!-- Spring Context Configuration' s Path definition -->
      <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>
            /WEB-INF/Spring-Context.xml
         </param-value>
      </context-param>
     
     
     
 <!-- The Bootstrap listener to start up and shut down Spring's root WebApplicationContext. It is registered to Servlet Container -->
      <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
      </listener>
      <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
      </listener>
     
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>*.xhtml</url-pattern>
  </servlet-mapping>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>

Spring-Context.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
      
      
      
      
        <bean id="User" class="org.integration.model.User">
        </bean>
      
        <bean id="UserDAO" class="org.integration.dao.UserDaoImpl">
        <property name="sessionfactory" ref="SessionFactory"></property>
        </bean>
      
        <bean id="UserService" class="org.integration.service.UserServiceImpl">
        <property name="userDAO" ref="UserDAO"/>
        </bean>
      
      
         <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/MyDb" />
        <property name="resourceRef" value="true" />
        <property name="expectedType" value="javax.sql.DataSource"/>
        </bean>
       
        <bean id="SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
       
       
        <property name="hibernateProperties">
       
            <list>
                <value>org.integration.model.User</value>
            </list>
       
          
        </property>
        <property name="mappingDirectoryLocations">
         <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
            </property>
       
        </bean>


</beans>

[Note: example only demonstrates to add user daa,it does not associated any ui component with action event such as update,  delete]

hope you will get my point

thanks
for any query ping me on pathak.nisarg@yahoo.com
















 

No comments:

Post a Comment

Spring Boot SSL configuration -Tomcat Server

Hi Friends hope you all are doing well. Today I am going to demonstrate about how to configure SSL in Spring boot web Application. Need o...