January 25, 2007

Multiple file upload using Apache Struts

Uploading of file using struts with the FormFile is easy! But the requirement that I got was not as sweet as I thought. I had to upload multiple files and the user had the facility to add files before doing the upload. Thus I had to have a “Add File” button that will present the user with one more column to select a file using the browse button.

Having the facility of FormFile and the apache upload package, I thought the task would be easy but I had tough time due the dynamic part of this task. In the end I solved the problem. And here is the solution for you people as you should not reinvent the wheel. :)

Add button problem was solved by a simple java script that created an INPUT tag and inserted to the existing form. The code is given below:

function addFileElement() {
fCount++;
var fObject = document.getElementById('fileSection');
var text = 'File:';
var tag='<input type="file" name="testFile['+fCount+']" value="">';
var brk='<br>';
var o1 = document.createTextNode(text);
var o2 = document.createElement(tag);
var o3 = document.createElement(brk);

fObject.appendChild(o3);
fObject.appendChild(o1);
fObject.appendChild(o2);
fObject.appendChild(o3);

}

As usual the form bean of struts had a FormFile and an ArrayList variable that would hold the entire set of files. Now, I the form bean we tune the setter method of FormFile so that, each time the function is called the new file that is being set is added to the ArrayList. The code is given below:

public void setTestFile(int in,FormFile t) {

try {

this.testFile = t; // testFile is FormFile’s obj
setFormFiles(t); // add the file to array
index++;
}catch(Exception e) {
System.out.println("Exception in setTestFile!" + e);
}

}

And now the jsp code! It’s fairly simple and sweet. All you have to do is provide a strut form that has "browse" buttons, a "submit" button and a “add” button. The add button will run the above java script and you will get more options to upload file!
The complete source code can be downloaded from here. Enjoy Programming! ;)

January 16, 2007

Working with Ajax

For some time my blog have been dry! The main reason for this is I am simply busy with my project. Experimenting with new things does take more time and I am sure you all will agree with this fact. I have already worked with Ajax during my 5th semester project, but was never deep into it. Time has come, now I work deeper on it and I have things yet to learn. As a student there are times you google stuffs and do not find the best resource. The “best” at times may mean simple, comprehensive!
Ajax is a combination of technologies and this is the reason for it to be complex, hard to debug and messy at times! But its outcome is fruitful! But to start with you need some good resource and this blog will put you into that direction! Check out the links given and continue discovering Ajax:

Java Passion – A free professional Ajax tutorial
Mozilla Developer Network
Ajax Matters – A collection of blogs, tutorials and more