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?

