Steps for customizing Error Messages In JSF Portlets


Validation is an important part of any Web/Portal application and should be properly implemented from client's business perspective. JSF provides a powerful validation framework and works very well with portal applications but it needs some customization to display custom validation messages to the user and not the default one which is "Validation Error: Value is required".

Below are the validation approaches that can be generally implemented and also using JSF framework.

Manual Validation

- Use string properties for bean
- Do Validation in setter methods or action controller
- Return null to redisplay form
- Create custom error messages

Implicit Automatic Validation

- add required attribute in JSF tags
- System redisplays form if there is a conversion error
- Use h:message to display field specific error message

Explicit Automatic Validation

- Use f:validateLength, f:validateDoubleRange, f:validateLongRange
- System redisplays form if failure, use h:message to display the message

Custom Validators

- Extend Validator
- Override Validate method
- Register validator in faces-config.xml

In real time applications it is often desired to have error messages which are descriptive. Thus we would desire to customize the default error messages provided by JSF. Below steps describes how to customize the error messages.

Steps for Customizing the Error Message

1. Construct the JSP
2. Register a PhaseListener
3. Implement PhaseListener
4. Specifying the custom messages in the properties file
5. Declaring the properties file in the faces-config.xml

Here we consider a page with a simple input text field and using the required = “true” attribute to validate the filed. In general we would get the error message as "Validation Error: Value is required".

In the below example we would customize this error message

Construct the JSP
<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding = "ISO-8859-1" session="false"%>
<%@taglib uri="" prefix="f"%>
<%@taglib uri="" prefix="h"%>
<h:message style=”color : red” />
<h:outputText value = “enter your name” />
<h:inputText id="name" value="#{}" required="true">
<h:commandLink action="#{}">
<h:outputText id="save" value="save" />

Register PhaseListener
<phase-listener> com.jsf.listener.MessageListner</phase-listener>

Implement PhaseListener

public class MessageListner implements PhaseListener {
public PhaseId getPhaseId() {

public void beforePhase(PhaseEvent e) {
FacesContext facesContext = e.getFacesContext();
UIViewRoot root = facesContext.getViewRoot();
String messageBundle = "";
Locale locale = root.getLocale();
ResourceBundle resourceBundle = ResourceBundle.getBundle(messageBundle,locale);
Iterator clientIdIterator = facesContext.getClientIdsWithMessages();
String fieldName;
while(clientIdIterator.hasNext()) {
String clientId = (String);
fieldName = clientId.substring(clientId.lastIndexOf(":",clientId.length())+1);
Iterator messageIterator = facesContext.getMessages(clientId);
while(messageIterator.hasNext()) {
FacesMessage facesMessage = (FacesMessage);
String detail = facesMessage.getDetail();
String errorMsg = "";
if(detail.equals("Validation Error: Value is required."))
errorMsg = resourceBundle.getString("error."+fieldName+"_"+"NOTNULL");
public void afterPhase(PhaseEvent e) {
Specifying the custom messages in the properties file

Error.name_NOTNULL = name is required

Declaring the properties file in the faces-config.xml


Finally on running this application and making the system commit the error we would get the error message as “name is required”.

Thus we can customize the default error messages as per our requirement.

No comments:

Post a Comment

Recent Posts

Popular Posts

© 2011-2019 Web Portal Club