简体   繁体   English

jsf以编程方式进行ajax函数调用

[英]jsf programmatically ajax function call

I am looking for a possibility to write programmatically ajax call on every element. 我正在寻找一种在每个元素上以编程方式编写ajax调用的可能性。

I have to ways, to build the UI Components 我必须设法构建UI组件

first - panelGroup binding- ) 首先-panelGroup绑定-)

    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. 我可以看到,Ajax调用已链接到该组件,但是未调用updateProduct()函数。

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 ? 如何在这里的每个select元素上添加ajax调用? 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... 这是一个非常简单的示例,在这里我没有首先通过循环构建很多选择元素,我需要完成这项工作...

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. 您需要给所有以编程方式创建的输入和命令组件一个固定的ID,以便JSF可以在请求参数映射中找到所需的提交信息。 Otherwise they end up getting an autogenerated ID which is different during postback. 否则,他们最终将获得一个自动生成的ID,该ID在回发期间会有所不同。

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. 我确信Java是错误的工具,无法在视图中声明组件。 JSF already ships with Facelets out the box which allows declaring components in a much easier and cleaner way by XML means. JSF已经附带了Facelets,可以通过XML方式以更轻松,更简洁的方式声明组件。 If you intend to build the view dynamically based on some preconditions, look at JSTL. 如果打算基于某些前提条件动态构建视图,请查看JSTL。 See also among others How to make a grid of JSF composite component? 另请参见其他内容如何制作JSF复合组件的网格? and JSTL in JSF2 Facelets... makes sense? JSF2 Facelets中的JSTL ...有意义吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM