Managed Beans are the Plain Old Java Objects (POJO) having set of properties and operations which are available to the JavaServer Faces components directly through the Unified Expression Language(EL).
The two types of methods that can be written in a JSF managed bean class are:
The two types of methods that can be written in a JSF managed bean class are:
- Standard methods
- JSF specific methods
Standard Methods
These are simple getter and setter methods of the properties that are available with the bean. Generally we do not allow the client to access the properties directly by name, instead provide the intermediatery methods to access or set the values of the properties.The access modifier of these methods should be public so that they can be accessible anywhere. The name of the getter method normally starts with "get" and appended with the name of the variable with first letter of the variable in capital letter.
private String name;Similarly, the setter method starts with the "set" and follows the same conventions as above. The setter methods will not return a value and are void type methods as they do not require any value to be returned.
public String getName(){
return this.name;
}
JSF Specific Methods
There are four different types of JSF specific methods that can be found in the managed beans.- Action Listener Methods
- Value Change Event Listener Methods
- Navigation Handler Methods
- Validation Methods
Action Listener Methods
These are the methods we write to handle the specific events that can be triggered by invoking the actions on the JSF components like Submit buttons, Command Links etc. When an action event handler is associated with such component and the action is triggered by the user, the corresponding method in the managed bean will automatically gets executed by the Faces Controller before the actual action is performed.Say for example, your application wanted to invoke a business method which require the prior information to process, the action event handled will be useful in making that information available before the actual business methods starts execution.
To be able to qualify a method in managed bean as an action event handler method, it needs to have javax.faces.event.ActionEvent object passed as a parameter to the method and the return type should be void.
public void myActionListener(ActionEvent e){
....
}
Now you can register this event with a component in a JSF page by using the actionListener property of the component.
<h:commandButton
action="#{MyBean.myAction}"
actionListener="#{MyBean.myActionListener}" ....>
Value Change Listener Methods
Value change event listener methods are used associate the UIInput components which accepts the user input for its value such as text boxes, check boxes etc. When the value of these components changed, the registered value change listeners on these components will automatically gets executed when the form on page gets submitted.
These are very useful in situations like examining the new data entered by the user before our business logic acts on that data. You can manipulate the old data and new data before the data gets applied on to the business model.
Similar to the action listener, the value change listener method of backing bean should have defined a parameter called ValueChangeEvent and return void.
public void myValueChangeListener(ValueChangeEvent e){The event can be registered on a JSF input component by using the valueChangeListener parameter of the component.
....
}
<h:inputText valueChangeListener="#{MyBean.myValueChangeListener}" ...>
Navigation Handler Methods
Navigation Handler, also called as Action Handler, used to handle the user actions/requests. These methods accepts no parameters and returns String. The return value should be one of the navigation rule name that will render the associated view. If the return value is null or empty then the same page will be rendered.These methods generally used as a handler methods to accept the user request and invoke the actual business logic. However these can contain the actual business logic as well.
The Navigation Handler methods can be referenced in a JSF page using the Unified Expression Language in a action parameter of the component.
<h:commandButton action="#{MyBean.myAction}" ...>
Validation methods
Validation methods are used to register a user defined validation criteria on a JSF UIInput component value. The validation method must accept FacesContext, UIInput and Object objects as parameters. The Object parameter is the value that gets validated and to be typecasted in order to validate it.public void validateEmail(FacesContext context,The setValid method on the UIInput component can be used to set the validation failed flag so that the same page will render back with the error messages set if any.
UIInput component, Object value){
....
if(failed){
( ( UIInput ) component ).setValid( false );
}
}
Google Web Toolkit is a very powerful way to create rich applications with ease. It has a lot of built in widgets that we can use to create rich user interfaces. Try the demo applications which are bundled with GWT and you can feel the richness and power of the Google Web Toolkit.
GWT uses the javascript for creating the user interfaces. It has overcomed the browser dependancies efficiently. So your pages generated with GWT are simple HTML/DHTML pages and they can run on any browser.
One more advantage is that these pages will load very quickly as they are simple HTML pages.
To download the Google Web Toolkit follow the link below. The size of the download is about 22MB only..
http://code.google.com/webtoolkit/download.html
GWT uses the javascript for creating the user interfaces. It has overcomed the browser dependancies efficiently. So your pages generated with GWT are simple HTML/DHTML pages and they can run on any browser.
One more advantage is that these pages will load very quickly as they are simple HTML pages.
To download the Google Web Toolkit follow the link below. The size of the download is about 22MB only..
http://code.google.com/webtoolkit/download.html
I have a PDF file embedded with the page and I have a download link which is used to download that pdf file. When I click on the download link, it will not work since it is the same url as with the embedded pdf; and because the adobe reader is already loaded in the browser it will automatically opens in a new window and the embedded pdf will be unloaded.
To prevent this scenario, we can use the following mechanism. First create a hidded iFrame in the same window using the following code.
<iframe style="DISPLAY: none" src="/" name="iframe1"></iframe>;
Now make a small change to your download link so that the url target is pointing to the hidden frame as shown below..
<a href="http://www.yoursite.com/..." target="iframe1">Download</a>
This will solve the problem. However this method has not been tested with the browsers other than Internet Explorer, Firefox and Netscape.
Please refer to the Java Puzzle - Problem of Two Developers before reading this puzzle. This puzzle uses the same scenario from the last month.
Now in this puzzle, we have to print as shown below:
"I am developer 1
You are developer 2"
Below are the conditions:
1. We have to use the code that we modified in the puzzle Java Puzzle of The Month - February 2009.
2. No changes should be made to any of Programmers, Developer1, Developer 2 classes.
2. As usual, the minimal changes should be made to the code.
Now in this puzzle, we have to print as shown below:
"I am developer 1
You are developer 2"
Below are the conditions:
1. We have to use the code that we modified in the puzzle Java Puzzle of The Month - February 2009.
2. No changes should be made to any of Programmers, Developer1, Developer 2 classes.
2. As usual, the minimal changes should be made to the code.
The JavaServer Faces technology was introduced after the Expression Language for Java Server Pages technology has been developed. The JavaServer Faces technology required more powerful language which should do the tasks which cannot be done by the JSPs' Unified Expression Language. Below are some of the issues in using the JSP Expression Language.
- The JavaServer Faces is a component model. Components are event driven and the events generated by the components are handled by the JSF framework in various phases, unlike the JSP expression which will be evaluated immediately. This is called post-back of events. At different levels, different events will get executed like Validations, Conversions etc., which are become major limitations in using the JSP expression language.
- The immediate evaluation will be done on static objects where as the JSF components require the get and set operations on the data objects stored on the server side beans.
- The JSF components are much powerful which require direct execution of methods on the Managed Beans. Also they need to get the data during the render phase and set the data in the beans during post-back.
- Another problem they sought is registering of the various events with their methods on the server side objects which will be executed during various phases.
In ordered to overcome the above limitations, they have developed a new expression language for JavaServer Faces technology which can evaluate expressions at different phases of the application, which can get and set the data on server side objects, which can invoke the methods on the server side objects.
The expressions in EL shall be written by enclosing the expression in between "#{" and "}" literals.
e.g.