Validation is very important in any web framework. In struts 1.1 we have validation framework with readymade scripts for validation of email,credit card, phone number etc. It is very difficult to write our own regular expression for such validations.
Similarly, Struts2 also has very good validation support. In JSF 2 it looks that we do not have much ready made validation support. I may be wrong. I find there are three standard JSF validators tags in JSF 2.
LengthValidator | <f:validateLength> | maximum, minimum |
DoubleRangeValidator | <f:validateDoubleRange> | maximum, minimum |
LongRangeValidator | <f:validateLongRange> | maximum, minimum |
<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<body bgcolor="green" text="white">
<f:view>
<h:form id="form1">
<h:outputText value="name"/>
<h:inputText id="name" value="#{backer.name}" >
<f:validateLength minimum="3" maximum="10"/>
</h:inputText>
<h:message for="name"/>
<br/>
<h:outputText value="place"/>
<h:inputText id="place" value="#{backer.place}" required="true" />
<h:message for="place"/>
<br/>
<h:outputText value="result"/>
<h:inputText id="text3" value="#{backer.result}" /><br/>
<h:commandButton id="button1"
action="#{backer.echo}"
value="click"/>
<br/>
</h:form>
</f:view>
</body>
</html>
********************************************************
Note the color code. I am using red color for the backing bean. Green color for backing bean attributes. rose color for backing bean method or function.
--------------------------------------------------------------------------------
<f:message> is placed out side but we indicate the control id to which it refers.
backer.java
--------------
package mypack;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.*;
@ManagedBean
@SessionScoped
public class backer
{
String name;
String place;
String result;
public void setName(String a){ name=a; }
public void setPlace(String a){ place=a; }
public void setResult(String a){result=a;}
public String getName(){return name;}
public String getPlace(){return place;}
public String getResult(){return result;}
//------------------------------------------
public String echo()
{
String s1=getName();
String s2=getPlace();
echobean bean1 = new echobean();
String r=bean1.echothis(s1,s2);
setResult(r);
return "success";
}
}
-----------------------------------------------------------------------
In a future blog post I will explore more advanced validation.