Wednesday, October 29, 2008

Special Characters in Spring MVC

You might be surprised to see when you type special characters on the form and in the controller, it comes in wrong e.g.
I typed a copy right sign, "©".  It came in as "©".
Even though I have already set my page's encoding to UTF-8, it didn't work.

The problem is Spring tried to encode using ISO-8859-1 still.  I think it is because, per Servlet standard, that is the default request/response encoding.

The solution is simple; we just have to explicitly set the encoding we want using CharacterEncodingFilter.  Thank to a post in Spring Forum.

Add the following to your web.xml.
<filter>
<filter-name>springCharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>springCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Wednesday, October 8, 2008

Daily Puppy

I love Daily Puppy

It's my fix of the day!

Talking about dogs, there is a dog parade hosted by Justin Rudd of HautedDogs.org.

Sun, Oct 26, 2:30 p.m. 
The start/finish is at Livingston Park, 4900 E. Livingston Dr. (@ Park Ave. in Belmont Shore), Long Beach, 90803.

Thursday, October 2, 2008

jQuery.ScrollTo and Dynamic Content

I have a page which displays dynamic content pulled from the database.  I need to scroll the page to a specific element upon loading, so I look around and found that jQuery.ScrollTo is very helpful. I could write a code as simple as

$.scrollTo($('#div1'), 500)

where 
$('#div1') is the element I want my page to scroll to and 
500 is a length (in millisec) of animation showing a page scrolls from the top to that element.

Nice, right?

I stuck the code in $(document).ready() function. Rendered it in IE8. Looked Great. 
Tried in Firefox3. Ding! It doesn't work. I would expect the result the other way around... ;P

So, I tried another option - passing absolute position instead jquery object to scrollTo method.

$(document).ready(function() {
var pos = $('#div').position();
$.scrollTo(pos, 500);
});

Still, it doesn't work on FF! Worse, it threw exceptions:
[Exception... "Could not convert JavaScript argument arg 0 
[nsIDOMViewCSS.getComputedStyle]" nsresult: "0x80570009 
(NS_ERROR_XPC_BAD_CONVERT_JS)" 
location: "JS frame :: http://localhost:8888/scripts/jquery.js :: 
anonymous :: line 871" data: no]

WTH...

I googled around. These 2 links saved me.
Ok. I'm not crazy. This problem is a known problem. Position() returns a value properly only if the 
element is visible.

Ahhhh, I should try doing this AFTER my element is loaded on the page.

$(window).load(function() { 
$.scrollTo($('#div1'),500); });

This works!

Alright, let's try one more thing. I put the following snipplet in $(document).ready() function.

$('#div1').load(function() { 
$.scrollTo($('#div1'),500); });

It doesn't work.  Oh well,   the documentation explains it.
"When bound to the window element, the event fires when the user agent finishes loading all content within a document, including window, frames, objects and images. For elements, it fires when the target element and all of its content has finished loading. Note: load will work only if you set it before the element has completely loaded, if you set it after that nothing will happen. This doesn't happen in $(document).ready(), which jQuery handles it to work as expected, also when setting it after the DOM has loaded."

I will just stick to what works :)