Passing around information via JSF FacesContexts

1.9k Views Asked by At

I have a listener that listens for JSF validation failures, and I need to be able to turn off a specific piece of functionality depending on certain contexts.

In my listener I only have the SystemEvent, so this listener isn't component specific, I was wondering if there was any way to pass around any other information, perhaps something like attributes on the FacesContext?...so that later in the validation listener I could check the context for an attribute that I could set in the JSF.

Ie

<f:someContextParam name="turnOff" value="true"/>

then later

boolean turnOff = (Boolean) FacesContext.getCurrentInstance().someWayToGetAttribute("turnOff");

...seems like a shot in the dark, I'm just trying to see if theres any contextual way to pass back information before I rewrite the architecture.

1

There are 1 best solutions below

2
On

You could enclose a <f:attribute> tag inside your input component and then retrieve the attribute value inside the validator via FacesContext.getCurrentInstance().getAttributes().get(attrname);

Syntax for the tag should be <f:attribute name="attrname" value="#{ELexpr}">

Here's a semi complete example validator:

public class NameValidator implements Validator 
{
    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
    {
        Object value = (String)component.getAttributes().get("FIELD_NAME");
        // validate stuff
    }
}

and corresponding jsf

<h:inputText id="name" value="#{registrationManager.name}">
    <f:validator validatorId="nameValidator" />
    <f:attribute name="FIELD_NAME" value="#{registrationManager.numAttempts}"/>
</h:inputText>

Updated Fixed the wrong use of the context to get the attribute to get it from the component instead.