July 1 2016

javax faces project stage configuration

javax faces project stage configuration

If you are working with JSF project, you should enable the ‘PROJECT STAGE’ for better performance. Project stage in JSF can be configured in web deployment descriptor file (web.xml) as context param.

<context-param>

<param-name>javax.faces.PROJECT_STAGE</param-name>

<param-value>Development</param-value>

</context-param>

The possible Project Stage values are Continue reading

Category: Jsf | LEAVE A COMMENT
June 23 2016

Jsf download file example

Jsf download file example


<!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"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

<h:head>
<title> Jsf File Download </title>
</h:head>

<h:body>

<h:form>

<h3> Jsf File Download </h3>

<h:commandButton value="Submit" actionListener="#{jsfFileDownload.download}" />

</h:form>
</h:body>

</html>

Continue reading

Category: Jsf | LEAVE A COMMENT
June 16 2016

JSF managed property

JSF managed property is used to access one managed bean in other managed bean (Dependency Injection).Managed property can be configured using @ManagedProperty annotation or by adding <managed-property> to <managed-bean> in faces-config.xml

In the below example, ‘requestManagedBean’ managed bean have managed property for ‘verificationManagedBean’ managed bean

Note Continue reading

Category: Jsf | LEAVE A COMMENT
October 19 2015

Jsf setpropertyactionlistener example

Jsf setpropertyactionlistener example

Jsf ‘<f:setPropertyActionListener>’ tag creates a special action listener and registers with the associated action. It sets value specified by ‘value’ attribute to ‘target’ attribute.It is generally used in datatable to get the record selected by the user.

Attribute          Value expression
value                store the value of target attribute
target               defines the destination of value attribute

In the below example, the ‘stud’ property is set to student property of the studentBean. This will take place before editStudent method is executed. Continue reading

Category: Jsf | LEAVE A COMMENT
August 12 2015

Jsf datatable row index

Jsf datatable row index

Jsf datatable row index is used to display the row numbers for each row. This can be done by using binding attribute of datatable. Bind the table to the view itself instead of a bean.

<h:dataTable binding=”#{student}”  .. >

Add a column with the following code

<h:column>

#{student.rowIndex + 1}

</h:column>

Example Continue reading

Category: Jsf | LEAVE A COMMENT
August 7 2015

Jsf validator method

Jsf validator method

Jsf validation can be done in three ways using standard validation tags, validator methods in backing beans and validator classes. Validator method and validator classes are used to write application specific or custom validations.

Validator methods are created when the validation is specific to the page. The validator methods are created in the backing beans and it should have the following syntax

public void methodname(FacesContext facesContext,UIComponent component,

Object objectToValidate) throws ValidatorException

If component’s value is not according to the expected, throw a new ValidatorException instance. If an exception is thrown, Jsf will automatically add the FacesMessage instance to the message queue. Continue reading

Category: Jsf | LEAVE A COMMENT
August 6 2015

Jsf custom validator

Jsf custom validator

A custom validator in Jsf can be created by implementing javax.faces.validator.Validator interface which has a one method (validate).

public void validate(FacesContext context, UIComponent component,Object value) throws ValidatorException;

The class has to implement validate method. If component’s value is not according to the expected, throw a new ValidatorException instance. If an exception is thrown, Jsf will automatically add the FacesMessage instance to the message queue.

The validator has to be registered and a unique identifier has to be assigned to it. Validator can be registered by adding it in faces-config.xml or by adding @FacesValidator annotation to the Validator class.

The validator can be applied to the component using <f:validator> by passing the validator unique identifier as parameter. Continue reading

Category: Jsf | LEAVE A COMMENT
July 29 2015

Jsf rendered attribute example

Jsf rendered attribute example

The rendered attribute which uses Boolean EL expression indicates whether a component is currently visible or not. The property is useful when you want to hide components for specific users or based on condition. The default value of rendered attribute is true.

Example:

<h:outputText value=”mapping” rendered=”Boolean EL expression” />

rendered Display the component
true Yes
false No

 Example Continue reading

Category: Jsf | LEAVE A COMMENT
May 25 2015

Jsf session timeout

Jsf session timeout

Session timeout is a property that is assigned to the Session object. If user do not request or refresh the page with the specified time, the session will end.

Jsf session timeout can be configured in two ways

  1. Adding ‘session-timeout’ in web.xml

Configure the value for ‘session-timeout’ of ‘session-config’ element in web.xml .The value should be specified in minutes

In the below example, if user does not request or refresh for 30 minutes the container will end the session. Continue reading