Validation of a field bound to an Integer variable in the bean

In my application, in the home page there's an add profile command button. The action method invoked on this command button

puts the bean to the session scope.

<h:commandButton action="#{manager.add}"/>

public String add()

{

MyBean item = new MyBean();

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("Profile",item);

return "addPage";

}

MyBean is a simple bean containing the getter and setter for the

age property.

public class MyBean {

private Integer age = new Integer(0);

public void setAge(Integer age){

this.age = age;

}

public Integer getAge(){

return age;

}

On the click of the add command button it navigates to a page that has a field that should accept only numbers. How do i validate this

<h:inputText id="age" value="#{Profile.age}">

In the managed bean (Manager.java) in the add action method

public String add{

FacesContext context = FacesContext.getCurrentInstance();

context.getExternalContext().getSessionMap("Profile");

//save it to the db

}

The age field is bound to an Integer. If I enter alphabets, it will throw

NumberFormatException. Where should I catch it?

How do I solve this problem?

[1339 byte] By [sreedevi2006a] at [2007-10-2 21:34:59]
# 1
JSF has automated Convertor for Integer type. It`ll automaticaly converts the string to an integer. If the convertion fails it`ll add error message to the faces context. You could display that message using <h:messages>
amitteva at 2007-7-14 0:48:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

This is not happening. If i enter 15sd for the age field, on the click of the add button it doesn't display any error message. It just stays in the same page. Also I have other fields like address, phone no. I have provided server-side validations(in action method) for these including age. But If I enter a wrong input for age none of the validations occur. age field is bound to an Integer object.

sreedevi2006a at 2007-7-14 0:48:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Are you sure you have a h:messages on your page?
pringia at 2007-7-14 0:48:55 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Yes I have <h:messages> on the page
sreedevi2006a at 2007-7-14 0:48:55 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...