Reality is a perception. Perceptions are not always based on facts, and are strongly influenced by illusions. Inquisitiveness is hence indispensable

Thursday, January 1, 2009

Web development framework - Struts 1 - part2

In this post, we will see some nuances involved in struts-config.xml, as described earlier, struts-config is set of guidelines for actionservlet that help request delegation.

Struts config example


The following xml file illustrates some of the common features.
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="SimpleFormBean" type="org.ui.beans.SimpleFormBean"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
<forward name="success" path="/success.jsp"/>
<forward name="showLogin" path="/csaLogin.jsp"/>
</global-forwards>
<action-mappings>
<action input="/csaLogin.jsp" name="SimpleFormBean" parameter="simpleShow" path="/show" scope="session" type="org.ui.actions.SimpleStrutsAction"/>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>
<message-resources parameter="com/myapp/struts/ApplicationResource"/>
<message-resources key="newBundle" parameter="com/myapp/struts/NewResource"/>

<!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>




The config file is made up of three major aspects: formbeans, action mappings, message-resources. ActionServlet processes the action mappings and identifies the action to be used for the current request. The 'path' attribute influences the decisions. The action element in xml also specifies the target action class in 'type' attribute and form bean to be used using 'name' attribute. The form bean listed in 'name' also listed under form beans element in the config xml file. The class used for bean representation is mentioned here. An action element can also directly map to a html or jsp file.

The action class contract dictates the use of execute() method. (however, using dispatch action forms with parameters, we can call any method of our choice). The form-bean as discussed earlier, is a good candidate for validation actions.

An example of how validations would look.

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getName() == null || getName().length() < 1) {
errors.add("name", new ActionMessage("error.name.required"));
}
if(getNumber() <= 0){
errors.add("number",new ActionMessage("error.posnumber.needed",getNumber()));
}
{
int x = (int)getNumber();
double y = x;
int res = ((Double)y).compareTo(getNumber());
if(res != 0){
errors.add("number",new ActionMessage("error.integer.needed",getNumber()));
}
}
return errors;
}


The jsp file meant to use the errors looks like this:

  <div id="page1">
<html:errors bundle="newBundle"/>
<html:form action="show.do" method="post">
<div class="fields">
<label for="user">USER</label> <html:text property="name" ></html:text>
<html:errors bundle="newBundle" property="name"></html:errors>
<label for="user-id">USER-ID</label><html:text property="number" ></html:text>
<html:errors property="number"></html:errors>
</div>
<span class="buttons">
<html:submit property="submitType" value="submit1"/>
<html:submit property="submitType" value="submit2"/>
</span>
</html:form>

</div>


