Working with implicit and explicit conversions

By implicit conversions, we understand all the conversions that JSF will accomplish automatically, without the presence of an explicit converter (in other words, if you don't specify a converter, JSF will pick one for you). Actually, JSF uses implicit conversion when you map a component's value to a managed bean property of a Java primitive type or of BigInteger and BigDecimal objects.

In this recipe, we will see an example of an implicit and an explicit conversion. Anyway, don't forget that explicit conversion provides greater control over the conversion.

Getting ready

We developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.

How to do it...

Our recipe is based on an imaginary situation where the user should insert their age into a simple JSF form consisting of a text field and a submit button. The submitted age will be implicitly converted and displayed on another simple JSF page. The following is the JSF form (the highlighted code maps the text field's value to the userAge managed bean property of a Java integer type):

<h:form id="AgeForm">
<h:inputText id="userAgeID" required="true"
value="#{userBean.userAge}">
</h:inputText>
<h:message showSummary="true"
showDetail="false" for="userAgeID"
style="color: red; text-decoration:overline"/>
<br />
<h:commandButton id="submit" action="response?faces-
redirect=true" value="Submit Age"/>
</h:form>

Note

The preceding code snippet makes uses of the new JSF 2 implicit navigation style. The {page_name}?faces-redirect=true request parameter indicates to JSF to navigate to the {page_name}. There is more about JSF 2 navigation in Chapter 11, JSF 2.0 Features.

The userAge is mapped into a managed bean as shown next:

package users;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class UserBean {
private int userAge;
public int getUserAge(){
return this.userAge;
}
public void setUserAge(int userAge){
this.userAge=userAge;
}
}

As the userAge is a Java integer, JSF will automatically convert the inserted age to this type (notice that we did not indicate any conversion in the previous code). This is called an implicit conversion. In the case that the inserted age is not an integer, this will be reflected by an error message exposed by the h:message component.

Now, speaking of explicit conversion we can enforce the previous situation by using the UIComponent converter attribute or f:converter tag nested within a UIComponent. The modifications are reflected in the next two lines:

<!-- explicit conversion using the UIComponent converter attribute -->
<h:inputText id="userAgeID" required="true"
value="#{userBean.userAge}"
converter="javax.faces.Integer">
</h:inputText>
<!-- converter tag nested within a UIComponent -->
<h:inputText id="userAgeID" required="true"
value="#{userBean.userAge}">
<f:converter converterId="javax.faces.Integer"/>
</h:inputText>

How it works...

There is no trick here! In the case of implicit conversion, JSF tries to identify which is the appropriate converter to be applied. Obviously, for explicit conversion, JSF tries to apply the indicated converter. When conversion fails, the form is redisplayed and an exception message is fired. Otherwise, the application follows its normal flow.

There's more...

You can mix explicit and implicit conversion over the same managed bean property, but, in this case, you should keep in mind the Java cast rules. For example, if you try to explicitly force an integer to a Byte type you will get an error, as java.lang.Integer type can't be cast to java.lang.Byte type, while a java.lang.Integer can be cast to java.lang.Double.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and is named: Working_with_implicit_and_explicit_conversion.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset