Skip to main content

Posts

Showing posts from April, 2007

Making a simple JSF application

Here's how we build a simple JSF application: Configure web.xml for FacesServlvet Create faces-context.xml (This is the file from where FacesServlet reads navigation rules, details of managed beans and other JSF specific configuration details) Create a controller or controllers for the application. Here is where JSF differs from Struts. Struts had a front controller, the ActioServlet which delegated requests to Action classes. JSF does not have the notion of a front controller. A JSF application uses an event model. The UI is tied to the backend with events. Controller classes (also known as backing beans in JSF) accept these events and also hold properties that accept user supplied values from a form. These properties will also be used to display data on pages. For example if we have a simple application that allows employees to put in a leave request, a backing bean will have properties that will be populated with the request that the user enters on the form. The same backing bea

Creating a custom component

The primary responsibility of a component is to decode and encode data. encode = convert request parameters to component attributes decode = render component attributes to the view (render html mostly...) The rendering can be directly implemented by the component or can be delegated to a renderer. We use a special renderer(s) when we want different types of rendering for that component (maybe for different display devices). So a JSF component has 2 parts: the component + renderer Image by Rick Hightower explaining on where rendering fits in the JSF lifecycle - Ref All custom components have to be subclasses of UIComponent. However, we subclass UICOmponentBase which is an abstract class that gives us a skeletal implementation of UIComponent. Creating the custom component: As an exercise, I will create a simple custom component that renders a 5x5 grid (as an html table). Here are the steps: Create a subclass of UIComponent (I chose UIOutput, since this is an output only component, ie. we

Details of JSF's MVC implementation:

Still reading Rick Hightower's article on JSF. This image has been referenced from Rick Hightower's article . The JSP page (essentially the tags) is bound to a server side view_root that maintains the state of the UI components. The view root is a composite just like Swing components. Any changes a user makes in their browser's view get reflected in the state of the view root. The view is also linked to properties (or nested properties) of a backing bean. It is recommended that we do not put business logic in the backing bean, they are meant to mediate between the view and the model. At first I thought, why do we need these backing beans? The properties can be set in the View Root objects, and they can handle the events as well, but I think there is a good reason for them. The View Root components are written by the component provider. We do not have control on them. However our application needs to have event handlers, and maybe transfer objects. These are bound to the v

Learning Java Server Faces

Resources: Marty Hall has a good JSF tutorial here . Rick Hightower has a very good 4 part series on JSF here . Reading the first part of Rick's tutorial. JSF provides component tags for every input field available in standard HTML. [Ref] Well I guess, JSTL also did this. JSF components are stateful. The statefulness of the components is provided through the JSF framework. JSF uses components to produce HTML responses. Were JSP tags also stateful? No. Assuming we are using Struts, let's say we change the state of a form, the state change is transmitted to the backend as an HTTP request. This causes some bean properties to be set. The original form can now be displayed again, using the form bean as a a source for the state of the form. This form will reflect the new change as long as form bean is accessible to the JSP tag. So the state is saved in the form bean, and the fact that the form bean is made accessible to the Tag helps us manage the state of the UI. In JSF, I believe

Would you like to take a pay-it-forward course?

Those of you who have seen my services section, will have noticed a virtual learning offering. I have been offering 2 courses: Java Programming Best Practices, and Object Oriented Design. Till now the fees for each course was $100 for a 6 - 8 week period. However, I have had the desire to work on the Pay-It-Forward model from a long time. I first read about the Pay-It-Forward concept on Liegh Blackall's wiki . What it really means is every participant has the option to not pay the course fees if they volunteer to mentor future participants or volunteer in other ways towards development/maintenance of the course. What a wonderful idea. This way we can create a community of mentors and move towards peer-taught, outcome-based courses. (Just as an aside Mark Shuttleworth and others are planning some very interesting things using the concept of peer taught and outcome based courses to help school students gain analytical skills.) Please send a mail to adaptives[at]gmail[dot]com, if yo

Polymorphism

Polymorphism is the ability of an object to assume multiple forms. The example below assumes a class heirarchy with Appender as the superclass. For now it is sufficient to know that the Appender class is responsible for appending log statements to a certain destination. This class has three subclasses, ConsoleAppender, FileAppender, and DatabaseAppender, each of which implement the 'append' of the superclass to direct the log statement to the console, file, and database respectively. The audio explains polymorphism with this simple example. [Time: 3 mins 40 secs] Watch a simple animation of how Polymorphism works. Discuss this post in the learning forum . Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net     Commercial Links Buy programming books from our Amazon.com powered storefront . Earn as you browse. Sign up for Algoco .

Inheritance

Logging frameworks like Log4J allow us to send logs to a multitude of destinations. The code responsible for logging to a destination is usually encapsulated into seperate classes. Without inheritance With inheritance When to use inheritance: IS-A relationship An Employee is a Person A BumperSale is a Sale A Square is a Shape IS-LIKE-A relationship But if the relationship between them is HAS-A (eg: Car has an Engine), then we use composition instead of inheritance. Reflections: What is the relationship between the following objects? Bathroom - Bathtub Car - Engine Person - Professor Vehicle - Car Discuss this post in the learning forum . Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net   Commercial Links Buy programming books from our Amazon.com powered storefront . Earn as you browse. Sign up for Algoco .

Encapsulation and information hiding

Encapsulation and information hiding are often used interchangeably. But they are actually different. Information hiding is a design principle, whereas encapsulation is a langauge facility. Encapsulation is the process of bundling code and the data it acts on together in one entity. David Parnas describes information hiding as "hiding of critical design decisions", so that the client code does not have to understand the intricacies of the design, and is also oblivious to any changes in design. Encapsulation can happen without information hiding, but will not be effective. Encapsulation facilitates, but does not gaurantee information hiding. Following some simple guidelines will help us create better classes   Rules for encapsulation: Rules for information hiding: These rules have been taken from this JavaWorld article on encapsulation . It's an excellent article that explains the concepts very well with relevant and well thought of code samples. Something on the side

Abstraction

According to Wikipedia - Abstraction is the process of reducing the information content of a concept, typically in order to retain only information which is relevant for a particular purpose. For example, abstracting a premiership football to a ball retains only the information on general ball attributes and behaviour. Similarly, abstracting an emotional state to happiness reduces the amount of information conveyed about the emotional state. Click on the audio player below to listen to an explanation of what abstraction means in object oriented development.   This photograph is attributed to gautamnguitar, and is posted on Flickr with a Creative Commons license. Discuss this post in the learning forum . Resources: What is data abstraction?     Commercial Links Buy programming books from our Amazon.com powered storefront .

What does object oriented design promise?

In this section we will refresh basic object oriented concepts, to ensure that everyone understands the basics before proceeding.  Let us start with a brief history of object orientated programming. Object orientation is a step in the evolution of software design The purpose of object orientation is to create programs that are flexible, maintainable, and robust. Discuss this post in the learning forum . Notes: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net

Object oriented design

Simula 67 was the first object oriented language, and as it's name suggests was created in 1967. Since then many object oriented languages have been created, all with the purpose of easing software development and making it easier to write robust, maintainable, and flexible programs. In the next few posts I will cover the fundamental principles of programming with objects and how to apply those principles while coding in real life situations. Remember, even though object orientation gives us constructs for writing maintainable programs, if we do not use them properly, the resulting code will probably be more unmaintainable than simple structured programs. These are some of the topics that I will post about. A quick refresher of object oriented principles In this section we will once again refresh the basic concepts of abstraction, encapsulation, inheritance, and polymorphism.  Translating requirements into system design In this section we will understand how to identify classes and

How to be a great programmer

Recently, someone on the JavaPosse newsgroup asked "how many hours do Java programmers sleep?". Relevant question isn't it? With so many langauge enhancements, API's, frameworks, specifications, and other stuff to learn, one either has to stop learning or stop sleeping. Well maybe not :-)... both can coexist, but what is needed is a great passion for programming. Heinz Kabutz, author of the famous Java Specialists newsletter shared some very interesting observations on how to be an excellent programmer in his 100th newsletter . I recommend that everyone who aspires to excell in programming should read it. Even though he speakes specifically about Java, the concept holds true not only for other languages, but for other professions as well. Discuss this post in the learning forum .