The attribute bundle="newBundle" is another aspect, the message properties can be customised (one of the starting steps for internationalisation, no we won't be looking at it in this post). We can have as many message/resource bundles as we like. Have a look at struts-config.xml. The bundle attribute in error tags should map to the key attribute given in struts config.

That summarised our discussion on basic struts.

Web development framework - Struts 1 - part1

For web development in j2ee, one needs to know about basic deployment strategy. The 'basic' stuff includes, knowledge of web server and configuration files required by the framework. For servlet/jsp applications web.xml, or deployment descriptor is the key. Production apps have their own framework and come with their own framework (scripts and config files), this is not the objective of this exercise though.

To simplify development, people have been developing utilities. Struts is one such aid. Supported by Apache. This is over and above the basic strategy. Struts dictates that the users provide config file(s). Let us see the various aspects involved in configuration in this blog.

Web.xml


Web.xml is the starting point for any web application. For a struts application the web.xml looks in the following fashion.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>




To understand the above, one needs to have some know how of relation between: servlet-class, servlet-name and servlet-mapping. They are supposed to provide degrees of freedom to various stakeholders namely: the java developer, the server admin or (author of deployment descriptor) and the network admin responsible for url allocation.

All struts applications try to mock front-controller pattern. (A true front controller has no logic what so ever and just delegates, it is left to application designers to come up with the delegators/handlers. In struts 1.xx this is achieved only by using a 'request processor', which is not the default. The delegation logic is held by front-controller in default case. The user can configure the delegation rules). The front-controller is called 'ActionServlet' and is part of struts library. In the above example all urls that match the regex *.do are mapped to this servlet. There are some initialization parameters given to action servlet, the most important of these is the 'config' parameter, we supply struts-config.xml(s) here. Multiple config.xmls are allowed and are comma separated. The config.xmls contain the rules to be followed for request delegation. These are used by ActionServlet.

When a request hits actionservlet, the servlet tries to map it to the appropriate action. Action is a specialised controller. ActionServlet also maps the request parameters to a bean, called as form-bean in struts lingo. By the time request reaches action class, the form-bean gets populated and acts as a data structure. Validation of parameters can be done in the Action class, but the most appropriate location would be form-bean, as per ObjectOriented model. We will see more of struts-config.xml in the next part

Last but not the least are tld definitions provided at the end. These have more to do with jsp tag-libs than struts framework. Think of them as helper classes.

Note


Struts 2 and Spring framework are ideologically more similar than struts 1. In the former, user is allowed to dictate the front-controller (dispatch servlet), user is not forced to extend framework classes. This amounts to greater freedom of design (loose coupling or framework agnostic design). However, how many times did we see an application design change radically?

Friday, December 26, 2008

The Other side, the Ugly side - Slumdog millionaire

As statistical evidence suggests, most Indian movies are cheap imitations. Some (moi) feel that they drain the energy and test the patience limits. Bollywood's definition of sensuality is not something 'original'. The industry where dreams are sold has a strange appeal, which I rarely understood. Watching Indian movies is like watching a magician's show, the caveat, you already know the tricks.

But once in a while, when you expect the least, the rabbit is out , a real one which you would watch with child like amusement. Now that is a rarity. What is so intriguing! you tell me.

The "Slumdog millionaire" is one of those rare gems, not a bollywood movie though, it is based on the popular book 'Q and A' by Vikas Swarup. If you haven't read the book, don't miss this one. An uncouth movie with an arcane and predictable ending, but captivating it is. A bunch of lies, but beautiful ones they all are. A story teller's story which I have missed since a long time is what helped me stick. Go find out what is in for you. As the credits started rolling, I was in for another shock, so will you be when you see the name AR Rahman.

==spoiler==
A street rag, goes out to be a winner. Not much help is it, but remember that I am not going to be the one shouting kajol's name on the way out of the movie, Gupt. Wiki would definitely help, but why bother! See what I found on wiki after watching the movie,

"Rotten Tomatoes reported that 93% of critics gave the film positive write-ups, based upon a sample of 135, with an average score of 8.1/10.[20] At Metacritic, which assigns a normalized rating out of 100 to reviews from mainstream critics, the film has received an average score of 86, based on 35 reviews."

Thursday, December 4, 2008

Kick starting with hibernate

ORM is no rocket science, it is a very useful tool though. The idea of a tool to help manage transactions and bean bindings is a developer's paradise. So how do we start off? Download hibernate from internet, along with it the not so obvious slf4j-simple-xxx.jar and slf4j-api-xxx.jar files are needed.

Place these in you project class path. The next step is writing the mapping files. The file hibernate.cfg.xml is the one which spells out the all encompassing details like those of the database, connection pool etc. The < entity >.hbm.xml files contain the object mapping information along any operation specific information.

A sample hibernate config xml for MySQL


<?xml version="1.0" encoding="UTF-8"?>

<!--
Document : hibernate.cfg.xml.xml
Created on : 02 November 2008, 19:31
Author : Kanthi Swaroop Rongala
-->

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hospital_care</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
-->

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup
<property name="hbm2ddl.auto">create</property>
-->

<!--
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
-->
<mapping resource="model/resources/Doctor.hbm.xml"/>
<mapping resource="model/resources/Patient.hbm.xml"/>
<mapping resource="model/resources/Appointment.hbm.xml"/>
</session-factory>

</hibernate-configuration>




Note that I have placed all my xmls in a different package called model.resources. What about the hibernate.cfg.xml location? We can move it as well.

See the following:
public class HibernateUtil {

private static SessionFactory sessionFactory = null;
private String CONFIG_FILE_PATH = "/model/resources/hibernate.cfg.xml";

private HibernateUtil(){
}

private SessionFactory create() {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure(CONFIG_FILE_PATH).buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
return sessionFactory;
}

public static SessionFactory getSessionFactory() {
if(sessionFactory == null){
HibernateUtil utils = new HibernateUtil();
utils.create();
}
return sessionFactory;
}
}


Hope this helps in giving you that well deserved push

Dynamic proxy

Every one knows compiled code is type safe and is static. It is not possible to dynamically type the behaviour in such cases. C, C++, Java … they all fit the bill. Java has this interesting feature called Dynamic proxies which promises what it spells. Now how the hell can… well it is not exactly dynamic!. There is a provision for plugging functionality (not just behaviour) at run time without major code changes. So should we really be calling it Dynamic?

As life goes on, let’s see an example. Suppose I have come legacy code/tested code/production code/my own kitchen sink code/tutorial code…Tan(90). The purpose of the code can be as simple as that of a logger. Now I want to impart some extra functionality (not just behaviour), say an actionlistener, whoa!! On a logger!! (Bear with me for the sake of example). Now how would be do that:
  1.  Implement the interface
  2.  Create a wrapper class which uses composition
Not bad?, and where would be place this? In our code base of course! Dynamic proxies offer yet another way of doing things.

Think of a utility that creates a wrapper for the interfaces you want and lets you inspect the calls being made. Armed with this knowledge the lone ninja developer can render a killer app, well that is the idea at least. This utility is part of java since jdk1.3 and is called the Proxy.

Starring…coming to your nearest desktop…
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

What exactly are these? Proxy helps you by creating the implementing class alias proxy and InvocationHandler helps you with the inspection/introspection. Proxy needs a set of interfaces and InvocationHandler needs to be hand coded.

Some code snippets


public interface IAppLogger {

void logError(String txt);

void logInfo(String txt);

void logWarning(String txt);

}

public class GenericInvocationHandler implements InvocationHandler {


public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
StringBuilder sb = new StringBuilder();
sb.append(" Calling ---> ");
sb.append(method.getName());
sb.append("(");
sb.append(")");
System.out.println(sb.toString());
//donothing proxy
//return method.invoke(obj, args);
return null;
}

}

