Monday, 9 June 2014

Jquery Basic form Validation


Jquery:
Jquery is a java script library provides much more ready made features. Main motto of jquery is write less, do more.

Simle form Validation:

step 1:

Create HTML form and include fallowing jquery libraries.
1. jquery-2.1.1.min.js(jquery main library)
2. jquery.validate.min.js(jquery validation plugin library)
3. customjq.js (This is js having common validation methods)

html form file:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JQUERY DEMO</title>
<script type="text/javascript" src="./js/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="./js/jquery.validate.min.js"></script>
<script type="text/javascript" src="./js/customjq.js"></script>
<link rel="stylesheet" type="text/css" href="./css/font.css">

</head>
<body>
 <form id="myform">
  <div align="left" class="large">
   <H1>JQUERY DEMO</H1>
  </div>
  <div align="center" class="small">
   <table width="40%" height="100%">
   <H2>Employee Details</H2>
    <tr>
     <td align="left">Email ID:
     <td><input type="text" name="field1" class="input1" /> <br /></td>
    </tr>
    <tr>
     <td align="left">User Name:</td>
     <td><input type="text" name="field2" class="input1" /></td>
    </tr>
    <tr>
     <td align="left" nowrap="nowrap">Mobile Number:</td>
     <td><input type="text" name="field3" class="input1" /></td>
    </tr>
    <tr>
     <td align="left"><input type="submit" name="Submit"
      value="Validate" />
     <td>
    </tr>
   </table>
  </div>
 </form>
</body>
</html>

customjq.js

$(document).ready(function() {
 $('#myform').validate({ // initialize the plugin
  rules : {
   field1 : {
    required : true,
    email : true
   },
   field2 : {
    required : true,
    minlength : 5
   },
   field3 : {
    required : true,
    number : true,
    minlength : 10
   }
  },
  messages : {
   "field1" : {
    required : "Please Enter Valid Email ID"
   },
   "field2" : {
    required : "Please Enter Valid User Name"
   },
   "field3" : {
    required : "Please Enter Valid Mobile Number",
    number : "Please enter numbers only"
   },
  },
  submitHandler : function(form) { // for demo
   alert('valid form submitted'); // for demo
   return false; // for demo
  }
 });
});


 out put:

There are three fields for validation email,username and mobile number. For email validation no need to write our own logic as we are doing in java script since this feature is already implemented in jquery library just we have to make use of this by declaring email:true. Similary for number validation we can use number:true. Using message section as shown above we can declare custom user defined validation messages.









 

Spring MVC + Hibernate DB Configuration

step1:

Create a datasource

Declare a bean and its class and inject all its properties like driver class name,url,username and password

ClassName:DriverManagerDataSource

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>

Steps 2:
ClassName:AnnotationSessionFactoryBean
Create a session factory based on AnnotationSessionFactoryBean java class.
Inject its all dependent properties such as datasource and hibernate properties file.

<bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="annotatedClasses">
   <list>
    <value>com.dineshonjava.model.Employee</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
   </props>
  </property>
 </bean>

Step 3:

Create hibernate transaction manager bean and inject dependent session factory to it.
 ClassName:HibernateTransactionManager 


<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


Need of HibernateTransactionManager :

Hibernate transaction manager:

See below code snippet for completing simple transaction. Here in order to execute simple code we are plumbing code like closing/opening connections, exception handling etc. This small unit of work usually called transaction can be handled by annoting the method with @Transactional that’s it hibernate will take care of transaction details.

public void doSomething()

{

Session sess = factory.openSession();

Transaction tx = null;

try

{   

         tx = sess.beginTransaction();// do some work... tx.commit();

}

  catch (RuntimeException e)

{

if (tx != null) tx.rollback();throw e; // or display error message

}

finally

{

sess.close();

}

}

After making use of transactional manager:

@Transactional

public void doSomething() {

    // do some work

}











 


Friday, 6 June 2014

JAXWS service producing/consuming


System Requirement:

JDK 1.7
aixs 1.1
JBOSS 4.2
eclipse kepler



Developing WEBSERVICE using JAXWS(Java API for XML Web Service) and JBOSS Server
Create Dynamic webproject:


 

 

 

Create a class GetEmployee: 

Service class is exposed as service class using annotation @webservice

Here method getEmployee() exposed as webmethod using annotation @WebMethod.
package com.gps.service;
import javax.jws.WebMethod;
import javax.jws.WebService; 
@WebService
public class GetEmployee {     

       @WebMethod
       public Employee [] getEmployee(){         

              Employee employees[]={};
              Employee employee1= new Employee();
              Employee employee2= new Employee();
              employee1.setName("prashant");
              employee1.setLastname("S");
              employee1.setEmpid(1063);
              employee1.setProfession("Developer");          

              employee2.setName("Ramesh");
              employee2.setLastname("M");
              employee2.setEmpid(1063);
              employee2.setProfession("Core Developer");
              employees[0]=employee1;
              employees[1]=employee2;
              return employees;

             

       }

 

}
Keep below entry in web.xml file 

     <servlet>

              <description>Employee Details service</description>

              <servlet-name>GetEmployee</servlet-name>

              <servlet-class>com.gps.service.GetEmployee</servlet-class>

              <load-on-startup>1</load-on-startup>

       </servlet>
       <servlet-mapping>

              <servlet-name>GetEmployee</servlet-name>

              <url-pattern>/GetEmployee</url-pattern>

       </servlet-mapping>
Run on JBOSS Server











Generate WSDL file from service class
Below cammand can be used to generate the WSDL file from service class. This approach is called as Bottom Up approach

D:\jdk1.7.0_51\bin>wsgen -verbose -keep -cp . com.gps.service.GetEmployee -wsdl
Generated WSDL files
Generate Client class from WSDL using axis tool
Consuming service using java client
Employee.java
GetEmployee.java
GetEmployeeBindingStub.java
GetEmployeeProxy.java
GetEmployeeService.java
GetEmployeeServiceLocator.java 

package com.gps.service;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
public class Client {

       public static void main(String[] args) throws ServiceException {
              // TODO Auto-generated method stub         
              GetEmployeeServiceLocator locator= new GetEmployeeServiceLocator(http://localhost:8080/SampleJaxws/GetEmployee);            

              try {
                     //ServerInfoBindingStub stub = new ServerInfoBindingStub(locator);
                     GetEmployeeBindingStub stub=(GetEmployeeBindingStub)locator.getPort(GetEmployeeBindingStub.class);
                     Employee employee []=stub.getEmployee();                 

                     for (Employee employee2 : employee) {

                           System.out.println(employee2.getName());

                           System.out.println(employee2.getLastname());

                           System.out.println(employee2.getProfession());

                           System.out.println(employee2.getEmpid());

                           System.out.println("***********************************");

                     }          
                 
              } catch (RemoteException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

       }

} 


Start JOBOSS on IP:

How to do please check below link.

http://stackoverflow.com/questions/16344333/jboss-mapping-ip-instead-of-localhost-works-from-within-eclipse-but-not-from

Run java client to check the response
Testing through SOAP UI