0

I have a component on my view which i want to update from my bean.

It works only once. If i press the second button nothing happen, but the bean method in the actionlistener was called.

I checked the browser log with firebug and have no errors or something else which looks odd.

My Bean is in ViewScope. I'm using primefaces 3.5.

JSFPage:

<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:p="http://primefaces.org/ui">


<h:body>
    <h:form id="myRootForm">
    <p:inputText id="text1" styleClass="myChangeClass" disabled="true" />
    </h:form>
    <h:form>
         <p:commandButton value="Activate Insert" 
             actionListener="#{controller.activate()}" update=":myRootForm" />
         <p:commandButton value="Deactivate" 
             actionListener="#{controller.resetFields()}" update=":myRootForm" />
    </h:form>
</h:body>
</html>

This is the EJB code:

import javax.faces.component.UIViewRoot;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.ViewAccessScoped;
import java.io.Serializable;
import javax.inject.Named;

@ViewAccessScoped
@Named("controller")
public class Controller {

    private UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();

    public void activate() {
            HtmlInputText text = (HtmlInputText) 
                         viewRoot.findComponent("myRootForm:text1");
            text.setDisabled(false);
            RequestContext.getCurrentInstance().update(text.getClientId());
    }

    public void deactivate() {
            HtmlInputText text = (HtmlInputText) 
                          viewRoot.findComponent("myRootForm:text1");
            text.setDisabled(true);
            RequestContext.getCurrentInstance().update(text.getClientId());
    }
}

** Update:

I made the example a bit better to read. When i'm using the "RequestScope" it is working, but i need the viewScope to hold my information.

For a better understanding why i want to do it this way: I have several input components which i want to be available in several mode's. The mode is indicated in a StyleClass (i.e. myChangeClass). When i press the button i go through the component tree of my form and searching for the styleclass and activiate the buttons. I don't want to do this by holding for every component an attribute in my bean.

nik the lion
  • 458
  • 1
  • 9
  • 22

1 Answers1

0

I don't know exactly what you're trying to achieve. I've added example below to help you.

XHTML code

<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:p="http://primefaces.org/ui">

<h:head>
    <title>Test</title>
</h:head>

<h:body>

    <h:form id="myRootForm">
        <p:inputText id="text1" disabled="#{tBean.enabled}" />

        <p:commandButton value="Activate Insert"
            actionListener="#{tBean.enable}" update=":myRootForm" />
        <p:commandButton value="Deactivate" actionListener="#{tBean.disable}"
            update=":myRootForm" />
    </h:form>
</h:body>
</html>

ManagedBean Code

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

@ManagedBean(name = "tBean")
@ViewScoped
public class TestBean {

    private boolean enabled;

    public void enable(ActionEvent e) {
        enabled = false;
    }

    public void disable(ActionEvent e) {
        enabled = true;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
}

Try above and see if that helps.

Makky
  • 17,117
  • 17
  • 63
  • 86
  • I give more details in my post. This explain why i don't want to use a attribute to change the state of my component. – nik the lion May 14 '14 at 09:14