Friday, August 29, 2014

How to create multilevel menu's in JSP using recursion..

There was a requirement where in I need to create multi-level menu's in a JSP

A role has certain set of Privileges.
Each privilege may or may not have child privileges..

Where Privilege has child privileges, it was desired to display a sub menu..

So this is how I have achieved...
First thing you need to know about this is how to create multi level menu's using HTML and CSS.

and once you learn that you'll have to use recursion in JSP to achieve the above requirement.

I'll just put the core logic here which is the base of this..

Create a parent jsp (lets say LoginSuccess.jsp)

<ul id="nav">
<c:forEach var="role" items="${userDbObject.roles}"> 
  <li><a href="#item1">${role.roleName}</a>
   <c:forEach var="priv" items="${role.privileges}">
     <c:set value="${priv}" var="myPriv" scope="request"/>
     <jsp:include page="showPrivileges.jsp"/>

Create a showPrivileges.jsp as follows:

<%@taglib prefix="c" uri=""%>
<c:if test="${empty myPriv.childPrivileges}">
  <c:set var="url" value="${myPriv.privUrl}" />
  <c:if test="${not empty url}">
   <li><a href="${url}" >${myPriv.privilegeName}</a></li>
  <c:if test="${empty url}">
   <li><a href="unimplemented.jsp" >unimplemented.jsp</a></li>
<c:if test="${not empty myPriv.childPrivileges}">
   <li><a href="#" >${myPriv.privilegeName}</a>
    <c:forEach var="childPriv" items="${myPriv.childPrivileges}">
      <c:set value="${childPriv}" var="myPriv" scope="request"/>
      <jsp:include page="showPrivileges.jsp"/>

Thursday, August 28, 2014

How to create multi-level menu's using HTML and CSS

Create a style.css as follows

