Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

download Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

of 18

Transcript of Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    1/18

    Workshop on Creating a Personal MusicManager Web Application with Struts2,

    Hibernate and MySQL

    Overview:

    In this workshop tutorial well develop a personal music manager application using Struts 2, Hibernate and MySQLdatabase. The web application can be used to add your music collection to the database. Well display form to add

    the record and underneath well show all the music collections. From each row the record can be deleted by clickingDelete link. We have chosen Struts2 as this is one of the flexible J2EE framework. The database is MySQL and wehave used Hibernate as ORM tool.

    Tools Used:

    1. Eclipse Indigo Java EE IDE for Web Developers2. Struts 23. Hibernate 34. Hibernate Tools Eclipse Plugin Version 3.5.15. mysql JDBC jar (mysql-connector-java-5.1.23)6. Tomcat 7

    Step 1: Preparing the Database

    We used phpMyAdmin to design the database and add table to it. You can use any tool to create the table in MySQL.Below is the screenshot of the table.

    The database we created is music_manager and the table albumtbl will be used for storing the music data. Ignoregenretbl for the time being as well use this table as master table to store all music genres in the later part of thetutorial.

    CREATETABLEIFNOTEXISTS`albumtbl`(`music_id`INT(4)NOTNULLAUTO_INCREMENT,`album_title`VARCHAR(255)NOTNULL,`album_genre`VARCHAR(255)NOTNULL,

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    2/18

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    3/18

    You would require servlet-api.jar file which youll get from Tomcat installation directory. My Tomcat is inC:\Java\Tomcat\tomcat7 folder.

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    4/18

    Step 4: Add Struts 2 support

    Include Struts2.xml and put the reference in web.xml for struts2 support.

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    5/18

    web.xml

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    6/18

    "http://struts.apache.org/dtds/struts-2.0.dtd">

    index.jsp

    /WEB-INF/web/jsps/musicmgr.jsp

    listAlbumlistAlbum

    listAlbum

    Step 5: Add Hibernate support

    To work with Hibernate we used the Jboss Elipse Hibernate plugin to generate hbm files for that. This plugin isoptional as it will save some time manually generating hibernate configuration and hbm files easily. We alreadyhave a nice step by step tutorial on how you can use this plugin to auto-generate the hbm and java files. Click here togo to the tutorialhttp://www.techcubetalk.com/2013/04/step-by-step-auto-code-generation-for-pojo-domain-java-classes-and-hbm-files-using-elipse-hibernate-plugin/

    You can download the plugin from jboss.org depending upon the Eclipse version.

    http://www.techcubetalk.com/2013/04/step-by-step-auto-code-generation-for-pojo-domain-java-classes-and-hbm-files-using-elipse-hibernate-plugin/http://www.techcubetalk.com/2013/04/step-by-step-auto-code-generation-for-pojo-domain-java-classes-and-hbm-files-using-elipse-hibernate-plugin/http://www.techcubetalk.com/2013/04/step-by-step-auto-code-generation-for-pojo-domain-java-classes-and-hbm-files-using-elipse-hibernate-plugin/http://www.techcubetalk.com/2013/04/step-by-step-auto-code-generation-for-pojo-domain-java-classes-and-hbm-files-using-elipse-hibernate-plugin/http://www.techcubetalk.com/2013/04/step-by-step-auto-code-generation-for-pojo-domain-java-classes-and-hbm-files-using-elipse-hibernate-plugin/http://www.techcubetalk.com/2013/04/step-by-step-auto-code-generation-for-pojo-domain-java-classes-and-hbm-files-using-elipse-hibernate-plugin/
  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    7/18

    Step 5a: Create hibernate.cfg.xmlthis will have all the database authentication information

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    8/18

    privateintnoOfTracks;

    publicAlbumtblBO(){}

    publicAlbumtblBO(intmusicId, StringalbumTitle, String

    albumGenre, StringalbumArtists, intnoOfTracks){this.musicId=musicId;this.albumTitle=albumTitle;this.albumGenre=albumGenre;this.albumArtists=albumArtists;this.noOfTracks=noOfTracks;

    }

    publicintgetMusicId(){returnthis.musicId;

    }

    publicvoidsetMusicId(intmusicId){

    this.musicId=musicId;}

    publicStringgetAlbumTitle(){returnthis.albumTitle;

    }

    publicvoidsetAlbumTitle(StringalbumTitle){this.albumTitle=albumTitle;

    }

    publicStringgetAlbumGenre(){returnthis.albumGenre;

    }

    publicvoidsetAlbumGenre(StringalbumGenre){this.albumGenre=albumGenre;

    }

    publicStringgetAlbumArtists(){returnthis.albumArtists;

    }

    publicvoidsetAlbumArtists(StringalbumArtists){this.albumArtists=albumArtists;

    }

    publicintgetNoOfTracks(){returnthis.noOfTracks;

    }

    publicvoidsetNoOfTracks(intnoOfTracks){this.noOfTracks=noOfTracks;

    }

    }

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    9/18

    Step 5c: Albumtbl.hbm.xmlthis contains the mapping between the fields of the table albumtbl table and the fieldsof the POJO class AlbumBO.

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    10/18

    publicstaticSession getSession(){returnhbmSessionFactory.openSession();

    }

    /*** closeSession closes the session, if it exists*/

    publicstaticvoidcloseSession(Session inSession){if(inSession !=null){

    inSession.close();}

    }}

    packagecom.tctalk.apps.mmgr.utils;

    publicinterfaceMusicMgrConstant {

    String_HIBERNATE_CONFIG_LOCATION ="hibernate.cfg.xml";}

    Step 5e: MusicManagerDao.java and MusicManagerDaoImpl.javathese are the DAO classes to have methodsto list, add and delete data in database.

    packagecom.tctalk.apps.mmgr.db.dao;

    importjava.util.List;

    importcom.tctalk.apps.mmgr.db.businessobjects.AlbumtblBO;

    publicinterfaceMusicManagerDao {publicListgetAllMusicAlbumsFromCollection();

    publicbooleanaddAlbum(AlbumtblBO album);

    publicbooleandelAlbum(intalbumId);}

    packagecom.tctalk.apps.mmgr.db.dao;

    importjava.util.List;

    importorg.hibernate.Criteria;importorg.hibernate.Session;

    importcom.tctalk.apps.mmgr.db.businessobjects.AlbumtblBO;importcom.tctalk.apps.mmgr.utils.HibernateUtils;

    publicclassMusicManagerDaoImpl implementsMusicManagerDao {

    publicListgetAllMusicAlbumsFromCollection(){ListalbumList =null;Session hbmSession =null;try{

    hbmSession =HibernateUtils.getSession();Criteria criteria =

    hbmSession.createCriteria(AlbumtblBO.class);albumList =criteria.list();

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    11/18

    }catch(Exceptionex){ex.printStackTrace();

    }finally{HibernateUtils.closeSession(hbmSession);

    }

    returnalbumList;}

    publicbooleanaddAlbum(AlbumtblBO album){Session hbmSession =null;booleanSTATUS_FLAG =true;try{

    hbmSession =HibernateUtils.getSession();hbmSession.beginTransaction();//add the album to the hibernate session to savehbmSession.save(album);hbmSession.getTransaction().commit();

    }catch(Exceptionex){ex.printStackTrace();

    STATUS_FLAG =false;}finally{

    HibernateUtils.closeSession(hbmSession);}returnSTATUS_FLAG;

    }

    publicbooleandelAlbum(intalbumId){Session hbmSession =null;booleanSTATUS_FLAG =true;try{

    hbmSession =HibernateUtils.getSession();hbmSession.beginTransaction();//first retrieve the album corresponds to that id

    AlbumtblBO albumObj =(AlbumtblBO)hbmSession.load(AlbumtblBO.class, albumId);

    hbmSession.delete(albumObj);hbmSession.getTransaction().commit();

    }catch(Exceptionex){ex.printStackTrace();STATUS_FLAG =false;

    }finally{HibernateUtils.closeSession(hbmSession);

    }returnSTATUS_FLAG;

    }

    }

    Step 6: Create the UI layer in WebContent section

    We created web folder to keep all the UI related files there. Since this app has only one JSP well create jsp folderand copy musicmngr.jsp.In the we are posting the form data to the action addAlbum . Underneath this we are iterating through the albumList ina table to list the music album.Musicmngr.jsp

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    12/18

    TechcubeTalk.com - Let's build apps from scratch series - Personal Music ManaApplication:: TechcubeTalk.com - Personal Music Manager ::

    Album TitleMusic GenreArtist NamesTotal No of TracksDelete

    delete

    Step 7: Add action class

    Action class MusicManagerAction.java will handle all data handling for the jsp. This class extendsMusicManagerForm which is nothing but a POJO to have all the getter/setters correspond to all form values andalbumList for the jsp.

    getAllAlbumList() method retrieves the music album list from database through MusicManagerDelegate and sets inthe albumList variable.

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    13/18

    addAlbumToCollection() method adds the music album to the database.

    delAlbumFromCollection() deletes the particular album based on the musicId from the database.

    MusicManagerAction.java

    packagecom.tctalk.apps.mmgr.web.actions;

    importjava.util.List;

    importcom.tctalk.apps.mmgr.db.businessobjects.AlbumtblBO;importcom.tctalk.apps.mmgr.web.delegates.MusicManagerDelegate;importcom.tctalk.apps.mmgr.web.forms.MusicManagerForm;

    publicclassMusicManagerAction extendsMusicManagerForm {

    privatestaticfinallongserialVersionUID =9168149105719285096L;privateMusicManagerDelegate musicMgrDelegate =new

    MusicManagerDelegate();

    publicStringgetAllAlbumList(){ListalbumList =musicMgrDelegate.getAllMusicAlbums();StringreturnString =ERROR;

    if(albumList !=null){setAlbumList(albumList);returnString =SUCCESS;

    }returnreturnString;

    }

    publicStringaddAlbumToCollection(){StringreturnString =ERROR;

    AlbumtblBO album =getAlbum();

    if(musicMgrDelegate.addAlbumToCollection(album)){returnString =SUCCESS;

    }

    returnreturnString;}

    publicStringdelAlbumFromCollection(){StringreturnString =ERROR;

    intalbumId =getMusicId();

    if(musicMgrDelegate.delAlbumFromCollection(albumId)){returnString =SUCCESS;}

    returnreturnString;}

    }

    MusicManagerForm.java

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    14/18

    packagecom.tctalk.apps.mmgr.web.forms;

    importjava.util.List;

    importcom.opensymphony.xwork2.ActionSupport;importcom.tctalk.apps.mmgr.db.businessobjects.AlbumtblBO;

    publicclassMusicManagerForm extendsActionSupport {

    privatestaticfinallongserialVersionUID =706337856877546963L;

    privateListalbumList =null;privateAlbumtblBO album =

    null;privateintmusicId;

    publicAlbumtblBO getAlbum(){returnalbum;

    }

    publicvoidsetAlbum(AlbumtblBO album){this.album=album;

    }

    publicListgetAlbumList(){returnalbumList;

    }

    publicvoidsetAlbumList(ListalbumList){this.albumList=albumList;

    }

    publicintgetMusicId(){

    returnmusicId;}

    publicvoidsetMusicId(intmusicId){this.musicId=musicId;

    }

    }

    Step 8: Add Delegate class

    Delegate class acts as a bridge between the presentation layer and the database handling layer. It takes the inputand pass to database layer (dao classes) to add/delete data in the database. Similarly it fetches the data fromdatabase and displays in the page.

    MusicManagerDelegate.java

    packagecom.tctalk.apps.mmgr.web.delegates;

    importjava.util.List;

    importcom.tctalk.apps.mmgr.db.businessobjects.AlbumtblBO;importcom.tctalk.apps.mmgr.db.dao.MusicManagerDao;

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    15/18

    importcom.tctalk.apps.mmgr.db.dao.MusicManagerDaoImpl;

    publicclassMusicManagerDelegate {MusicManagerDao mmgrDao =(MusicManagerDao)new

    MusicManagerDaoImpl();

    publicListgetAllMusicAlbums(){returnmmgrDao.getAllMusicAlbumsFromCollection();}

    publicbooleanaddAlbumToCollection(AlbumtblBO albumobj){returnmmgrDao.addAlbum(albumobj);

    }

    publicbooleandelAlbumFromCollection(intalbumId){returnmmgrDao.delAlbum(albumId);

    }}

    Step 9: Final Integration

    Once all are integrated the final package structure will look like this below-

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    16/18

    Build the project and right click on it and select Export as war file and save in a known folder. Open the Tomcatmanager console app and browse the WAR file to install it.

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    17/18

    Once installed, run the app as (http://localhost:8080/PersonalMusicManagerAppthe URL may vary based on yourTomcat installation folder/port etc.)

    If everything is running successfully the screen will be shown -

  • 8/11/2019 Workshop on Creating a Personal MusicManager Web Application with Struts2.docx

    18/18

    http://www.techcubetalk.com/wp-content/themes/wp-max/images/uploads/2013/04/image017.png