dev@jsftemplating.java.net

Re: HandlerContext

From: Ken Paulsen <Ken.Paulsen_at_Sun.COM>
Date: Fri, 05 Sep 2008 13:38:21 -0700

The @Handler, @HandlerInput, and @HandlerOutput annotations are used to define the meta-data describing the handler method.  This meta-data is used to locate and validate the handler when used from a .jsf file.

The HandlerContext is an environment (context) object which is passed to the handler which provides access to the actual input/output values specified in the .jsf file as well as many other things such the FacesContext, the meta-data defined by .jsf file, the event type, etc.  The HandlerContext is most commonly used to get/set I/O values in the handler code.

Does this make sense?  Here's a simple example handler and it's cooresponding .jsf page:

    @Handler(id="setAttribute",
        input={
            @HandlerInput(name="key", type=String.class, required=true),
            @HandlerInput(name="value", required=true)})
    public static void setAttribute(HandlerContext context) {
        String key = (String) context.getInputValue("key");
        Object value = context.getInputValue("value");
        context.getFacesContext().getExternalContext().
            getRequestMap().put(key, value);
    }

    <staticText value="#{test}">
        <!beforeCreate
           setAttribute(key="test" value="Hello!");
        />
    </staticText>

The first line of the method uses the HandlerContext (context) to get the value of "key" from the .jsf file ("test" in this example).  The 2nd line gets the "value" value, which is "Hello!".  It then sets this value in the request attribute map.  The EL in the static text will look in the request attribute map for "test", find it and will use its value "Hello!" for the string to display for this StaticText component.

Good luck!

Ken

SUSHANT TANEJA wrote:
Hello,

From what I understand is Handler is used to handle components and Input and Output are controlled with HandlerInput and HandlerOuput.

What does HandlerContext do??

Regards,
Sushant Taneja


Connect with friends all over the world. Get Yahoo! India Messenger.