1

I am looking for a possibility to write programmatically ajax call on every element.

I have to ways, to build the UI Components

first - panelGroup binding- )

    HtmlSelectOneMenu HSOM = new HtmlSelectOneMenu();       
    UISelectItems items = new UISelectItems();   

    List<SelectItem> comboList = new ArrayList<SelectItem>();         
    comboList.add(new SelectItem(" "));   
    comboList.add(new SelectItem("1"));   
    comboList.add(new SelectItem("2"));   
    comboList.add(new SelectItem("3"));   

    items.setValue(comboList);   
    HSOM.getChildren().add(items);                      
    HSOM.setValueExpression("value", buildValueExpression("#{productDetails.productOptionValue}"));

    AjaxBehavior ajax = new AjaxBehavior();
    ajax.setValueExpression("value", buildValueExpression("#{productDetails.updateProduct()}"));
    HSOM.addClientBehavior("valueChange", ajax);
    HSOM.addValidator(new BeanValidator());
    productOptions.getChildren().add(HSOM);

    private ValueExpression buildValueExpression(String exp) {
    FacesContext facesInstance = FacesContext.getCurrentInstance();
    Application application = facesInstance.getApplication();
    ExpressionFactory expressionFactory = application.getExpressionFactory();
    String expression = exp;
    return expressionFactory.createValueExpression(facesInstance.getELContext(), expression, String.class);
}

I can see, that a Ajax Call is linked to the component, but the updateProduct() function did not get called.

the other possibility to create the dynamic components is)

  public void encodeEnd(FacesContext context) throws IOException {
            System.out.println("Start encoding");    
     ResponseWriter responseWriter = context.getResponseWriter();
     responseWriter.startElement("span", null);
     responseWriter.writeAttribute("id",getClientId(context),"id");
     responseWriter.writeAttribute("name", getClientId(context),"clientId");
     responseWriter.write("Farbe");
     responseWriter.endElement("span");

     responseWriter.startElement("select", null);
     responseWriter.writeAttribute("id",getClientId(context),"id");
     responseWriter.writeAttribute("name", getClientId(context),"clientId");
     responseWriter.writeAttribute("value", "#{artikelDetails.productOptionValue}", "value");
        responseWriter.startElement("option", null);
        responseWriter.write("Gelb");
        responseWriter.endElement("option");     
        responseWriter.startElement("option", null);
        responseWriter.write("Blau");
        responseWriter.endElement("option");
     responseWriter.endElement("select");        
     System.out.println("End encoding");
     }

How to add a ajax call on every select ele here ? And which of both method's do you prefer ?

This is a very simple example, where i do not build lot of select ele via loop first i need to get this work...

Armand
  • 726
  • 11
  • 29
Sence
  • 115
  • 8

1 Answers1

0

You need to give all programmatically created input and command components a fixed ID, so that JSF can find the desired submitted information in the request parameter map. Otherwise they end up getting an autogenerated ID which is different during postback.

In your case, that's thus:

HSOM.setId("someId");

And which of both method's do you prefer ?

None of both. I'm confident that Java is the wrong tool for the purpose of declaring components in the view. JSF already ships with Facelets out the box which allows declaring components in a much easier and cleaner way by XML means. If you intend to build the view dynamically based on some preconditions, look at JSTL. See also among others How to make a grid of JSF composite component? and JSTL in JSF2 Facelets... makes sense?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • But if there are a lot of components to build, based on database informations eg product variations and conditions, i like do build em programmatically. Sure i can use ui:repeat, jstl and other ui "management" components, but i dont agree with that the use of ui components is always cleaner and better. If you have a tree with many leafs, you can define a join function and execute the function automaticly if a leaf has further sub elements. how do it with jstl or ui:repeat ? vg – Sence Jul 16 '13 at 06:36
  • JSTL does not end up in component tree. It runs during building the component tree. Note that `ui:repeat` is not JSTL, `c:forEach` (and `c:if`, etc) is. Click the given links to get enlightenend. – BalusC Jul 16 '13 at 10:09