• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cannot use f:valueChangeListener or f:actionListener with custom component?

 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, the J2EE tutorial says:


You do not need to create a custom component in these cases:
  .....
   
You need to register event listeners on components. You can either register event listeners on components using the f:valueChangeListener and f:actionListener tags, or you can point at an event-processing method on a managed bean using the component’s actionListener or valueChangeListener attributes. See Implementing an Event Listener and Writing Managed Bean Methods for more information.



I think we can still use event listeners with custom components. In the Core JSF book, chapter 11, the example shows this:


<corejsf: spinner ...>
  <f:valueChangeListener .../>
</corejsf:spinner>


The spinner is a custom component that extends UIInput class.
 
Himai Minh
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
To answer my own question, I modified an example from memorynotfound at https://memorynotfound.com/jsf-custom-input-facescomponent-example/.
I found out that I cannot add an action listener to the custom component, UIInput.
Because the parent source is not an action source even thought the custom component has a command button.


@FacesComponent(value = "com.memorynotfound.jsf.CustomInput")
public class CustomInput extends UIInput {

 ...
   @Override
   public void encodeEnd(FacesContext context) throws IOException {
       String clientId = getClientId(context);
       char sep = UINamingContainer.getSeparatorChar(context);
       encodeInputField(context, clientId + sep + "inputfield");
       encodeSubmitButton(context, clientId + sep + "submit");
       encodeOutputField(context);
   }

   private void encodeInputField(FacesContext context, String clientId) throws IOException {
       ResponseWriter writer = context.getResponseWriter();
       writer.startElement("input", this);
       writer.writeAttribute("type", "text", null);
       writer.writeAttribute("name", clientId, "clientId");
       writer.writeAttribute("size", "30", null);
       Object value = getValue();
       if (value != null) {
           writer.writeAttribute("value", value.toString(), "value");
       }
       writer.writeAttribute("size", "6", null);
       writer.endElement("input");
   }

   private void encodeSubmitButton(FacesContext context, String clientId) throws IOException {
       ResponseWriter writer = context.getResponseWriter();
       writer.startElement("input", this);
       writer.writeAttribute("type", "Submit", null);
       writer.writeAttribute("name", clientId, "clientId");
       writer.writeAttribute("value", "Click Me!", null);
       writer.endElement("input");
   }

...

}




I did this:


<custom:custom-input>
   <f:actionListener type="com.memorynotfound.jsf.MyActionListener"/>
</custom:custom-input>


I got an error saying :<f:actionListener> Parent is not of type ActionSource, type is: com.memorynotfound.jsf.CustomInput@5b8c3387

So, that proves that custom component does not work with listener like the JEE tutorial says.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic