Monday, 3 February 2014

Create a Simple Spring MVC application along with Java Script or JQuery Datatables

The main reason for writing this tutorial is i found many examples online but all are either very basic or more complicated(i felt) so i wanted to share my knowledge in creating a sample spring MVC application that will display some data using JQuery Datatables.


Prerequisites :

        To do this tutorial we need to have any version eclipse that have an option to create dynamic web project and Apache Tomcat or Similar Server to run the application.
         

 Step by Step Procedure:
  1.  Lets start creating a dynamic web project by name Spring3MVC as shown in below image.
   


 2. Once we create the dynamic web project we have the skeleton for web app, we need to add the jars under lib folder of WEB-INF required for Spring that are mentioned in the Jars Required Section .

  3. You need to add the below entries in your web.xml to configure theDispatcher servlet of spring 

<display-name>Spring3MVC</display-name>
            <servlet>    <servlet-name>spring</servlet-name>    <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup>          </servlet>         <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.action</url-pattern>       </servlet-mapping>


 if you observer the above XML code servlet name is specified as spring  which means  the spring container will search for the spring-servlet.xml  for loading the beans and other spring specific configurations.

4.  Create a file with name spring-servlet.xml  under WEB-INF folder and copy the below XML content to that file.

<?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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

 <context:component-scan base-package="com.sample.spring3mvc.controller" />

 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/pages/" />
  <property name="suffix" value=".jsp" />
 </bean>

 <bean id="productFacade"
  class="com.sample.spring3mvc.facade.impl.ProductFacadeImpl">
 </bean>
 <bean id="productDao"
  class="com.sample.spring3mvc.dao.impl.ProductDaoImpl">
 </bean>
 <mvc:annotation-driven />
 <mvc:annotation-driven>
  <mvc:message-converters>
    <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
  </mvc:message-converters>
</mvc:annotation-driven>
</beans>

5. Explanation of Entries in spring-servlet.xml
     <context:component-scan base-package="com.sample.spring3mvc.controller" /> : This entry will tell spring container where  the controller classes are available, all the files/packages under the above package will scanned for controllers.
   viewreolver bean  used to configure the prefix path and suffix extension of the views returned, so that we dont need to return these values in controller.
  <mvc:annotation-driven> : This entry is required if we are going to use any annotations in the bean classes. For example @Autowired.
  <mvc:message-converters>
    <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
  </mvc:message-converters> 
         The above entry is required for automatic conversion of normal objects to JSON when @Responsebody is used in any of the controller method which we will see in the later parts of the tutorial.
  The rest of the entries are just beans that we gonna use in application.

With this we are done with the configuration part and now we jump into real coding stuff.

Java and JSP Coding

  Create a index.jsp and place it under WebContent/webroot folder and the content would be something like this.

<html>
<head>     <title>Spring 3.0 MVC Series: Index - </title> </head> <body>     <a href="home.action"> Home</a> </body> </html>
    The below jsp would be a simple one if you do not want the Datatable in it.
 Create home.jsp under Web-INF/pages with the below content
  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<link
href="//datatables.net/download/build/nightly/jquery.dataTables.css"
rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="//datatables.net/download/build/nightly/jquery.dataTables.js"></script> <meta charset=utf-8 /> <title>Spring MVC -DataTables Example</title> </head> <body> <div class="container"> <h2>Directly Loaded Data and added Datatable after load</h2> <table id="example" class="display" width="100%"> <thead> <tr> <th align="left">Name</th> <th align="left">Color</th> <th align="left">Price</th> </tr> </thead> <tfoot> <tr> <th align="left">Name</th> <th align="left">Color</th> <th align="left">Price</th> </tr> </tfoot> <tbody> <c:forEach items="${products}" var="product"> <tr> <td>${product.name}</td> <td>${product.color}</td> <td>${product.price}</td> </tr> </c:forEach> </tbody> </table> </div> <div class="container"> <h2>Loaded Data through ajax after page Load</h2> <table id="example1" class="display" width="100%"> <thead> <tr> <th align="left">Name</th> <th align="left">Color</th> <th align="left">Price</th> </tr> </thead> <tbody> <tr> <td></td> <td></td> <td></td> </tr> </tbody> </table> </div> </body> <script> $(document).ready(function() { var table1 = $('#example').DataTable(); var table2 = $('#example1').DataTable({ "ajax" : "${pageContext.request.contextPath}/getAllProds.action", }); }); </script>
</html>


If you observe in the above code we are directly referencing the javascript files it works only if u have internet connection. If you have downloaded those javascript files you can refer it locally as well.


Datatable usage in Spring Explanation

In the above jsp code i gave two examples for Datatable                  I. Applying Datatable to already loaded HTML table with id example1, the js code for this is as simple as $('#example').DataTable();                 II. In the second case we are directly loading data using an ajax call associated with Datatable which is  $('#example1').DataTable({ "ajax":"${pageContext.request.contextPath}/getAllProds.action"});                  but for doing the above step the data returned from the controller should be in a particular form of JSON as suggested in the site, which is done using getJSONForProd  method in the controller and returning aaData  from the controller.               For Live Examples on Datatables you can go here Live Datatables Example.
Controller Code    Create a Controller with name HomeController.java and use the below code.    ProductData is just a POJO .
   productFacade.getAllProducts() will return list of ProductData  which u can just mock it.
package com.sample.spring3mvc.controller;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sample.spring3mvc.data.ProductData;
import com.sample.spring3mvc.facade.ProductFacade;


@Controller
public class HomeController {

    @RequestMapping("/home")
    public ModelAndView helloWorld(final Model model) {
        model.addAttribute("products",productFacade.getAllProducts());
        return new ModelAndView("home", "message", model);
    }
    @Autowired
 private ProductFacade productFacade;
 
