Thursday, January 5, 2012

Sons of DHTMLX and Spring Series

Episode 1: Home
After a long Christmas break, traveling, reading, playing with kids and eating my favorite  Roberto's burritos (carne asada with the works: pico di gallo, guacamole, cheese and sour cream), it is time to return to the blog and give my best wishes of a happy New Year to  all readers.  

In the early days of the DHTMLX Java Tag library some developers of a large financial company asked me to give code examples with the Spring Framework. I have also received requests from blog readers to provide examples of using DHTMLX and Spring, so I am taking the opportunity to write a series (see also episode 2 , episode 3 and episode 4) of posts on the subject. 
I have dug up my old examples, updated them to run with Spring 3.1, DHTMLX 3.0 and Google App Engine and packaged in a simple web application. Part of the DHTMLX Spring Adapter code presented comes from an open source library that will be included in the upcoming release (sometime in 2012) of the DHTMLX Java Tag Library.


DHTMLX Server Side
As extension of the JavaScript component library DHTMLX delivers a free open source Java package (dhtmlxConnector) for server side integration.  The connector provides  all the functionality needed to integrate the major database servers. Below is a Java code sample to connect a dhtmlxGrid to a database table: 
  1. /**
  2.  * DHTMLX Grid Connector Example
  3.  */
  4. public class BasicConnector extends ConnectorServlet {
  5.    @Override
  6.    protected void configure() {
  7.       Connection conn= ( new DataBaseConnection()).getConnection();
  8.      
  9.       GridConnector c = new GridConnector(conn);
  10.       c.dynamic_loading(100);
  11.       c.render_table("table", "id", "field1,field2");
  12.    }
  13. }
With the dhtmlxConnector you can have a working dhtmlxGrid in minutes if you have direct access to the database; however, the design of the connector does not allow you to get the same result in a environment where the persistence layer is based on JDO/JPA.  

Spring MVC
In order to get the advantage of the many features that the framework offers, Spring developers adopt a well defined architecture. A typical basic Spring Web Application consists of a  Model, Controller, Service and Repository.
Book.java
  1. /**
  2.  * Model
  3.  */
  4. public class Book {
  5.     private Long id;
  6.     private String author;
  7.     private String title;
  8.     private Integer sales;
  9.     private Integer price;
  10. }
Controller.java
  1. /**
  2.  * Controller
  3.  */
  4. @Controller
  5. public class BookController {
  6.     @Autowired
  7.     private BookService bookService;
  8.     @RequestMapping(value = "/books", method = RequestMethod.GET)
  9.     public void getBooks() {
  10.         List<Book> books = bookService.getBooks();
  11.         ...
  12.     }
  13. }
BookService.java
  1. /**
  2.  * Service
  3.  */
  4. public interface BookService  {
  5.     List<Book> getBooks();
  6.     Book getBook(String id);
  7.     Book store(Book book);
  8. }
BookDao.java
  1. /**
  2.  * Repository
  3.  */
  4. public interface BookDao {
  5.     Book find(String id);
  6.     List<Book> findByTitle(String title);
  7.     Book store(Book book);
  8.     List<Book> findAll();
  9. }

Request Mapping
Spring provides a powerful and flexible Java annotation for request mapping. There are many schools on the request mapping design. The table below highlight the URL mapping of the example application presented in the series:

URL
GET
PUT
POST
DELETE
/books
List books
NA
Create/Update book
NA
/books/id
Get book (id)
NA
NA
NA

DHTMLX Spring Adapter
The sample application of the series illustrates an approach how to integrate a Grid and a Form component with the Spring Framework. Below is the code example for the Controller class using the DHTMLX Spring Adapter.
BookController.java
  1. /**
  2.  * DHTMLX Spring Adapter Example
  3.  */
  4. @Controller
  5. public class BookController {
  6.   @Autowired
  7.   private BookService bookService;
  8.   @RequestMapping(value = "/books", method = RequestMethod.GET)
  9.   public GridAdapter getBooks(@RequestParam("c") String c) {
  10.     GridAdapter adapter = new GridAdapter(c,Book.class);
  11.     adapter.setData( bookService.getBooks() );
  12.     return adapter;
  13.   }
  14.   @RequestMapping(value = "/books/{id}", method = RequestMethod.GET)
  15.   public FormAdapter getBook(@PathVariable("id") final String id) {
  16.     Book book = bookService.getBook(id);
  17.     FormAdapter adapter = new FormAdapter( book );
  18.     return adapter;
  19.   }
  20.   @RequestMapping(value = "/book", method = RequestMethod.POST)
  21.   public FormAdapter storeBook(@Valid @ModelAttribute Book book, ...) {
  22.     FormAdapter adapter = new FormAdapter(book,binding);
  23.     if( adapter.hasValidData() ) {
  24.       bookService.store(book);
  25.     }
  26.     return adapter;
  27.   }
  28. }
