August 31 2015

Spring map injection

Spring map injection

The spring core container supports injecting the primitives, array, collections and spring bean through constructor or setter dependency injection.

A map can be injected using <map> element. A <map> element represents java.util.Map. A map can contain zero or more <entry> elements. Each <entry> represents one map entry. The <entry> element takes <key> and <value> elements.

Example

In the below example, countryMap is injected using setter dependency injection by placing the <map> element inside the <property>. The map can be injected using dependency injection by placing the <map> element inside <constructor-arg>. Continue reading

Category: Spring | LEAVE A COMMENT
August 27 2015

Spring factory method attribute

Spring factory method attribute

A spring bean can be instantiated using static factory method. A method that returns instance of a class is called factory method. The spring container uses the static factory method to instantiate the bean. The class name of the factory method has to be specified using ‘class’ attribute and the factory method name has to be specified using ‘factory-method’ attribute of the bean element.

<bean id=”employeeService” class=”com.answersz.factorymethods.EmployeeFactory” factory-method=”getEmployeeService” />

Example

In the below example, getEmployeeService is the static factory method in EmployeeFactory class which returns the instance of EmployeeService. The EmployeeService bean can instantiated by configuring class as ‘EmployeeFactory’ and factory-method as ‘getEmployeeService ‘.

EmployeeService.java Continue reading

Category: Spring | LEAVE A COMMENT
August 18 2015

Array of objects in java

Array of objects in java

Arrays are objects that can store multiple variables of same data type. The element of the array can be accessed using index number. The index of the array will always start from zero. If array’s size 5, the index will be from 0 to 4.

Array can hold either primitives or object references.If array is initialized the elements will get their default values, primitives will get the default values as per the data type whereas object references will get null.

 Array of Primitives

In the below example, marks is an array of primitives(int). Values can be initialized using index.

int[] marks = new int[5];

marks[1] = 10;

Array of Object References

In the below example, empArray is an array of Employee Object References. The Employee object reference has to be assigned to the array using index. Continue reading

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
August 5 2015

Compare two dates in java

Compare two dates in java

Dates can be compared using different ways

1. Comparing using compareTo method

public int compareTo(Date date)

The java.util.Date.compareTo(Date date) method compares two Dates. compareTo method returns 0 if this date is equal to argument date, positive value (>0) if this date is after the argument date and negative value (<0) if this date is before the argument date.

Example Continue reading

August 5 2015

Java date difference in days

Java date difference in days

Convert the dates into milliseconds using the getTime() method and calculate the difference (subtract) between them.

long diffMs = date2.getTime() – date1.getTime();

diffMs will have the difference between dates in milliseconds. diffMs can be converted to days manually or using java.util.concurrent.TimeUnit

long diffDays = diffMs / (24 * 60 * 60 * 1000);

or

long diffDays = TimeUnit.DAYS.convert(diffMs, TimeUnit.MILLISECONDS)) Continue reading

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