Primefaces:使用RequestContext仅运行一次的Ajax更新

时间:2014-05-13 14:08:39

标签: ajax jsf-2 primefaces requestcontext

我的视图中有一个组件,我想从我的bean更新。

它只工作一次。如果我按下第二个按钮没有任何反应,但调用了actionlistener中的bean方法。

我用firebug检查了浏览器日志,没有任何错误或其他看似奇怪的东西。

My Bean在ViewScope中。我使用的是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>

这是EJB代码:

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());
    }
}

**更新:

我让这个例子好一点。 当我使用&#34; RequestScope&#34;它正在工作,但我需要viewScope来保存我的信息。

为了更好地理解我为什么要这样做: 我有几个输入组件,我希望在几种模式下可用。 模式在StyleClass中指示(即myChangeClass)。 当我按下按钮时,我会浏览表单的组件树并搜索样式表并激活按钮。我不想通过在我的bean中保存每个组件的属性来实现这一点。

1 个答案:

答案 0 :(得分:0)

我不确切地知道你想要实现的目标。我在下面添加了示例来帮助您。

XHTML代码

<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代码

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;
    }
}

尝试以上,看看是否有帮助。