about:config trick for window.close() In Firefox
July 7th, 2010
No comments
| We have got list of post(s) about Hibernate. Today as a next step we’ll try to intergrate with MVC framework. We’ll take the most used one which Struts1.3. For example we’ll take the same Singer Example and we’ll work with that for intergrating with struts also.Since we have the domain object generated already, we can make use of the same domain object to populate the values from DB, when we use the Hibernate. |
| We just take the examples of listing all the Singers from Singers table. Since we are using Struts1.x, we may need to write the action class and form class. The following code shows Form Class. Since we are going to list all the Singers, then our Form looks like the following. |
package com.techmaddy.form;
/*
* Copyright 2010 - Kumarasamy
* kumarasamy@techmaddy.com
*/
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.techmaddy.singer.Singer;
public class SingerForm extends ActionForm {
private static final long serialVersionUID = 1L;
private Singer[] singer = new Singer[0];
public Singer[] getSinger() {
return singer;
}
public void setSinger(Singer[] singer) {
this.singer = singer;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
singer = new Singer[0];
}
}
|
| The DAO class will make a call to DB and returns the results. This is almost very similar to the class CRUDTest.java in Simple CRUD Operation With Hibernate. Instead of my CRUDTest class, we have an DAO Class which gets the data from DB. |
| My DAO Class looks like the following. |
package com.techmaddy.bo;
/*
* Copyright 2010 - Kumarasamy
* kumarasamy@techmaddy.com
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import com.techmaddy.singer.HibernateSessionFactory;
import com.techmaddy.singer.Singer;
public class SingerManager {
public Singer[] getAllSingers() {
List singer = new ArrayList();
Session session = null;
Transaction tx = null;
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
Query qry = session.createQuery("from Singer");
for (Iterator iterator = qry.iterate(); iterator.hasNext();) {
singer.add(iterator.next());
}
return (Singer[]) singer.toArray(new Singer[0]);
}
}
|
| The same way, based in the action performed from the screen, we can make a call to Editing and deleting the Singers.The following Images shows the result screen. |
Download To download the War file for Singer with Struts Integration Click Here |
| Here goes my file upload component in PHP. It just uploads the file into the specified directory.Other thing i have added as of now is it’ll upload the files into the folder on the current month and checks for the file exists already or not, if it already exists then it’ll prefix the time in milliseconds in the file name. |
<?php
/*
* Copyright 2010 - Kumarasamy
* kumarasamy@techmaddy.com
*/
class DocumentHandler {
var $uploadDir;// Directory where the documents to be uploaded
function prepateForUpload() {
$this->uploadDir = '/var/www/uploads';
$this->uploadDir = $this->uploadDir . "/" . date("Y_m");
//Check the month folder is available, if not then create it.
if(!is_dir($this->uploadDir))
mkdir($this->uploadDir);
} //End of setDetails
function copyFile($_FILES,$fileCntrlName) {
$resultArray = array();
$uploadfile = $this->uploadDir . "/" . basename($_FILES[$fileCntrlName]['name']);
//If the file exists already in the folder, append the millisecond in the file name.
if(is_file($uploadfile)) {
list($msec, $sec) = explode(".", microtime(true));
$uploadfile = $this->uploadDir . "/" . $msec . $sec . basename($_FILES[$fileCntrlName]['name']);
}
if (move_uploaded_file($_FILES[$fileCntrlName]['tmp_name'], $uploadfile)) {
$resultArray['error'] = '';
$resultArray['displayMessage'] = 'File Uploaded Successfully!!!';
}
return $resultArray;
} //End of copyFile
} //End of DocumentHandler
?>
|
The following things can be added,
|
| Idea’s are accepted!!! |