Creating Custom JSF Alert Messages component

In web applications,The Jsf frame work didn’t provide the alert functionality for the h:messages tag. So to provide this functionality there are two ways we can provide the messages in the JavaScript Alert.

1. Write a simple methode(By using getResponse from current context),

2. Custom component

I always preferre the second way, because the custom component is the best and we can maintain easly. we can observe the other more befits at the end of this post. So first I will show how to write a custom comonent.

Before writing the custom component I want to ask two simple Questions,

Why we need to go for the Custom component?

When we need to write the Custom component?

Steps for writing Custome component:

* Write a [Name].tld(Tag Library Description)

* Java implementation for the properties

* Faces Configuration

* Renderer class for the custom component

* Java class to represent the Tag.

Download the Netbeans ready made Project for jsf-components

Please follow the Steps to rock the target manually….

1. Create a components pakage

2a) Right click Source Packages

2b) Select ‘New’
2c) Select ‘Other’
2d) Select ‘Java’
2e) Select ‘Java Package’
2f) Click ‘Next’
2g) Enter components as the package name

2h)Click ‘Finish’

3. Create the tag library descriptor

3a) Right click Source Packages

4b) Select ‘New’
4c) Select ‘Other’
4d) Select ‘Web’
4e) Select ‘Tag Library Descriptor’
4f) Click ‘Next’
4g) Enter the following information:

  • TLD name: jsf-components
  • URI: http://jsf-components
  • Prefix: jc
4h) Click ‘Finish’
5. Add the javaee.jar from the GlassFish project (%GLASSFISH%/lib) to the library dependencies of the project.
6. Create the Java class for the UI component, AlertMessage subclassing the UIMessagecomponent.
AlertMessage.java
package components;
import javax.faces.component.UIMessages;
public class AlertMessage extends UIMessages {
private static final String MSG_COMPONENT_TYPE = “components.alertmessage”;
private static final String MSG_COMPONENT_FAMILY = “javax.faces.Message”;
private String escape;
@Override
public String getFamily() {
return MSG_COMPONENT_FAMILY;
}
@Override
public String getRendererType() {
return MSG_COMPONENT_TYPE;
}
}
7. Create the Java class for the tag, AlertMessageTag subclassing the UIComponentELTagtag.
AlertMessageTag.java
package components;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentELTag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
public class AlertMessageTag extends UIComponentELTag {
private ValueExpression escapeParam;
private static final String MSG_COMPONENT_TYPE = “components.alertmessage”;
private static final String MSG_RENDERER_TYPE = “components.alertmessage”;
@Override
public String getComponentType() {
return MSG_COMPONENT_TYPE;
}
@Override
public String getRendererType() {
return MSG_RENDERER_TYPE;
}
@Override
protected void setProperties(UIComponent component) {
super.setProperties(component);
}
@Override
public int doEndTag() throws JspException {
return super.doEndTag();
}
@Override
public int doStartTag() throws JspException {
return super.doStartTag();
}
@Override
public void setPageContext(PageContext ctx) {
super.setPageContext(ctx);
}
@Override
public void setParent(Tag tag) {
super.setParent(tag);
}
}
9. Create the Java class for rendering the tag, AlertMessageRenderer subclassing the JSF
AlertMessageRenderer.java
package components;
import java.io.IOException;
import java.util.Iterator;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;
import org.apache.commons.lang.StringEscapeUtils;
public class AlertMessageRenderer extends Renderer {
@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
Iterator iter = context.getMessages();
if (iter.hasNext()) {
ResponseWriter writer = context.getResponseWriter();
// Construct one big string of all messages
StringBuffer message = new StringBuffer();
while (iter.hasNext()) {
FacesMessage msg = (FacesMessage) iter.next();
if (message.length() > 0) {
// Separate each message with the JavaScript escape code
// for newline
message.append(”\n”);
}
message.append(msg.getSummary());
}
// Escape the constructed string to be outputable as a JavaScript
String displayMessage = StringEscapeUtils.escapeJavaScript(message.toString());
writer.flush();
writer.write(”<script type=’text/javascript’>alert(’”+displayMessage+”‘);</script>”);
}
}
@Override
public void decode(FacesContext ctx, UIComponent component) {
if (ctx == null || component == null) {
throw new NullPointerException();
}
}
}
10. Update the tag library descriptor to reflect the created component:
<?xml version=”1.0″ encoding=”UTF-8″?>
<taglib version=”2.0″ xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd”>
<tlib-version>1.0</tlib-version>
<short-name>jc</short-name>
<uri>http://jsf-components</uri>
<tag>
<name>AlertMessage</name>
<tag-class>components.AlertMessageTag</tag-class>
<body-content>JSP</body-content>
</tag>
</taglib>
11. Create the faces configuration (META-INF/faces-config.xml) for the component:
<?xml version=”1.0″ encoding=”UTF-8″?>
<faces-config version=”1.2″ xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd”>
<component>
<component-type>components.alertmessage</component-type>
<component-class>components.AlertMessage</component-class>
</component>
<render-kit>
<renderer>
<description>Renderer for the alert message component.</description>
<component-family>javax.faces.Message</component-family>
<renderer-type>components.alertmessage</renderer-type>
<renderer-class>components.AlertMessageRenderer</renderer-class>
</renderer>
</render-kit>
</faces-config>
Now coding part is over, just build the project will get the dist with jsf-components.jar file, now you can add this jar into any other jsf project and you can use the AlertMessages tag.
To use this component, we have to add these two tag in the jsf project’s jsp file
<%@taglib prefix=“jc” uri=“http://jsf-components” %> at the top of the jsp file…
<jc:AlertMessage /> inside the form
Download:
The above commons-lang jar file and add it to the jsf project library, now the jsf project you will get the desired functionality
This is jsf-components.jar for alert message, add this into your jsf project and follow the above procedure(in bold text)
So this is the use of custom component, we can make a jar file for that and we can add it for any JSF application and we can use it as the third party library….

Digg