Sunday, May 24, 2009

iBatis Vs Hibernate

Hi All,

I did a research on what framework to choose for the DB layer of the application as I had to migrate one of my applications to use one of the popular frameworks available. I performed the research on the two most popular open source products - iBatis and Hibernate. I am listing down the features of each one of them below,

Hibernate :
Object <-> Relational mappingsIt provides three different O/R mapping strategies. It allows associating multiple objects to a single table row. It supports polymorphic associations, bidirectional associations and association filtering. It also provides support for basic collection types.
Hibernate query languageIt provides query language that uses objects instead of tables and makes it easy for a developer to write queries.
Improves PerformanceIt has a feature of loading the child objects in a hierarchy only when it is used called lazy loading. It also provides a scalable architecture. Connection pooling and prepared statement caching is also possible.


iBatis :
Data MapperIt allows the developer to map the result set to the class using XML. The result set can be either from a direct SQL or stored procedure.
Performance TuningSince iBatis gives the full control of the SQL query generated to the developer, it helps in performance tuning of the queries where complex joins are required to fetch the data.
Ease of MigrationSince it accepts native SQL and Stored procedures, it is easy to migrate to iBatis without much effort. The learning curve is less here as the developer just need to know about the mapping of result-set with class.


iBatis Vs Hibernate :

Friday, May 22, 2009

Struts 2.0

Hi All,

In this post I am going to brief about Struts 2.0 framework and how to configure it with the tomcat server.

Struts 2.0 is a simple MVC framework. It helps us in converting simple java class (POJO) into an action class that serves the requests. It helps us map the request parameters with the action class variables and also helps in validation of the input parameters without much coding. It provides the developer with lots of tag to handle the display of data in the web page with minimal coding. Struts 2.0 also supports ajax theme and this makes the page more dynamic with least amount of effort from the developer.

Now let us see how to configure Struts 2.0 with tomcat container. The download links are below,
Tomcat - http://tomcat.apache.org/download-55.cgi
Struts - http://struts.apache.org/download.cgi#struts216

The steps to be followed for configuring Structs are below,
1. Configure Tomcat's web.xml file to include Struts 2.0 filter.
2. Write a simple jsp page.
3. Write your simple action class.
4. Modify struts.xml (the controller) to define the action, its mapped class and the resultant Jsp (view).

web.xml Configuration :
We need to add the struts filter in web.xml so that all the requests coming to tomcat with ".action" should now be routed to struts framework. All other requests which contain .html, .gif etc will be taken care by server directly.

web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


JSP Page :
Before we create a jsp, we need to include the uri for struts tags. In the example below, we have used "<s:property value="message" />" to get the value that is set in the variable message in action class that is mapped to this jsp. Struts just invokes the getter of the variable to retrieve the value from the action class.

MyFirstView.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title> My First Struts Page </title>
</head>
<body>
<s:property value="message" />
</body>
</html>


Action Class :
Any java class can act as the request handler in struts, provided it has a public method that takes no parameters and has a String return type. The string that is returned by the method in action class will be used by struts to map to a particular jsp (as defined in struts.xml). If no method is specified in the action mapping, struts will transfer the control to "execute" method of the action class.

MyActionClass.java
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class MyActionClass extends ActionSupport
{
public static final String MESSAGE = "Struts";
private String message;

public String execute() throws Exception
{
setMessage(MESSAGE);
return SUCCESS;
}

public void setMessage(String message)
{
this.message = message;
}

public String getMessage()
{
return message;
}
}


struts.xml Configuration :
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="tutorial" extends="struts-default">
<action name="HelloWorld"
class="tutorial.MyActionClass">
<result>/MyFirstView.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>


If you have any queries/comments, please post here and I will try to reply within a day.