Archive for March, 2009

JQuery: Get Input Value From Form Field

Saturday, March 28th, 2009
<input type="text" name="companyId" value="123" />

Get the value of the companyId with:

$("input[name='companyId']").attr("value"); //will return '123'

Java: Servlet Set Expires Header with Cache Control

Saturday, March 28th, 2009

When setting the expires date in Servlet, better to set the cache-control as well.

final int CACHE_DURATION_IN_SECOND = 60 * 60 * 24 * 2; // 2 days
final long   CACHE_DURATION_IN_MS = CACHE_DURATION_IN_SECOND  * 1000;
long now = System.currentTimeMillis();
//res being the HttpServletResponse of the request
res.addHeader("Cache-Control", "max-age=" + CACHE_DURATION_IN_SECOND);
res.addHeader("Cache-Control", "must-revalidate");//optional
res.setDateHeader("Last-Modified", now);
res.setDateHeader("Expires", now + CACHE_DURATION_IN_MS);

For no cache

res.setHeader("Pragma", "No-cache");
res.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
res.setDateHeader("Expires", 1);

Hibernate (annotation): Error: TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing

Friday, March 27th, 2009

If you get an error like this one

Hibernate: Error: TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing..

It is probably because you did not annotate your ManyToOne with the Hibernate SAVE_UPATE Cascade annotation:

@ManyToOne()
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
public Category getCategory() {
        return category;
}

Note, putting only

@ManyToOne(cascade = {CascadeType.MERGE,CascadeType.PERSIST})
public Category getCategory() {

Will not resolve the issue.

HTML: AJAX Modal Dialog Box

Wednesday, March 25th, 2009

Here are some JQuery puglins for creating Modal Dialog Box:

  • Facebox: Facebook L&F, does have some images for corners.
  • SimpleModal: Seems simple and lightweight.
  • ThickBox: Seems complete but quite heavy (also, not very well maintained, last change log was from 2007).

Spring: Error Cannot convert value of type [$Proxy...] to required type

Sunday, March 22nd, 2009

In Spring, if you get an error like:

Error Cannot convert value of type [$Proxy...] to required type

It might be because you are using Spring AOP (e.g., @transactional annotations) with “interface-proxy” mode (the Default) rather than “class-proxy” mode. To setup “class-proxy” do the following:

  1. Copy aspectjweaver.jar and aspectjrt.jar to your Web application classpath (you can find these files in the Spring Distribution/lib/aspectj
  2. Add the following in the your app context xml:
    <aop:config proxy-target-class=”true”/>

See also:

WordPress: PHP is user logged in > is_user_logged_in()

Saturday, March 21st, 2009

<?php
if ( is_user_logged_in() ) {echo ‘WordPress says you’re logged in!';}
else if ( !is_user_logged_in() ) {echo ‘WordPress says you’re not logged in!';}
?>

Particularly useful to avoid adding Google Analytic code when the user is logged-in. For example, if you are editing you blog from different access point, the Google Analytic filters can become cumbersome to manage.

Java: Hibernate 3.4 and SLF4J Impl Class not found and Singleton error

Friday, March 20th, 2009

Hibernate 3.4 does not have a SLF4J implementation, and if you download the latest implementation from http://www.slf4j.org/download.html you will get an imcompatible impl version (1.5.6 impl with the 1.5.2 api from Hibernate).

Just do the following:

  1. Remove all slf4j*.jar from your lib.
  2. Add the the latest sfl4j-api (i.e. slf4j-api-1.5.2)
  3. Add the latest impl slf4j-log4j12-1.5.6-.jar

NOTE: If you use Hibernate, make sure to use the slf4j-log4j*.jar, otherwise, changing the log4j.properties will have no effect. (see Turn off logging from log4j and slf4j)

Photoshop: Should be smarter about “Save for Web” target folders.

Sunday, March 8th, 2009

One thing that really really ennoyed me in Photoshop, is that “Save for Web” dialog only remembers the last folder used. So, every time you change project (making the last folder irrelevant), you have to browse back to the appropriate target folder.

Adobe Photoshop should store the “Save for Web” target folders per .psd. If one .psd file does not already have one, then, use the latest folder. (Also, might be good to have the default folder being the .psd directory, rather than the directory root).

Just some wishes for Adobe Photoshop CS5.

OpenWeb: SVG vs Canvas

Monday, March 2nd, 2009

In short, my finding is that Canvas is good to draw a single [animated or not] image and SVG is better for interactive 2D components or applications (e.g. Interactive Charting, Google Docs Presentations, and Zoho Show). I might be wrong, feel free to comment.

Canvas:

Svg:

Canvas vs SVG