Looks familiar, huh?
The series will cover many aspects of the integration of Spring and DHTMLX trying to get the best of both worlds, including client and server site validation, MVC architecture, and URL mapping best practice.

In the upcoming episodes I will go deep in the code, illustrating  the design of the DHTMLX Spring Adapter. Below is the list of  Sons of DHTMLX and SPRING  episodes: 

Episode 1: Home
Introduction to the DHTMLX Spring Adapater (this post)

Episode 2: Caregiver
Take care of your objects with client and server side validation  (released 01/11/12)

Episode 3: Turning and Turning
Spring MVC annotation driven configuration (released 01/18/12)

Episode 4: Fruit for the Crows
Full Source code delivery (released 02/16/12)


Episode 5: Call of Duty 
Get involved and contribute to the open source development (released 02/22/2012)

Stay Tuned!

39 comments:

  1. good stuff. looking forward to further postings from you.

    ReplyDelete
  2. Ah, there is a good discussion about this paragraph in this place on this website. I read all that, so now I am commenting here too... Qualified travelers to Turkey by filling an online form with their personal details can apply visa to Turkey and pay visa cost Turkey online.

    ReplyDelete
  3. instagram takipçi satın al
    instagram takipçi satın al
    takipçi satın al
    takipçi satın al
    instagram takipçi satın al
    takipçi satın al
    instagram takipçi satın al
    aşk kitapları
    tiktok takipçi satın al
    instagram beğeni satın al
    youtube abone satın al
    twitter takipçi satın al
    tiktok beğeni satın al
    tiktok izlenme satın al
    twitter takipçi satın al
    tiktok takipçi satın al
    youtube abone satın al
    tiktok beğeni satın al
    instagram beğeni satın al
    trend topic satın al
    trend topic satın al
    youtube abone satın al
    beğeni satın al
    tiktok izlenme satın al
    sms onay
    youtube izlenme satın al
    tiktok beğeni satın al
    sms onay
    sms onay
    perde modelleri
    instagram takipçi satın al
    takipçi satın al
    tiktok jeton hilesi
    pubg uc satın al

    ReplyDelete
  4. Do you think to make a video about this code fo youtube? You could get many likes and views from this site https://soclikes.com/

    ReplyDelete
  5. What an outstanding post! “I'll be back” (to read more of your content). Thanks…. Indian tourist visa or eTV for India is an online travel authorization. Foriegn travelers whose sole purpose of visiting India is for leisure, sightseeing, religious work or casual travel to visit their friends or relatives can apply for Indian tourist visa through online India visa application system.

    ReplyDelete
  6. The website is good and the stock is good. Thanks for all you do. Indian visa photo specifications, you can read all the guidelines related to Indian Visa Photo Specifications online through India Evisa Portal.

    ReplyDelete
  7. Hi, I think this is very useful information. The article is inspiring me to have the same mindset as you. Are you interested in visiting India? Travel to India requires an Indian visa. I don't no about what is medical visa in India? I found this site & read the Indian e visa page and I got knowledge about medical visa India & requirements. Then apply through this site. It's really great service! I received my visa online within days. Amazing service and super helpful. I recommend it to everyone!

    ReplyDelete
  8. Good afternoon everyone. Great blog. Thanks for sharing. You know how many categories of India e-visa, you can get more info about electronic Indian Visa & India visa price via our India visa page.

    ReplyDelete
  9. Hi, you should continue your writing. I am sure you have a great readers’ base already! Are you planning to travel to Turkey and don't have a Visa? You need not worry because now you are just a click away. You can apply for Turkey visa urgently and select urgent if you want it soon.You can get your visa within the processing time of Rush applications which can take up to 1 - 4 hours.

    ReplyDelete
  10. Hii guys,If you are interested to visit Pakistan then nowadays the Pakistan e visa process is online. The process is very simple if you have the right documents. First of all, check that you are a citizen of the eligible country and then apply for Pakistan visit visa. You can visit our e visa Pakistan website and get all the information here.

    ReplyDelete
  11. It s a very useful page. Thank you. fc616d60d3be8bd68d1f4c642fb3875e
    egil
    cumayeri
    gonen
    bergama
    cekerek
    bodrum
    kecioren
    yalihuyuk
    eskil

    ReplyDelete
  12. It s a very useful page. Thank you. d736584b7819b4b5336c3259a90bb6d8
    harmancik
    puturge
    uzundere
    uskudar
    uzumlu
    pinarbasi
    sogut
    alpu
    altinova

    ReplyDelete
  13. Congratulations on your article, it was very helpful and successful. b3a37798ff9da3dffe38b29678574dbb
    numara onay
    website kurma
    sms onay

    ReplyDelete
  14. Thank you for your explanation, very good content. d878d0e6bc2409c1abc9b84c6e7abe8a
    altın dedektörü

    ReplyDelete