Enabling and Disabling Button In JSF

In this tutorial enabling and disabling a button on different conditions has been explained. A button can be disabled setting "disabled" attribute of the commandButton tag to "true". We can set this attribute while writing the tag or by setting it according to different conditions. In this example we have used beans to set this attribute to "true" or "false" according to the type of the user. The first page that comes to the user is login page where the user puts login ID and password. If ID and Password both are "admin" then the next page will be containing buttons "Employee Area" and "Admin Area" enabled. If both are "emp" then in the next page only one "Employee Area" button will be enabled and "Admin Area" button will be disabled. If the ID and Password doesn't match with any of these then no button will be enabled.

Code for all the pages have been given below :

login.jsp :

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>






Enable Disable Button





















required="true"/>
required="true"/>
action="#{EnableDisable.CheckValidUser}" />







EnableDisable.jsp :

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>






Enable Disable Button





















style="color:red;" rendered="#{EnableDisable.render3}" />
style="color:Green;" rendered="#{EnableDisable.render4}" />
Employee Area"
disabled="#{EnableDisable.disable1}" />
Admin Area"
disabled="#{EnableDisable.disable2}" />

EnableDisable.java (JavaBean) :

package roseindia;

public class EnableDisable{
String loginname;
String password;
boolean disable1;
boolean disable2;
boolean render3;
boolean render4;

public EnableDisable(){
}
public String getLoginname(){
return loginname;
}
public void setLoginname(String loginname){
this.loginname = loginname;
}

public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}

public boolean getDisable1(){
return disable1;
}
public void setDisable1(boolean disable1){
this.disable1 = disable1;
}

public boolean getDisable2(){
return disable2;
}
public void setDisable2(boolean disable2){
this.disable2 = disable2;
}

public boolean getRender3(){
return render3;
}
public void setRender3(boolean render3){
this.render3 = render3;
}

public boolean getRender4(){
return render4;
}
public void setRender4(boolean render4){
this.render4 = render4;
}

public String CheckValidUser(){

if(loginname.equals("admin")==true && password.equals("admin")==true){
disable1=false;
disable2=false;
render3=false;
render4=true;
}
else{
if(loginname.equals("emp")==true && password.equals("emp")==true){
disable1=false;
disable2=true;
render3=false;
render4=true;
}
else{
disable1=true;
disable2=true;
render3=true;
render4=false;
}
}

return "success";

}
}

Rendered Output : This is the first page for the user.

If the user fills Login ID as "admin" and Password as "admin":

Then user is welcome and all buttons are enabled.

If the user fills Login ID as "emp" and Password as "emp":

Then again user is welcome but "Admin Area" button is disabled.

If the user is not administrator or employee then there is a message indicating no permission to proceed further and both buttons are disabled.

faces-config.xml :


"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">




EnableDisable
roseindia.EnableDisable
request


/pages/login.jsp

#{EnableDisable.CheckValidUser}
success
/pages/EnableDisable.jsp


Download Example Here :

0 comments:

Post a Comment