Wednesday 16 May 2012

JSF + SPRING + JDBC TEMPLATE



CustomerBo.java
package com.jagan.customer.bo;

import java.util.List;

import com.jagan.CustomerBean;


public interface CustomerBo{

void addCustomer(String name);
 }

CustomerBoImpl .java
package com.jagan.customer.bo.impl;
 import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import javax.faces.context.FacesContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;
 import com.jagan.CustomerBean;
import com.jagan.customer.bo.CustomerBo;
import com.jagan.customer.dao.CustomerDao;

public class CustomerBoImpl implements CustomerBo{

private JdbcTemplate jdbcTemplate;
      public CustomerBoImpl(JdbcTemplate jt)
{
jdbcTemplate =jt;
}
public void addCustomer( final String name)
{
// TODO Auto-generated method stub
jdbcTemplate.update("insert into customer values(?)",new PreparedStatementSetter(){
public void setValues(PreparedStatement ps)throws SQLException
{
ps.setString(1, name);

}
});
}

}
CustomerBean .java


package com.jagan;
import java.io.Serializable;
import java.util.List;
import javax.faces.context.FacesContext;
import org.springframework.beans.BeanUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;
import com.jagan.customer.bo.CustomerBo;
import com.jagan.customer.bo.impl.CustomerBoImpl;
import com.jagan.customer.dao.CustomerDao;

public class CustomerBean implements Serializable{

//DI via Spring
CustomerBo  custmerService;
public CustomerBo getCustmerService() {
return custmerService;
}

public void setCustmerService(CustomerBo custmerService) {
this.custmerService = custmerService;
}

public String name;


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
public String  addCustomers()
{
/*
CustomerBo customerBO =  (CustomerBo) FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()).getBean("custmerService");
//copy customerform to model
BeanUtils.copyProperties(customer, customer);*/
String name  = getName();
System.out.println("Name:"+name);
if(name != null){
custmerService.addCustomer(name);
}




clearForm();

return "";
}

   //clear form values
  private void clearForm(){
setName("");

    }

}

default.xhtml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<head>
   
  <h1>JSF 2.0 + Spring + JDBC Example</h1>
  
   <h2>Add New Customer</h2>
</head>
 <body>
   <h:form>
 
 
    Name : 
    <h:inputText id="name" value="#{customer.name}" size="20" required="true" label="Name">
</h:inputText>
 
    <h:message for="name" style="color:red" />

   <h:commandButton value="Submit" action="#{customer.addCustomers}" />
 
   </h:form>
 </body>
 </html>


application-context.xml :


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName">
   <value>oracle.jdbc.driver.OracleDriver</value>
   </property>
   <property name="url">
   <value>jdbc:oracle:thin:@localhost:1521:orcl</value>
   </property>
   <property name="username">
   <value>scott</value>
   </property>
   <property name="password">
   <value>tiger</value>
   </property>
   </bean>
  <bean id="jdbctemp" class="org.springframework.jdbc.core.JdbcTemplate">
   <constructor-arg>
   <ref local="datasource"/>
  </constructor-arg>
   </bean>
    <bean id="custmerService" class="com.jagan.customer.bo.impl.CustomerBoImpl">
   <constructor-arg>
   <ref local="jdbctemp"/>
  </constructor-arg>
   </bean>

</beans>
faces-config.xml
..............


<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">

<application>
 <!--
   <variable-resolver>
      org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>

-->
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

   </application>
<managed-bean>
<managed-bean-name>customer</managed-bean-name>
<managed-bean-class>com.jagan.CustomerBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>custmerService</property-name>
<value>#{custmerService}</value>
</managed-property>
</managed-bean>

</faces-config>


we

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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_2_5.xsd"
  id="WebApp_ID"
  version="2.5">
  <display-name>JavaServerFaces</display-name>
  <context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
  </context-param>
  <!-- Add Support for Spring -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <!-- Change to "Production" when you are ready to deploy -->
    <!-- Welcome page -->
  <welcome-file-list>
    <welcome-file>default.xhtml</welcome-file>
  </welcome-file-list>
  <!-- JSF mapping -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- Map these files with JSF -->
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
  <!--Tomcat 5 Workaround: Listener used to initialize JSF on startup-->
  <!--Remove comment tags to enable listener.
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
-->
  <!--Tomcat 5 Workaround: Listener implementation to handle web application lifecycle event-->
  <!--Remove comment tags to enable listener.
<listener>
<listener-class>com.sun.faces.application.WebappLifecycleListener</listener-class>
</listener>
-->
</web-app>


No comments:

Post a Comment