Wednesday, August 25, 2010

PopUp AlertMessagebox for JSF

In my last post i have written Javascript function for email validation. Today i will be discussing  PopUp AlertMessagebox for JSF
When we develop websites we often have to display to user some alerts if user makes some mistakes
In such cases we can use jsf-components.
Steps to implementation
1) Download  jsf-components.jar..
2)Include this taglib <%@taglib prefix="jc" uri="http://jsf-components" %>
and also add<jc:AlertMessage /> tag after <h:form>tag
3)Add below method .If you are having a java class for common methods
it's better to add this method there for reuseibility
/**
* Add a Message to the Faces Context
* @param msg
*/
public static void showMessage(String msg) {
msg = "" + msg;
FacesMessage fm = new FacesMessage(msg);
FacesContext.getCurrentInstance().addMessage(msg, fm);
}
4)Use it
configure managebean in faces-config.xml
<managed-bean>
<managed-bean-name>userlogin</managed-bean-name>
<managed-bean-class>client.gui.main.Login</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

In JSP paste the below code
<h:outputLabel id="lb_USR_NAME" value="UserName:" styleClass="label"/>
<h:inputText id="tf_USR_NAME" autocomplete="off" value="#{userlogin.tf_USR_NAME}" />
<h:outputLabel id="lb_PASS" value="Password:" styleClass="label"/>
<h:inputText id="tf_PASS" autocomplete="off" value="#{userlogin.tf_PASS}" />
<h:commandButton id="cb_login" value="Login" action="#{userlogin.submit}"/>

In Java Class paste the below code
package client.gui.main;
/**
*
* @author Subin Suresh
*/
public class Login{
public String submit(){
String status = null;
String userName = (String) getTf_USR_NAME().toString();
String passWord = (String) getTf_PASS().toString();
if (((userName.equals("subin"))) && ((passWord.equals("subin")))) {
showMessage("UserName and Password Does not Matches .");
status = "failure";
}else{
status = "success";
}
return status; 
}
}

Download  jsf-components.jar..

JSF Related topics: JCaptcha in JSF, Integrating Richfaces with JSF,Getting client and server sessionId in JSF and more.....

1 comment: