Tuesday, March 15, 2011

Jquery Autocomplete with DataSource

Hi All,

Recently i needed an autocomplete textbox in which i need to store an ID and display a list. So, i gone for Jquery autocomplete which i was very used to. I have used this before but it was just for single list where i was not storing any ID. I tried my hands on with JSON using Jquery autocomplete but it was not giving me results the way i wanted.

So, finally, i found a solution as mentioned below ::

1) A textbox for autocomplete


<input type="text" id="input" name="input"/>


2) An onload script


function loadData(){
$("#input").autocomplete('<%=request.getContextPath()%>/showMyCity.do' , {
        extraParams: { locationName : function() { 
        return document.getElementById('input').value; 
            }
         },
        minChars: 0,
        width: 185,
        matchContains: "word",
        autoFill: false,
        max:50,
        formatItem:function(row) {
            return row[0];
        },
        formatResult: function(row) {
            return row[0];
        }
    });
    
    
    $("#input").result(function(event, data, formatted) {
        if (data)
            document.getElementById("selectedLocation").value=data[1];
            
            
    });

}



3) Jsp page which results your search :


<c:forEach items="${LOCATION_LIST}" var="bean">

<c:out value="${bean.description}"/>|<c:out value="${bean.code}"/>
</c:forEach>




With a pipe character added in between, you can hide/show text on autocomplete.

This resoved my issue and i need not  have to use JSON for that.

Cheers,

Ujjwal Soni

UBS



Monday, March 14, 2011

URL rewriting for multi country website like /countryname/cityname


Hello Everyone,


Recently, i had to develop a website which needed a different URL mapping, it needed a url mapping like /France/Paris (ie: country/city).I looked out for many options, first of all i tried it with url rewriting servlet and implemented a filter with it, but it was not exactly the thing i wanted. So i looked out for other options and finally i found UrlRewriteFilter from tuckey.


I downloaded the jars from www.tuckey.org/urlrewrite and configured that in web.xml file.


I found its configuration too easy, you need to configure that in web.xml under filter tag.
<filter>
     <filter-name>UrlRewriteFilter</filter-name>
     <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
  </filter>
 
  <filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


Secondly, there is a seperate xml file called urlrewrite.xml that is needed by this filter.

You need to configure url rewriting rules in there. You can also use expression language to define a rule for your URL type.


So, i did someting like this to achieve my goal.

 <rule>
     <from>^/([a-z\s-] )$</from>
         <to type="redirect">%{context-path}/redirectAction.do?country=$1</to>
    </rule>
   
    <rule>
     <from>^/([a-z\s-] )/$</from>
         <to type="redirect">%{context-path}/redirectAction.do?country=$1</to>
    </rule>
   
    <rule>
     <from>^/([a-z/\s/-] )/([a-z/\s/-] )$</from>
         <to type="redirect">%{context-path}/redirectAction.do?country=$1&amp;city=$2</to>
    </rule>
      
    <rule>
     <from>^/([a-z/\s/-] )/([a-z/\s/-] )/$</from>
         <to type="redirect">%{context-path}/redirectAction.do?country=$1&amp;city=$2</to>
    </rule>


The above configuration helped me to accept multi spaced url's like www.ujjwalbsoni.blogspot.com/United States Of America/New York



I created an action in struts that gets me the names of countries and cities accordingly and i finally got what i wanted.


Expression for redirect rule was really tedious since i was having multiple spaces in country/city names. But finally, i created and succeeded in making one and using that.




Cheers!!!


Ujjwal Soni


UBS


Thursday, March 3, 2011

A great new java framework playframework

Hi,




Today, my friend told me about a new emerging java framework, its known as Play Framework.

Its new and really awesome...Its not a game framework, but its like an MVC framework, a bit simpler one.

Check out this link http://www.playframework.org/documentation/1.1.1/5things

Cheers!!!

Ujjwal Soni












Generating xlsx excel 2007 files using jdk 1.4

Hi,

i need to generate excel 2007 reports in java, well, i had many options to do this like Jexcel, apache poi, Aspose...but this list got smaller since i had to do this in jdk 1.4,  since i had to do this in older jdk, i chose apache poi, but apache poi supports generating xlsx only on jdk 1.6, some blogs says that if we backport then poi can work on older jdk's, so i tried backporting it and converted jars in jdk 1.4, all was done and it worked for generating xls files, but when i changed it for xlsx using XHSSF class, it started throwing errors, i did lot of research but at the end, all was in vain. later on i read somewhere that apache poi backporting leads to serious memory issues on jdk 1.4, so i had gone for Jexcel, well jexcel needs a license and i gotaa use open source so i winded up this idea.

Finally, i had chosen Apache POI with Jdk 1.6 and its working great.

Cheers!!

Ujjwal

Tuesday, January 25, 2011

Backporting Struts 2 to JDK1.4 or JDK 1.3

Struts 2 framework and its dependencies available currently are compiled using JDK 1.5 and if you want to use same framework on JDK1.4 then you will require to backport these jars. Struts 2 framework core jars and its plugin jars can be translated to JDK 1.4 or 1.3 using Retrotranslator utility.