    @ResponseBody
    @RequestMapping("/getAllProds")
 public Map<String, Object[]> getAllProducts()
 {
     List<ProductData> aaData= productFacade.getAllProducts();
  return Collections.singletonMap("aaData", getJSONForProd(aaData));
 }
  
    /**
     * I only want certain product info..
     */
     public Object[] getJSONForProd(Collection<ProductData> prods){
         Object[] rdArray = new Object[prods.size()];
         int i = 0;
         for(ProductData u:prods){
             // [ name, id,arn,groups ]
             Object[] us = new String[]{u.getName(),u.getColor(),Double.toString(u.getPrice())};
             rdArray[i] = us;
             i++;           
         }
         return rdArray;
     }
}

Run the application on server
  Once all the above steps are completed successfully you run the application by right clicking on the application as shown below
Run on server
Once you run you can see the associated server will start and then you can key in the below link in any of the qualified browser ex:Chrome and test: http://localhost:8080/Spring3MVC/


In case of any doubts/clarifications/Suggestions you are welcome @vinaytaj2006@gmail.com



Tuesday, 13 March 2012

Polymorphism:Core of Object Oriented Programming

Java is an Object Oriented Language and Polymorphism is its core concept.Nobody can program efficiently without knowing the core programming techniques like Polymorphism,Code Reuse etc.The genuine meaning of polymorphism is "many forms".In Java this meaning is rightly justified.Polymorphism in Java means a method can have various versions.It may be in A super class and it can also be in sub class.There are ways to invoke the methods from either super class or the sub class.As you are familiar with Java you must know that a sub class in Java inherits all the methods of its super class(except methods are declared private in super class).Every sub class can provide its own functionality to every super class method.That is in simpler terms the method is overridden in sub class.We can invoke the super class method using both reference variable types either as of super class or of sub class.

Following Example make things clear for you.

class Shape{
public void imageShape(){
System.out.println("Image Shape Printed");
}
}

class Square extends Shape{

public void imageShape(){
super.imageShape();
System.out.println("Square Shape Printed");
}

public void shape_square(){
System.out.println("The Shape is square");
}
}

class TestShape{
public static void main(String a[]){
Square sqr=new Square();
Shape shp=new Shape();
sqr.imageShape();
}
}

Output:-
The output of above program is
Image Shape Printed
Square Shape Printed

Explanation:-
There are certain you must have noticed in the code.First of all starting from our super class Shape it has one method imageShape().The sub class Square inherits the method imageShape() from its parent Shape.Square class provides square specific functionality to this method.
Note:-super keyword can be used to refer to the super class version of imageShape() method.
never use super outside method body.If you do so compiler don't hesitate to slap you.You can also add your additional methods to your more specific classes as we did here.
TestShape class makes object of the Square class and invokes method imageShape().This reference cause the sub class version of imageShape() which results in the presented output.

The story is not as you like there are some problems for you I know you can solve them,if possible let me know the answers with proper explanations:-


1.What would be the output of following:-

class Shape{
public void imageShape(){
System.out.println("Image Shape Printed");
}
}

class Square extends Shape{

public void imageShape(){
System.out.println("Square Shape Printed");
}

public void shape_square(){
imageShape();
System.out.println("The Shape is square");
}
}

class TestShape{
public static void main(String a[]){
Square sqr=new Square();
sqr.imageShape();
sqr.shape_square();
}
}

--------------------------------------------------------------------

2.What would be the output of following code:-

class Shape{
public void imageShape(){
System.out.println("Image Shape Printed");
}
}

class Square extends Shape{

public void imageShape(){
super.imageShape();
System.out.println("Square Shape Printed");
}

imageShape();
public void shape_square(){
imageShape();
System.out.println("The Shape is square");
}
}

class TestShape{
public static void main(String a[]){
Square sqr=new Square();
sqr.imageShape();
}
}

Why Java does not support multiple Inheritance?

One can always think of extending multiple classes to inherit the multiple features in the form of methods.Like following


class Car extends Accelerate,Color{ //interesting code}

but the above declaration is not legal in terms if Java.A class cannot extend more than one class.This exactly implies that only one parent class per class.But a class can have multiple ancestors such that if class A extends class B and class B extends class C then class A has two ancestors class B and C .A class can have multiple ancestors up the inheritance tree but it is not possible by direct declaration of extending classes.

In C++ ,One can extend multiple classes simultaneously that is the declaration we have done for class Car is correct in case of C++.Capability of extending multiple classes is called as "Multiple Inheritance".Java creators thought a lot about allowing the multiple inheritance,but they were messed up after using the multiple inheritance.Like in this case ,if a class extended two classes and if both classes has method(s) in common then how the methods will be inherited and how we will come to know that which method is called of which class.That is why they excluded the multiple inheritance in Java.There is a famous problem which is faced during the multiple inheritance application called as "Deadly Diamond of Death".This is named so because the shape of class diagram which is formed after multiple inheritance implementation.The diamond is formed when classes B and C extend A ,and both B and C inherit methods from A.If class D extends both B and C,and both B and C have overridden the methods in class A,class D in theory has inherited the two different implementations of same method.
:-


There is an indirect way by which you can implement the multiple inheritance in Java and this is through extending a class and implementing an interface just like following:-

class Car extends Accelerate implements Movable{
// implement all Movable methods
}

In this way you can get methods from both class and interface and you can override them according to your use.Remember all methods of interface needs to be overridden in implementing class

Run Postman API remote or code or command line

POSTMAN is used to run the APIs generally. What if we want to create a collection in postman and run it from some other code or command l...