Tuesday, February 28, 2012

When you need to host a specific file on your spring application...

... and you don't want to disturb server configuration, as it would mean you need to make this setting on every new server you deploy your application.

Simply add to web.xml something like this:
<mime-mapping><extension>jnlp</extension>
  <mime-type>application/x-java-jnlp-file</mime-type>
</mime-mapping>
<mime-mapping><extension>jar</extension>
  <mime-type>application/java-archive</mime-type>
</mime-mapping>


Hope this helps anyone else. I was always rechecking Spring Security filters and always forgetting about the supporter MIME types...

Tuesday, February 21, 2012

Using AJAX call to send custom post data from the form

This was my solution and probably there are the better ones:
jQuery.ajax({
url : //URL
type: 'post',
data: {'data' : JSON.stringify(data)},
dataType: 'html',
cache: false,
async : false,
error: function (jqXHR, textStatus, errorThrown) {
document.open();
document.write(res);
document.close();
},
success: function (result) {
document.open();
document.write(result);
document.close();
}
});


It is interesting to note that replacing whole html page via
jQuery('html').html(result);

does not work in IE9.

Summary: if you wanna replace the whole page with newly html-formatted result, you have to use document.open();document.write(NewHTMLDocAsString);document.close(); instead of jQuery('html').html(result);

Monday, February 20, 2012

Javascript Prototype, sort of a object inheritance

Very cool thing to know about javascript:
http://msdn.microsoft.com/en-us/scriptjunkie/ff852808

Javascript comparison operators

I came across very nice short explanation of the differences between == and === comparison operators. I have copied this into my blog just in case the post gets deleted from stackoverflow for whatever reason.

The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

Reference: Javascript Tutorial: Comparison Operators

The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. It's this case where === will be faster, and may return a different result than ==. In all other cases performance will be the same.

To quote Douglas Crockford's excellent JavaScript: The Good Parts,


JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:


'' == '0'           // false
0 == ''             // true
0 == '0'            // true
false == 'false'    // false
false == '0'        // true
false == undefined  // false
false == null       // false
null == undefined   // true
' \t\r\n ' == 0     // true


The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !==. All of the comparisons just shown produce false with the === operator.


A good point was brought up by @Casebash in the comments and in @Phillipe Laybaert's answer concerning reference types. For reference types == and === act consistently with one another (except in a special case).

var a = [1,2,3];
var b = [1,2,3];
var c = { x: 1, y: 2 };
var d = { x: 1, y: 2 };
var e = "text";
var f = "te" + "xt";

a == b            // false
a === b           // false

c == d            // false
c === d           // false

e == f            // true
e === f           // true

The special case is when you compare a string literal with a string object created with the String constructor.

"abc" == new String("abc")    // true
"abc" === new String("abc")   // false

Here the == operator is checking the values of the two objects and returning true, but the === is seeing that they're not the same type and returning false. Which one is correct? That really depends on what you're trying to compare. My advice is to bypass the question entirely and just don't use the String constructor to create string objects.

Source

Wednesday, February 15, 2012

When IE doesnt parse the received XML AJAX response...

Take care that configured dataType of your ajax call is set to 'xml' instead fo 'html' or 'json', since otherwise IE will interpret it as XML.