Skip to main content

Posts

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 .