2

I'm approaching JSF binding argument in PrimeFaces.

This is my form:

<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">
<h:head>
    <h1 class="title ui-widget-header ui-corner-all">
        <p:spacer width="100" height="10" />
        PrimeFaces Test Binding
    </h1>
    <title><ui:insert name="title">PrimeFaces Test</ui:insert></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</h:head>
<h:body>
    <h:form id="form1"> 
        <h:panelGroup binding="#{bindingTestClass.panelGroup}" />
    </h:form>
</h:body>
</html>

And that's the body:

@ManagedBean(name="bindingTestClass")
@ViewScoped
public class BindingTestClass implements Serializable{

    transient HtmlOutputLabel testValue = null; 
    transient HtmlSelectOneMenu menu = null; 
    transient HtmlPanelGroup panelGroup = null; 

    @PostConstruct
    private void makeUp(){ 

        menu = new HtmlSelectOneMenu();
        panelGroup = new HtmlPanelGroup();
        testValue = new HtmlOutputLabel();

        panelGroup.setId("B");
        panelGroup.setLayout("block"); 
        panelGroup.setStyleClass("grid-form");

        //LINE 0
        testValue.setId("A");
        testValue.setValue("BasicLabel"); 
        panelGroup.getChildren().add(testValue);

        HtmlOutputText  linebreak = new HtmlOutputText();
        linebreak.setValue("<br/>");
        linebreak.setEscape(false);
        panelGroup.getChildren().add(linebreak); 

        menu.setId("F"); 

        // populate the drop down list 
        UISelectItems items = new UISelectItems(); 

        List comboList = new ArrayList(); 

        comboList.add(new SelectItem("---")); 
        for(int a = 0; a <10; a++){
            comboList.add(new SelectItem( a+1 + " test"));
        } 
        items.setId("ss");
        items.setValue(comboList); 
        menu.getChildren().add(items); 
        //Add list to combobox 
        /*this first attempt doesn't work either, only change backend values
        menu.addValueChangeListener(new ValueListenerTest()); 
        menu.setOnchange("submit()");
        */ 
        AjaxBehavior ajaxBehavior = (AjaxBehavior) FacesContext.getCurrentInstance().getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
        ajaxBehavior.addAjaxBehaviorListener(new CustomAjaxListener());
        //ajaxBehavior.setTransient(true);  
        ajaxBehavior.setUpdate("form1");  
        menu.addClientBehavior("change",ajaxBehavior); 
        panelGroup.getChildren().add(menu);

    }

    //Getters,Setters 
    public HtmlPanelGroup getPanelGroup() {
        return panelGroup;
    }

    public HtmlOutputLabel getTestValue() {
        return testValue;
    }

    public void setTestValue(HtmlOutputLabel testValue) {
        this.testValue = testValue;
    }

    public void setPanelGroup(HtmlPanelGroup panelGroup) {
        this.panelGroup = panelGroup;
    } 
    public HtmlSelectOneMenu getMenu() {
        return menu;
    } 
    public void setMenu(HtmlSelectOneMenu menu) {
        this.menu = menu;
    } 
}

The fact is that if and only if i put the separate binding code in my view this way:

<h:outputLabel binding="#{bindingTestClass.testValue}" />

and obviously take out the corresponding child from panelgroup, when I change the selectOneMenu value, the listener change the value in the backing bean, and then it updates the form which updates indeed the value of the "testValue" label.

I've tried different attempts but still didn't catch why it won't work in this way, putting everything in a single panelgroup.

Can anybody point me in the right direction?

Many many thanks in advance!

EDIT: the code above works with @RequestScoped scope and obviously putting all attributes variabile in "private" modifier mode. But there's no chance with the @ViewScoped?

Black.Jack
  • 1,878
  • 2
  • 22
  • 39
  • 2
    I don't often see people programmatically build JSF components in the backing-bean. I'm not saying it must not be done, but it's you're going to get fewer answers if you implement things in unconventional ways. Looking at your code, I can't immediately see anything that couldn't be more easily implemented on the Facelets page ([ref](http://stackoverflow.com/a/19999684/201891)). – DavidS Apr 24 '15 at 18:49
  • I don't understand, if i wrap in a facelet tag it doesn't work either, it doesn't show up anything too after the main title. I need to implement this way, i got dynamical component lists that before page is loaded can decide what has to be on screen. Thanks 4 coop. – Black.Jack Apr 25 '15 at 05:48
  • 2
    Search SO for binding and viewscope. Binding should use requestscoped beans – Kukeltje Apr 25 '15 at 09:16
  • Yup in requestScoped, it works. But what if want a ViewScoped? Yeah i've searched a lot before putting this post, i'm here just because i can't proceed alone. Edited the updating info and trial. Thanks for precious coop. – Black.Jack Apr 25 '15 at 09:51
  • Strange kind of code, i have never seen anyone doing such things, why are you building your html stuff from MB. What is that you want to get as output from your code, most of your code can be done on the jsf/xhtml page and only do the getter/setter/logic/etc at the MB. Frankly you are making things more complicated, JSF is designed to make life much easier that too with addons like primeface/omniface/etc. – user1433804 Apr 25 '15 at 18:25

1 Answers1

0

Binding in JSF works correctly only with request scope beans.

Look at this question

Community
  • 1
  • 1