public class ProxyGen {

public Object createProxy(Class[] interfaces, InvocationHandler handler){
ClassLoader cl= ProxyGen.class.getClassLoader();
return Proxy.newProxyInstance(cl, interfaces , handler);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GenericInvocationHandler handler = new GenericInvocationHandler(new AppLogger());
ProxyGen factory = new ProxyGen();
ActionListener proxyObj2 = (ActionListener) factory.createProxy(new Class[]{IAppLogger.class, ActionListener.class}, handler);
proxyObj2.actionPerformed(null);
IAppLogger proxyObj = (IAppLogger) proxyObj2;
proxyObj.logError("abc");
proxyObj.logInfo("abc");
proxyObj.logWarning("abc");
}

}

As the whims of the butterfly go, in a flap of wing the requirements change. We now have a new technique under our belt.

I have seen a generic logging example on net as part of my learning, thank you Google, once again!

Thursday, November 6, 2008

Top 25 Geek Blogs

The title says it all.
http://cellphones.org/blog/news/top-25-geek-blogs

Thursday, October 30, 2008

HTML - Buttons as Links

UI designers pursue their job with the idea of getting a clean and pleasing finish. That is their job, however this single focus causes them to ignore the periphery. Accessability and Technical realisation take a back seat. A really good designer would provide a solution that is solvable and not some thing which is impossible.

What is an impossible design? Think of cramping a list of names into a horizontal row without any due consideration for 'screen estate', now if anyone does a Ctrl++ to increase the font size what would happen? It is impossible to support all resolutions, but most often the min and max constraints can be frozen, not having them defined causes even more confusion.

What is a solvable problem then! The unfortunate truth about web-dev is lack of standard environment. The browsers, CSS, java script all tend to vary. Removing backward compatibility is a sin, but that is the burden these age old browsers have to carry. The new kids on the block (Chrome), Good luck!.

Nuf of cribbing and coming to point, one of our teams was given this task of submitting forms using links. Now HTML supports form submissions on buttons and images alone! So what did they do, went ahead and used images. Not bad unless you think of resizing text. Like most puzzles which we face, I happened to run into this conversation and started twiddling with the source. Look where we are Maa.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
/* Works on Firefox2+ but not on IE6 :(*/
.link a>input{
background:none;
border:0;
border-bottom:1px blue solid;
padding:0;
height:1.2em;
cursor:pointer;
}

.link a>input:hover{
background:none;
border:0;
border-bottom:1px red solid;
padding:0;
height:1.2em;
cursor:pointer;
font-weight:600;
}


</style>
</head>

<body>
<div class="link"> <a> <input type="button" value="submit"/> </a> </div>
<div class="link"> <a> <input type="button" value="crap"/> </a> </div>
</body>
</html>

Popular Posts

Labels

About Me

Well for a start, I dont' want to!. Yes I am reclusive, no I am not secretive; Candid? Yes; Aspergers? No :). My friends call me an enthusiast, my boss calls me purist, I call myself an explorer, to summarise; just an inquisitive child who didnt'learn to take things for granted. For the sake of living, I work as a S/W engineer. If you dont' know what it means, turn back right now.