Retrotranslator is a open source project which does this translation.

You can read more about this on http://retrotranslator.sourceforge.net/

Here are the steps to create the backport jars for JDK 1.4 or JDK 1.3

1. Download Struts 2 - Alternative Java 4 JARs: from http://struts.apache.org.
This also contains the retrotanslator jar files

2. Unzip the downloaded file, this should create backport folder containing core jars, and retrotranslation jar files.

3. Now copy all your Struts 2 Jars (including dependancies) to a folder say (Struts2-1.5)

4. Create another folder for converted jar files say (Struts2-1.4)

5. Run below command for each jar in the Struts2-1.5 folder

java -jar retrotranslator-transformer-.jar -advanced -srcjar Struts2-1.5/struts2--.jar -destjar ./Struts2-1.4/struts2--.jar



Note: Replace the & tokens with appropriate jar values.
Here are commands for struts 2.0.11.2 backporting

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-core-2.0.11.2.jar -destjar Struts2-1.4/struts2-core-j4-2.0.11.2.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/xwork-2.0.5.jar -destjar Struts2-1.4/xwork-j4-2.0.5.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-codebehind.jar -destjar ./Struts2-1.4/struts2-codebehind.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-core.jar -destjar ./Struts2-1.4/struts2-core-1.4.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-spring.jar -destjar ./Struts2-1.4/struts2-spring.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-pell.jar -destjar ./Struts2-1.4/struts2-pell.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-plexus.jar -destjar ./Struts2-1.4/struts2-plexus.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-struts1.jar -destjar ./Struts2-1.4/struts2-struts1.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-jsf.jar -destjar ./Struts2-1.4/struts2-jsf.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-sitegraph.jar -destjar ./Struts2-1.4/struts2-sitegraph.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-sitemesh.jar -destjar ./Struts2-1.4/struts2-sitemesh.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-tiles.jar -destjar ./Struts2-1.4/struts2-tiles.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-config-browser.jar -destjar ./Struts2-1.4/struts2-config-browser.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/xwork-2.0.5.jar -destjar ./Struts2-1.4/xwork-2.0.5.jar


6. This should create all your jars which are required for JDK 1.4.

7. Make sure you delete all JDK 1.5 compiled jars from your application lib and replace it with translated jars for JDK 1.4. Keeping both may create unknown issues.


Retrotranslator is not limited to backporting the Struts 2 framework. Its a utility which can be utilized for backporting any JDK 1.5 compiled code to JDK 1.4.

Cheers,

Ujjwal Soni

Monday, January 24, 2011

java.sql.SQLException: Data size bigger than max size for this type

Hi All,

Last week i was facing an issue on inserting Blob data in Oracle 10g database. I tested my application on Jboss container and locally my application was running fine, the real problem i faced when i deployed it on Oc4J container.

I got this error ::: java.sql.SQLException: Data size bigger than max size for this type

I tried everything, even i upgraded my jdbc driver but the problem was still there.


Here is my problem code ::



CallableStatement cstmt =
dbConnection.prepareCall("{

call product_image(?,?,?)}");


cstmt.setInt(1, id);
cstmt.setBinaryStream(2,file.getInputStream(), (int) file.getFileSize());
cstmt.setString(3, companyCode);

cstmt.registerOutParameter(1, oracle.jdbc.driver.OracleTypes.INTEGER);

cstmt.execute();
int returnValue=cstmt.getInt(1);

I am using Jdk 1.4 and even my OC4J server is having jdk 1.4

 I temporarily resolved the issue using simple update statements using prepared statement and inserted blob in  my DB, but i wanted that via a procedure.

 Later, i tried below code and it worked fine ::


String sql = "{call TEST_INSERT_USER_REG(?)}";
cstmt = conn.prepareCall(sql);
cstmt.setBytes(1, Your BLOB data in Bytes);
cstmt.close();
 


Cheers,

Ujjwal Soni








Clearing form data using jquery

A question I often hear is, “How do I clear a form?”
Initially the answer seems very straightforward – a one-liner in jQuery:

$('form :input').val("");
 
But upon closer examination we find that this is a bad way to solve the problem. When someone says they want to “clear a form” what they really mean is that they want to clear the visible state from all the form fields. With this in mind, the code above is clearly not the right way to get the job done. First, it will blast away the values of hidden inputs, checkboxes and radio buttons. Not good. The values of those fields should not be altered. And second, it does not properly account for select elements. What we need is something smarter. Here’s a start:
 
  1. $.fn.clearForm = function() {
  2.   return this.each(function() {
  3.     var type = this.type, tag = this.tagName.toLowerCase();
  4.     if (tag == 'form')
  5.       return $(':input',this).clearForm();
  6.     if (type == 'text' || type == 'password' || tag == 'textarea')
  7.       this.value = '';
  8.     else if (type == 'checkbox' || type == 'radio')
  9.       this.checked = false;
  10.     else if (tag == 'select')
  11.       this.selectedIndex = -1;
  12.   });
  13. };
 
Voila! Now we have a plugin for clearing form fields that can be called like this:
 
$(':input').clearForm()
 
or
 
$('form').clearForm()
 
Cheers,
 
Ujjwal Soni