Home >> Miscellaneous
JSF based form validation for input text
Extending an already hosted example on JSF, at JSF Example
I am going to discuss more on form text field validation
using JSF (version 1.0) as technology.
Software environment I have used for this example, so far as follows:
1. JDK 5.0.x
2. Tomcat 5.5.x
3. Eclipse 3.2
4. Apache Tomcat 6.0.18
5. List of JAR files used for this example as as follows:
5.1. commons-beanutils.jar
5.2. commons-collections.jar
5.3. commons-digester.jar
5.4. commons-logging.jar
5.5. jsf-api.jar
5.6. jsf-impl.jar
5.7. jstl.jar
5.8. standard.jar
The JSF form has two input text and secret fields, and we are
going to discuss more on how to do input text field validation
and showing error messages on screen.
index.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="sampleHtml" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="sampleCore" %>
<html>
<body>
<h4>Enrollment form:</h4>
<sampleCore:view>
<sampleHtml:messages layout="table"/>
<sampleHtml:form>
<table>
<tr><td>Name :</td><td><sampleHtml:inputText value="#{SampleMGMTBean.txtName}" /></td></tr>
<tr><td>Pass :</td><td><sampleHtml:inputSecret value="#{SampleMGMTBean.txtPass}"/></td></tr>
<tr><td><sampleHtml:commandButton value="Submit" action="#{SampleMGMTBean.submitButton}"/></td>
<td><sampleHtml:commandButton value="Cancel" action="#{SampleMGMTBean.cancelButton}"/></td></tr>
</table>
</sampleHtml:form>
</sampleCore:view>
</body>
</html>
|
ButtonEventCapture.java
package sample;
/**
* This code is provided on "AS IS" basis
* @Author: Amit
* @Contact: http://www.techienjoy.com
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
public class ButtonEventCapture
{
String txtName;
String txtPass;
public void setTxtName(String txtName) {
this.txtName = txtName;
}
public String getTxtName() {
return txtName;
}
public void setTxtPass(String txtPass) {
this.txtPass = txtPass;
}
public String getTxtPass() {
return txtPass;
}
public String submitButton() {
System.out.println("Submit button is clicked...");
System.out.println("Name: "+txtName);
System.out.println("Pass: "+txtPass);
if(txtName.trim().equalsIgnoreCase("")) {
FacesContext.getCurrentInstance().
addMessage("name_blank",
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Please enter name",""));
}
if(txtPass.trim().equalsIgnoreCase("")) {
FacesContext.getCurrentInstance().
addMessage("pass_blank",
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Please enter password",""));
}
if(txtName.length() >8) {
FacesContext.getCurrentInstance()
.addMessage("name_too_large",
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Name should be lesser than 8 characters",""));
}
Iterator itr = FacesContext.getCurrentInstance().getMessages();
if(itr.hasNext()) {
FacesMessage msg = (FacesMessage) itr.next();
List list = msg.VALUES;
if(list.size() > 0) {
return null;
}
}
return "preview";
}
public String cancelButton() {
System.out.println("Cancel button is clicked...");
return null;
}
}
|
Please note the text marked as
BOLD above, the way to
add message to FacesContext to be able to show on screen.
using the Tag in index.jsp page.
All other files such as web.xml, faces-config.xml files
can be seen in the earlier
JSF Example
This example can be treated as a very basic steps towards knowing
form validation using JSF, and is debatable.
Some of the screens captured with many test cases such as
1. submit button is pressed by leaving name and pass text fields
as blank.
2. submit button is clicked by entering name as a text with
more than 8 characters length and leaving pass as blank.
3. submit button is clicked by entering appropriate values
for the name and pass text fields and then seeing preview
screen as shown below:

In order to run this example using browser, the URL is
http://localhost:8080/JSF-Sample/index.
jsf
Here the web address is localhost, web application name is
JSF-Sample, If you try to use index.jsp or / at the end of this URL,
you may encounter error on page, as mentioned in the web.xml
file, the servlet URL mapping refers to
*.jsf
If you like to share your comment/suggestions/feedback relating to this Page,
you can do so by droping us an email at
usingframeworks @ gmail . com
with the subject line mentioning URL for this Page (i.e,
/JSF-Example-Validation.php) or use this
LINK.
As per this website's privacy policy, we never disclose your email id,
though we shall post your comments/suggestions/feedback with
your name (optional) and date on this Page. If you don't want your
comments/suggestions/feedback to be shared in this Page, please
mention so in your email to us. Thank you very much.....
If anything missed out , please let me know at
techienjoy at yahoo . com
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Android ListView Example : |
Example on Android List View
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
|
|
|
| Android Tab View Example : |
Example on Android Tab View
explained with a very simple scenario
and appropriate screens captured and shown.
|
|
|
|
| Android ListView Example : |
Example on Android ListView and
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
| Android Examples : |
List of ANDROid examples
with source code and output
screens captured and shown.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Android ListView Example : |
Example on Android ListView
explained with a very simple scenario
whereby showing folder and files with
structure and appropriate screens
captured and shown.
|
|
| Android Gallery Example : |
Example on Android Gallery View
explained with a very simple scenario
and appropriate screens captured and shown.
|
|
|
|
|
| Google GWT Example : |
Example using GWT and some design patterns and various
ways of implementing this example.
|
|
| Android Sensors Example : |
Example on Android Sensors Listed and
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|