【问题标题】:Primefaces: Ajax update with RequestContext running only oncePrimefaces:Ajax 更新,RequestContext 只运行一次
【发布时间】:2014-05-13 14:08:39
【问题描述】:

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

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

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

我的 Bean 在 ViewScope 中。我正在使用 primefaces 3.5。

JSFP 页面:

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

** 更新:

我使这个例子更易读。 当我使用“RequestScope”时它正在工作,但我需要 viewScope 来保存我的信息。

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

【问题讨论】:

    标签: ajax jsf-2 primefaces requestcontext


    【解决方案1】:

    我不知道你想要达到什么目的。我在下面添加了示例来帮助您。

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

    试试上面的方法,看看是否有帮助。

    【讨论】:

    • 我在帖子中提供了更多详细信息。这解释了为什么我不想使用属性来更改组件的状态。
    猜你喜欢
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 2011-12-13
    相关资源
    最近更新 更多