动态组件的JSF Ajax实现:SelectOneRadio

时间:2013-12-18 14:32:08

标签: ajax jsf-2 primefaces runtime

任何人都可以告诉我如何向动态创建的SelectOneRadio添加ajax行为。 要求是当我选择任何一个单选按钮时,我想显示jsf动态创建的字段集。 我尝试了以下。但没有运气!!!!任何人都可以帮忙吗????

AjaxBehavior ajax=(AjaxBehavior) FacesContext.getCurrentInstance().getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID); 
ajax.addAjaxBehaviorListener(new CustomAjax());
ajax.setTransient(true);
radio.addClientBehavior("change",ajax);


public class CustomAjax implements AjaxBehaviorListener{

@Override
public void processAjaxBehavior(AjaxBehaviorEvent arg0)
        throws AbortProcessingException {
    System.out.println("Ajax :"+arg0.getComponent());

}

}

使用 JSF2和primefaces 3.4.1

我尝试了user2251135给出的解决方案....没有运气.. 让我澄清一下这个问题: 我想以编程方式获得以下xhtml功能:

<p:selectOneRadio id="customRadio" value="#{test.panel1}"
                        layout="pageDirection">
<f:selectItem itemLabel="Option 1" itemValue="1" />
<f:selectItem itemLabel="Option 2" itemValue="2" />
<f:selectItem itemLabel="Option 3" itemValue="3" />
<p:ajax update="payment-amount-panel" />
</p:selectOneRadio>
<h:panelGroup id="payment-amount-panel">
<p:fieldset legend="Legend1" rendered="#{test.panel1 == '1'}">
<h:outputText value="Francis Ford Coppola's1 " />
</p:fieldset>
<p:fieldset rendered="#{test.panel1 == '2'}">
<h:outputText value="Francis Ford Coppola's2" />
</p:fieldset>
<p:fieldset rendered="#{test.panel1 == '3'}">
<h:outputText value="Francis Ford Coppola's3" />
</p:fieldset>

</h:panelGroup>

和Bean

@ManagedBean (name="test")
@ViewScoped
public class Test {

private String panel1;

public void setPanel1(String panel1){
this.panel1 = panel1;
}

public String getPanel1(){
return this.panel1;
}
}

首先,感谢user2251135对该问题的回复。

你们中的任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

这是一个更完整的解决方案:

项目依赖关系

<dependencies>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.1.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>3.4.1</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

ManagedBean

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.el.ELContext;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIForm;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.component.behavior.ajax.AjaxBehavior;
import org.primefaces.component.behavior.ajax.AjaxBehaviorListenerImpl;
import org.primefaces.component.selectoneradio.SelectOneRadio;

@ViewScoped
@ManagedBean
public class SampleMBean implements Serializable {

    private String selectedValue;

    private UIForm form;

    public void createRadio() {

        form.getChildren().clear();
        Application application = FacesContext.getCurrentInstance()
            .getApplication();

        AjaxBehavior ajax = new AjaxBehavior();
        ajax.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(
            createActionMethodExpression("#{sampleMBean.processAction}")));
        ajax.setUpdate("@form");

        SelectOneRadio radio = (SelectOneRadio) application.createComponent(SelectOneRadio.COMPONENT_TYPE);
        radio.setId("radioId");
        ValueExpression vExp = createValueExpression("#{sampleMBean.selectedValue}", String.class);
        radio.setValueExpression("value", vExp);
        radio.addClientBehavior("change", ajax);

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

        UISelectItems selectItems = new UISelectItems();
        selectItems.setValue(items);
        radio.getChildren().add(selectItems);

        form.getChildren().add(radio);
    }

    public MethodExpression createActionMethodExpression(String name) {
        FacesContext facesCtx = FacesContext.getCurrentInstance();
        ELContext elContext = facesCtx.getELContext();
        return facesCtx
            .getApplication()
            .getExpressionFactory()
            .createMethodExpression(elContext, name, String.class,
                new Class[]{});
    }

    public ValueExpression createValueExpression(String valueExpression,
        Class<?> valueType) {
        FacesContext context = FacesContext.getCurrentInstance();
        return context
            .getApplication()
            .getExpressionFactory()
            .createValueExpression(context.getELContext(), valueExpression,
                valueType);
    }

    public void processAction() {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Info", "SelectedValue = " + selectedValue));
    }

    public UIForm getForm() {
        return form;
    }

    public void setForm(UIForm form) {
        this.form = form;
    }

    public String getSelectedValue() {
        return selectedValue;
    }

    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }

}

视图

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">

    <f:view contentType="text/html">
        <h:head>
            <f:facet name="first">
                <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
                <title>Demo</title>
            </f:facet>
        </h:head>

        <h:body>
            <p:growl id="growl" showDetail="true" sticky="true" autoUpdate="true" />  

            <h:form>
                <p:remoteCommand name="loadForm" action="#{sampleMBean.createRadio}"
                                 update=":mainForm" global="false" process="@this" />
            </h:form>

            <h:form id="mainForm" binding="#{sampleMBean.form}" />

            <script type="text/javascript">
                jQuery(document).ready(loadForm());
            </script>
        </h:body>
    </f:view>
</html>

答案 1 :(得分:0)

首先感谢user2251135的回复。

我的结局出了问题。 我使用 import javax.faces.event.AjaxBehaviorListener 而不是 import org.primefaces.component.behavior.ajax.AjaxBehaviorListenerImpl

所以我改成了主要的ajax。

...谢谢