CalcActionListener.java
package
org.apache.myfaces.examples.example1;
import
javax.faces.component.UIComponent;
import
javax.faces.context.FacesContext;
import
javax.faces.el.VariableResolver;
import
javax.faces.event.AbortProcessingException;
import
javax.faces.event.ActionEvent;
import
javax.faces.event.ActionListener;
public class
CalcActionListener
implements ActionListener
{
public void
processAction(ActionEvent event) throws AbortProcessingException
{
FacesContext facesContext =
FacesContext.getCurrentInstance();
UIComponent component =
event.getComponent();
VariableResolver vr =
facesContext.getApplication().getVariableResolver();
CalcForm form =
(CalcForm)vr.resolveVariable(facesContext, "calcForm");
if
(component.getId().equals("addButton") ||
component.getId().equals("href1"))
{
form.add();
}
else
{
form.subtract();
}
}
}
CalcForm.java
package org.apache.myfaces.examples.example1;
import
java.io.Serializable;
import
java.math.BigDecimal;
public class CalcForm
implements Serializable
{
private BigDecimal
number1 = new BigDecimal(0);
private BigDecimal
number2 = new BigDecimal(0);
private BigDecimal
result = new BigDecimal(0);
public void add()
{
result = number1.add(number2);
}
public void subtract()
{
result = number1.subtract(number2);
}
//getters
and setters
}
UcaseActionListener.java
package
org.apache.myfaces.examples.example1;
import
javax.faces.component.UIComponent;
import
javax.faces.context.FacesContext;
import
javax.faces.el.VariableResolver;
import
javax.faces.event.AbortProcessingException;
import
javax.faces.event.ActionEvent;
import
javax.faces.event.ActionListener;
public class
UCaseActionListener
implements ActionListener
{
public void
processAction(ActionEvent event) throws AbortProcessingException
{
FacesContext facesContext =
FacesContext.getCurrentInstance();
UIComponent component =
event.getComponent();
VariableResolver vr =
facesContext.getApplication().getVariableResolver();
UCaseForm form =
(UCaseForm)vr.resolveVariable(facesContext, "ucaseForm");
if (component.getId().equals("ucaseButton"))
{
form.uppercase();
}
else
{
form.lowercase();
}
}
}
UcaseForm.java
package
org.apache.myfaces.examples.example1;
import
java.io.Serializable;
public class UCaseForm
implements Serializable
{
private String text = "";
public void uppercase()
{
text = text.toUpperCase();
}
public void lowercase()
{
text = text.toLowerCase();
}
public String getText()
{
return text;
}
public void setText(String
text)
{
this.text = text;
}
/**
* Test method for method binding.
*/
public String
jumpHome()
{
System.out.println("JumpHome
Action was called.");
return "jump_home";
}
}
ValidationController.java
package
org.apache.myfaces.examples.example1;
import
javax.faces.component.UIInput;
import
javax.faces.context.FacesContext;
import
javax.faces.validator.LengthValidator;
import javax.faces.validator.LongRangeValidator;
import
javax.faces.validator.Validator;
public class
ValidationController
{
public String
enableValidation()
{
FacesContext facesContext =
FacesContext.getCurrentInstance();
UIInput number1 = (UIInput)facesContext.getViewRoot().findComponent("form1:number1");
Validator[] validators =
number1.getValidators();
if (validators == null ||
validators.length == 0)
{
number1.addValidator(new
LongRangeValidator(10, 1));
}
UIInput number2 =
(UIInput)facesContext.getViewRoot().findComponent("form1:number2");
validators = number2.getValidators();
if (validators == null ||
validators.length == 0)
{
number2.addValidator(new LongRangeValidator(50,
20));
}
UIInput text =
(UIInput)facesContext.getViewRoot().findComponent("form2:text");
validators = text.getValidators();
if (validators == null ||
validators.length == 0)
{
text.addValidator(new
LengthValidator(7, 3));
}
return "ok";
}
public String
disableValidation()
{
FacesContext facesContext =
FacesContext.getCurrentInstance();
UIInput number1 =
(UIInput)facesContext.getViewRoot().findComponent("form1:number1");
Validator[] validators =
number1.getValidators();
if (validators != null)
{
for (int i = 0; i <
validators.length; i++)
{
Validator validator =
validators[i];
number1.removeValidator(validator);
}
}
UIInput number2 =
(UIInput)facesContext.getViewRoot().findComponent("form1:number2");
validators = number2.getValidators();
if (validators != null)
{
for (int i = 0; i <
validators.length; i++)
{
Validator validator =
validators[i];
number2.removeValidator(validator);
}
}
UIInput text =
(UIInput)facesContext.getViewRoot().findComponent("form2:text");
validators = text.getValidators();
if (validators != null)
{
for (int i = 0; i <
validators.length; i++)
{
Validator validator =
validators[i];
text.removeValidator(validator);
}
}
return "ok";
}
public String
getNumber1ValidationLabel()
{
FacesContext facesContext =
FacesContext.getCurrentInstance();
UIInput number1 = (UIInput)facesContext.getViewRoot().findComponent("form1:number1");
Validator[] validators =
number1.getValidators();
if (validators != null &&
validators.length > 0)
{
long min =
((LongRangeValidator)validators[0]).getMinimum();
long max = ((LongRangeValidator)validators[0]).getMaximum();
return " (" + min + "-" + max + ")";
}
else
{
return "";
}
}
public String
getNumber2ValidationLabel()
{
FacesContext facesContext =
FacesContext.getCurrentInstance();
UIInput number1 =
(UIInput)facesContext.getViewRoot().findComponent("form1:number2");
Validator[] validators =
number1.getValidators();
if (validators != null &&
validators.length > 0)
{
long min =
((LongRangeValidator)validators[0]).getMinimum();
long max =
((LongRangeValidator)validators[0]).getMaximum();
return " (" + min + "-" + max + ")";
}
else
{
return "";
}
}
public String
getTextValidationLabel()
{
FacesContext facesContext =
FacesContext.getCurrentInstance();
UIInput number1 =
(UIInput)facesContext.getViewRoot().findComponent("form2:text");
Validator[] validators =
number1.getValidators();
if (validators != null &&
validators.length > 0)
{
long min =
((LengthValidator)validators[0]).getMinimum();
long max =
((LengthValidator)validators[0]).getMaximum();
return " (" + min + "-" + max + "
chars)";
}
else
{
return "";
}
}
}