0

I have a checkbox component with a <f:attribute> and a <p:ajax listener>.

<h:selectManyCheckbox ...>
  <p:ajax listener="#{locationHandler.setChangedSOI}" />
  <f:attribute name="Dummy" value="test" />
  ...
</h:selectManyCheckbox>

I tried to get the <f:attribute> value of test inside the listener method as below:

public void setChangedSOI() throws Exception {
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, String> map = context.getExternalContext().getRequestParameterMap();
    String r1 = map.get("Dummy");
    System.out.println(r1);
}

However, it printed null. How can I get it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
pkn1230
  • 103
  • 1
  • 3
  • 15

1 Answers1

4

Component attributes are not passed as HTTP request parameters. Component attributes are set as .. uh, component attributes. I.e. they are stored in UIComponent#getAttributes(). You can grab them through that map.

Now the right question is obviously how to get the desired UIComponent inside the ajax listener method. There are 2 ways for this:

  1. Specify the AjaxBehaviorEvent argument. It offers a getComponent() method for the very purpose.

    public void setChangedSOI(AjaxBehaviorEvent event) {
        UIComponent component = event.getComponent();
        String dummy = component.getAttributes().get("Dummy");
        // ...
    }
    
  2. Use the UIComponent#getCurrentComponent() helper method.

    public void setChangedSOI() {
        UIComponent component = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());
        String dummy = component.getAttributes().get("Dummy");
        // ...
    }
    
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi BalusC, on using your first option i get the following error, Jun 25, 2015 1:26:09 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet Spring MVC Dispatcher Servlet threw exception javax.el.MethodNotFoundException: /WEB-INF/certificates/locationList.xhtml @162,118 listener="#{locationHandler.setChangedSOI}": Method not found: com.csc.exceed.certificate.web.LocationHandler@1ecd6fb.setChangedSOI() at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:109) – pkn1230 Jun 25 '15 at 08:00
  • Is there something that we can't call a method with AjaxBehaviourEvent as parameter using primefaces? – pkn1230 Jun 25 '15 at 08:02
  • What the heck is a Spring MVC dispatcher servlet doing in a JSF request? Do you understand/know what you're doing? http://stackoverflow.com/questions/18744910/using-jsf-as-view-technology-of-spring-mvc/ – BalusC Jun 25 '15 at 08:03
  • I have done such a thing previously also using in place of . It's working there. – pkn1230 Jun 25 '15 at 08:08