body {
 font: normal .8em/1.5em Arial, Helvetica, sans-serif;
 background: #ebebeb;
 width: 900px;
 margin: 100px auto;
 color: #666;

a {
 color: #333;
#nav {
 margin: 0;
 padding: 7px 6px 0;
 /*background: #7d7d7d  repeat-x 0 -110px;*/
 line-height: 100%;

 border-radius: 2em;
 -webkit-border-radius: 2em;
 -moz-border-radius: 2em;

 -webkit-box-shadow: 0 1px 3px rgba(0,0,0, .4);
 -moz-box-shadow: 0 1px 3px rgba(0,0,0, .4);
#nav li {
 margin: 0 5px;
 padding: 0 0 8px;
 float: left;
 position: relative;
 list-style: none;

/* main level link */
#nav a {
 font-weight: bold;
 color: #e7e5e5;
 text-decoration: none;
 display: block;
 padding:  8px 20px;
 margin: 0;

 -webkit-border-radius: 1.6em;
 -moz-border-radius: 1.6em;
 text-shadow: 0 1px 1px rgba(0,0,0, .3);
#nav a:hover {
 background: #000;
 color: #666;

/* main level link hover */
#nav .current a, #nav li:hover > a {
 background: #000  ;
 color: #fff;
 border-top: normal 0px #f8f8f8;

 -webkit-box-shadow: 0 1px 1px rgba(0,0,0, .2);
 -moz-box-shadow: 0 1px 1px rgba(0,0,0, .2);
 box-shadow: 0 1px 1px rgba(0,0,0, .2);

 text-shadow: 0 0px 0 rgba(255,255,255, 1);

/* sub levels link hover */
#nav ul li:hover a, #nav li:hover li a {
 background: none;
 border: none;
 color: #666;

 -webkit-box-shadow: none;
 -moz-box-shadow: none;
#nav ul a:hover {
 background: #0078ff  repeat-x 0 -100px !important;
 color: #fff !important;

 -webkit-border-radius: 0;
 -moz-border-radius: 0;

 text-shadow: 0 0px 0px rgba(0,0,0, .1);

/* dropdown */
#nav li:hover > ul {
 display: block;

/* level 2 list */
#nav ul {
 display: none;

 margin: 0;
 padding: 0;
 width: 185px;
 position: absolute;
 top: 35px;
 left: 0;
 background: #ddd  repeat-x 0 0;
 border: solid 1px #b4b4b4;

 -webkit-border-radius: 10px;
 -moz-border-radius: 10px;
 border-radius: 10px;

 -webkit-box-shadow: 0 1px 3px rgba(0,0,0, .3);
 -moz-box-shadow: 0 1px 3px rgba(0,0,0, .3);
 box-shadow: 0 1px 3px rgba(0,0,0, .3);
#nav ul li {
 float: none;
 margin: 0;
 padding: 0;

#nav ul a {
 font-weight: normal;
 text-shadow: 0 1px 0 #fff;

/* level 3+ list */
#nav ul ul {
 left: 181px;
 top: -3px;

/* rounded corners of first and last link */
#nav ul li:first-child > a {
 -webkit-border-top-left-radius: 9px;
 -moz-border-radius-topleft: 9px;

 -webkit-border-top-right-radius: 9px;
 -moz-border-radius-topright: 9px;
#nav ul li:last-child > a {
 -webkit-border-bottom-left-radius: 9px;
 -moz-border-radius-bottomleft: 9px;

 -webkit-border-bottom-right-radius: 9px;
 -moz-border-radius-bottomright: 9px;

/* clearfix */
#nav:after {
 content: ".";
 display: block;
 clear: both;
 visibility: hidden;
 line-height: 0;
 height: 0;
#nav {
 display: inline-block;
html[xmlns] #nav {
 display: block;
* html #nav {
 height: 1%;

Create body of your HTML as follows

<link href="style.css" rel="stylesheet" type="text/css" />
                <!-- Navigation -->  
                <ul id="nav"> 
                    <li><a href="#">Home</a></li>  
                    <li><a href="#">Categories</a>  
                            <li><a href="#">All</a>  
                                    <li><a href="#">Category 1</a>  
                                        <ul class="droprightMenu">  
            <a href="#">Category 1.2</a>
                                                    <li><a href="#">Category 1.2.1</a></li>
            <a href="#">Category 1.3</a>
                                                    <li><a href="#">Category 1.3.1</a></li>
                                            <li><a href="#">Category 1.4</a></li>  
                                    <li><a href="#">Category 2</a></li>  
                                    <li><a href="#">Category 3</a></li>  
                                    <li><a href="#">Category 4</a></li>  
                            <li><a href="#">Manage</a></li>  
                    <li><a href="#">Profile</a>  
                            <li><a href="#">Login</a></li>  
                            <li><a href="#">Register</a></li>  
                            <li><a href="#">Edit Profile</a></li>  
                            <li><a href="#">My Posts</a></li>  
                            <li><a href="#">Logout</a></li>  
                    <li><a href="#">Help</a></li>  

And the output will look like:

Tuesday, August 26, 2014

How to implement a self join in hibernate

Source :

We have a create table in the database.

CREATE TABLE employee (
employee_id NUMBER(10) NOT NULL,
PRIMARY KEY ('employee_id'),
CONSTRAINT 'FK_MANAGER' FOREIGN KEY ('manager_id') REFERENCES 'employee' ('employee_id')

Here in Employee table, we defined a column MANAGER_ID which is mapped to the same table’s primary key. Thus for each employee we will store its manager’s id also. Manager will be yet another employee in this table

We will be using annotations to implement this in hibernate:

public class Employee {
    private Long employeeId;
    private String firstname;
    private String lastname;
    private Employee manager;
    private Set<Employee> subordinates = new HashSet<Employee>();
    public Employee() {
    public Employee(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    // Getter and Setter methods

Note that in Employee entity class, we defined two new attributes: Employee manager and Set subordinates. Attribute manager is mapped with @ManyToOne annotation and subordinates is mapped with @OneToMany. Also within @OneToMany attribute we defined mappedBy="manager" making manager as the relationship owner and thus which manages the foreign relationship within table.

Also the annotation @JoinColumn is defined on manager making it the relationship owner. @JoinColumn defines the joining column which in our case is manager_id.

Monday, August 25, 2014

How to integrate Spring and Hibernate

Here are the few tips I have gathered on how to integrate spring and hibernate.

Create your bean.xml something like this...

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="" xmlns:xsi="" xsi:schemaLocation="">

<bean id="usrMgmtServiceImpl"   class="com.usermanagement.service.impl.UserManagementServiceImpl">
<property name="privService" ref="privService"/>
<property name="roleService" ref="roleService"/>
<property name="userService" ref="userService"/>

<!--               DB CONFIGURATION BEGINS  -->

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value></value></property>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
<property name="driverClassName">  
<property name="url">  
<property name="username">  
<property name="password">  

<bean id="mysessionFactory"  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- These are the POJO objects, you have to mention these here... -->
<property name="annotatedClasses">

<property name="hibernateProperties">  
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="">update</prop>
<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.cache.use_second_level_cache">true</prop> 
<!--<prop key="hibernate.cache.provider_class">org.hibernate.cache.SingletonEhCacheProvider</prop>-->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">  
<property name="sessionFactory" ref="mysessionFactory"></property>  
<!--               DB CONFIGURATION ENDS  --> 

<bean id="privService" class="com.usermanagement.service.impl.PrivilegeServiceImpl">
<property name="privDao" ref="privDao"/>
<bean id="roleService" class="com.usermanagement.service.impl.RoleServiceImpl">
<property name="roleDao" ref="roleDao"/>
<bean id="userService" class="com.usermanagement.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>

<bean id="privDao" class="com.usermanagement.dao.impl.PrivilegeDAOImpl" >
<property name="template" ref="template"></property>
<bean id="roleDao" class="com.usermanagement.dao.impl.RoleDAOImpl" >
<property name="template" ref="template"></property>
<bean id="userDao" class="com.usermanagement.dao.impl.UserDAOImpl">
<property name="template" ref="template"></property>


The list of jars that you might need are
  1. antlr-2.7.6.jar
  2. asm-1.5.3.jar
  3. axis2-spring-1.6.1.jar
  4. cglib-2.1_3.jar
  5. commons-collections-3.1.jar
  6. commons-dbcp-1.4.jar
  7. commons-logging-1.0.4.jar
  8. commons-pool-1.6.jar
  9. dom4j-1.6.1.jar
  11. hibernate-3.2.6.jar
  12. hibernate-annotations-3.4.0.GA.jar
  13. hibernate-commons-annotations-3.1.0.GA.jar
  14. hibernate-core-3.3.0.SP1.jar
  15. hibernate-entitymanager.jar
  16. hibernate-jpa-2.0-api-1.0.0.Final.jar
  17. jta-1.1.jar
  18. log4j-1.2.14.jar
  19. ojdbc14.jar
  20. org.springframework.asm-3.0.0.RELEASE.jar
  21. org.springframework.beans-3.0.0.RELEASE.jar
  22. org.springframework.context-3.0.0.RELEASE.jar
  23. org.springframework.core-3.0.0.RELEASE.jar
  24. org.springframework.expression-3.0.0.RELEASE.jar
  25. org.springframework.jdbc-3.0.0.RELEASE.jar
  26. org.springframework.orm-3.0.0.RELEASE.jar
  27. org.springframework.web.servlet-3.0.1.RELEASE-A.jar
  28. slf4j-api-1.6.1.jar
  29. spring-tx-3.0.0.RELEASE.jar