You are on page 1of 4

Object Oriented Software Engineering

Solution Lab Assignment # 02


Due Date: 18 -03-2021 Section: S Total Marks: 10

Program: BS(SE)
Instructions
I can take either quiz or in class viva for this assignment by calling any one on the white board to solve
any question of the assignment. Copied assignment will be getting zero marks and further action will be
taken as per university policy.

Question: Create a form Student using JSF framework. The form has following text fields First
Name, Last Name, Age, CNIC, Mobile Number, Email I’d. When this form is submitted, the
validator will make sure the “username” text field contains a minimum length of 5, maximum
length of 10 and so on. Apply all the validator on form text fields.
Solution
“f:validateLength” is a JSF string length validator tag, which is used to check the length
of a string. For example,

JSF tag…

<h:inputText id="username" value="#{user.username}">


<f:validateLength minimum="5" maximum="10" />
</h:inputText>

1. Managed Bean
A simple managed bean, with an “userName” property.

package com.mkyong;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable{

String username;

public String getUsername() {


return username;
}

public void setUsername(String username) {


this.username = username;
}

2. View Page
JSF XHTML page, show the use of “f:validateLength” tag to make sure the form’s input
“username” contains a minimum length of 5, maximum length of 10.

<?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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
>
<h:body>

<h1>JSF 2 validateLength example</h1>

<h:form>

<h:panelGrid columns="3">

Enter UserName :

<h:inputText id="username" value="#{user.username}"


size="20" required="true"
label="UserName" >
<f:validateLength minimum="5" maximum="10" />
</h:inputText>

<h:message for="username" style="color:red" />

</h:panelGrid>

<h:commandButton value="Submit" action="result" />

</h:form>
</h:body>
</html>
3. Demo
Minimum length validation failed.

You might also like