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
Register PhaseListener
Implement PhaseListener
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.
Below are the validation approaches that can be generally implemented and also using JSF framework.
Manual Validation
![]() |
Error |
- 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="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
<h:form>
<h:message style=”color : red” />
<h:outputText value = “enter your name” />
<h:inputText id="name" value="#{}" required="true">
</h:inputText>
<h:commandLink action="#{}">
<h:outputText id="save" value="save" />
</h:commandLink>
</h:form>
</f:view>
Register PhaseListener
<faces-config>
<lifecycle>
<phase-listener> com.jsf.listener.MessageListner</phase-listener>
</lifecycle>
</faces-config>
Implement PhaseListener
public class MessageListner implements PhaseListener {Specifying the custom messages in the properties file
public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
public void beforePhase(PhaseEvent e) {
FacesContext facesContext = e.getFacesContext();
UIViewRoot root = facesContext.getViewRoot();
String messageBundle = "com.jsf.nl.MyJsfResource";
Locale locale = root.getLocale();
ResourceBundle resourceBundle = ResourceBundle.getBundle(messageBundle,locale);
Iterator clientIdIterator = facesContext.getClientIdsWithMessages();
String fieldName;
while(clientIdIterator.hasNext()) {
String clientId = (String)clientIdIterator.next();
fieldName = clientId.substring(clientId.lastIndexOf(":",clientId.length())+1);
Iterator messageIterator = facesContext.getMessages(clientId);
while(messageIterator.hasNext()) {
FacesMessage facesMessage = (FacesMessage)messageIterator.next();
String detail = facesMessage.getDetail();
String errorMsg = "";
if(detail.equals("Validation Error: Value is required."))
{
errorMsg = resourceBundle.getString("error."+fieldName+"_"+"NOTNULL");
}
if(errorMsg!=null){
facesMessage.setDetail(errorMsg);
}
}
}
}
public void afterPhase(PhaseEvent e) {
}
}
# ERROR MESSAGES
Error.name_NOTNULL = name is required
Declaring the properties file in the faces-config.xml
<faces-config><application>
<message-bundle>com.jsf.nl.MyJsfResource</message-bundle>
</application>
</faces-config>
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