Dynamic binding in spring mvc

December 30, 2009

In spring mvc , the view is backed by a command ( form backing ) object. Everything is fine when there is a one to one mapping between the command object and the view fields. But when there is a functionality like adding rows via js in an html table where the command object can grow dynamically, you need to do a little tweak to make sure it works with spring.

Say you have a table which is without any data initially. Now there is a button called addRows with which you can keep on adding data to the table. Say each row in the table corresponds to an object called Row. So when the user keeps on adding some rows to the table and submits the page you get a list of rows in the controller.

With a conventional declaration of a List , the problem with springs is that it is unable to do a dynamic binding of the newly added row objects to the form backing object.

We need something that can grow lazily, there is an implementation of this in apache commons library called LazyList which lets you do just that.

So you have to initialize something like this in the model object ..

@SuppressWarnings ( “unchecked” )

private List myDynamicRows = LazyList. decorate ( new ArrayList(),

new Factory() {

public Object create() {

return new Row();

}

});

I am currently working on a prototype of the same and will soon upload the codebase in my blog .

Clipboard object in js

December 30, 2009

There is a very nice but badly documented object in js which can be used to copy clipboard contents to and from your webpages.
The js object is called clipboardData.
I tried this in ie 6 and mozilla 3.5 browsers. It works fine with both of them. Here is a code snippet of how to use it in a js code and get the clipboard data in a local js variable ->

var data= clipboardData.getData( ‘text’ );

This will get the data from your clipboard…
Of course this has it’s own perils, one that it exposes your clipboard to the client js. So an malicious js can access you clipboard and even modify it.
Lastly when you try to use this , in case your browser security is set at medium it will give you a warning saying that the webpage is trying to access your clipboard do you want to allow it or not.

Javascript’s and Objects

September 13, 2009

Whatever I am going to write is here is heavily borrowed from these 2 sources ->

The book ( have a look at the review for it ) -> http://www.i-marco.nl/weblog/archive/2008/08/22/objectoriented_javascript_by_s

And the blog -> link_

I feel java scripts are the most ignored and hated of all the things in a web application. When we write an application a lot of thought goes on the design of the application ( barring js ) and the database. The js developement remains fairly adhoc. And the result is we often turn our application into a monolith with a lot of java script codes , which slows down our page loads, and redundant calls which turns into a support person’s nightmare.

I have been reading about java scripts for the past few days.  And found a couple of new things about it ( well new to me ). It looks quite cool …

I would like to touch upon some basic OO features that js provides , which looks pretty cool and can be used in web pages.  So the very first thing , in java script is that it is object driven rather than class driven. It’s a bit hard to understand but what my understanding is , js does’nt know anything about classes. All it knows is about objects. It’s a bit weird specially for guys like me coming from the java background.

So how does an object looks like ->

To start with you need to create a function like this ( ideally these are known as constructor functions .. you can create objects in other ways too but this I guess is the simplest , widely used , reusable one .. )

function Animal(name) {

this.name=name;

this.getName = function() { return this.name; }

}

Now to create an animal object and use it’s method , use this. Voila it’s simple is’nt it ->

>>>var dog = new Animal(‘Dog’);

>>>dog.getName();

“Dog”

I am not sure how you test out the  js but you can use firebug console in firefox it’s really cool.

Ok so now we have the dog object. Interesting to know that the arrays that we use in js often are actually Array objects that js creates for us. So when we say ->

var arr = ['1','2','3','4'];

It actually creates an array object called arr. So we can use the array object methods on it like a.push(’9′) to add an element to the array or a.pop() to get an element out of it.

So far so good, now what if I try to implement inheritance with java script. Is that possible ?

Every object has a secret prototype property. Prototype is a property that gets called immediately as the object is created. You can add methods and variables to this prototype object.

This prototype property is also consulted when you try to access a property that does not exist in the current object.

Ok so let’s look at the following example ( borrowed from the book OO Java script by Stefanov ) ->

//creating an anonymous object and assigning it to monkey variable

var monkey = {

feeds: ‘bananas’,

breathes:’air’

};

function Human() {}

Human.prototype = monkey;

Ok so we create a developer object now and give it some properties ->

var developer = new Human();

developer.feeds= ‘pizza’;

Now when we try looking up feeds , it is a property of the developer object so,

>>>developer.feeds

“pizza”

However when we say developer.feeds since it is not present in the developer object as such it looks up the prototype link to get the appropriate property in the prototype chain. The result ->

>>>developer.feeds

“pizza”

Using this you can augment the built in functions. The following is an example  ( it is always better to check if the function is present or not , this ensures if the function is written in later js releases we are not overwriting it )->

if(! String.prototype.reverse) {

String.prototype.reverse= function() { return //the actual logic };

}

I plan to add the function contains to an array that will return a boolean true if the object is present in the array.Well all these was just the startup, of course for further reading you can consult any of the sources mentioned above ( or go a google search on it :-) )…

Clustered and non clustered indexes

September 12, 2009

In the project that I am working in currently we are having some serious performance issues.. There is a page that does about 1000 inserts under a single transaction. On looking into that we found that there is one insert that is taking about 94 ms to insert one record. Ok so for 1000 records it counts down to about 9 secs which is huge.

We have an ms sql server. So on investigation we found out that there is a primary key that is defined in the server. Now whenever you have a primary key defined on an sql server it creates a clustered index for you, well provided there is no other clustered index defined for the table. In our case we have none and I suspect it is this clustered index that is the culprit. If I remove the primary key constraint on the table and instead create a unique non clustered index in the table it will be much faster. I have seen people doing that in one of the projects that I worked on earlier.

Ok so  the next question is what is a clustered index and what is a non clustered one ?

A clustered index is one where you have the actual data on the table physically sorted. So if there is an clustered index defined on any column ( or a combination of columns ) in the table and you try to insert a data to the table. The data might need to be physically moved in the disk to make sure that the actual data in the table is sorted. Now this makes the inserts on the tables having clustered index costlier.

For a non clustered index a separate index is created ( which is like the physical data being stored some where else ). So in case of any inserts it is this index table that is updated. So, performance going by the inserts on the table the non clustered ones are faster than the clustered indexes.

However obviously when it comes to select specially something like range searches ( which you might want for your search screen ) clustered indexes are far better.

Hey ….

September 12, 2009

Me @ Canadian Open 09

Ok so finally I have my own place in the net apart from my orkut account :) Well to start with I am a software engineer by profession and I love it :-) Apart from that my hobbies include listening to music and playing games on my psp… Finally, those wondering what is the image on my page header, so  it’s my favourite coffe joint. For those who don’t know it’s called Tim Horton’s , it’s very popular in Canada and yes they make awsome iced cappachinos..


Follow

Get every new post delivered to your Inbox.