RequestContext won't work

Socrates

I am having trouble to update the view from the bean in the back using PrimeFaces's RequestContext. In the example below I have a button and 2 panels. When pressing the button, I want to update one panel, but not the other one. It does not work though and I can't find the error! requestContext.update("panela"); is fired, but doesn't do its job! Help greatly appreciated!

The XHTML file:

<!DOCTYPE html>
<html xmlns="http://www.w3c.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head/>
    <h:body>
        <h:form>
            <p:panelGrid columns="1">
                <p:commandButton value="Save" actionListener="#{runtimeUpdatesBean.save}" />
                <p:panel id="panela">
                    <h:outputText value="#{runtimeUpdatesBean.texta}"/>
                </p:panel>
                <p:panel id="panelb">
                    <h:outputText value="#{runtimeUpdatesBean.textb}"/>
                </p:panel>
            </p:panelGrid>
        </h:form>
    </h:body>
</html>

The bean:

package com.glasses.primework;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.context.RequestContext;

@ManagedBean
@SessionScoped
public class RuntimeUpdatesBean {
    private String texta;
    private String textb;
    private boolean outcome;

    public String getTexta() {
        texta += "a";
        System.out.println("RuntimeUpdatesBean.getTexta() = " + texta);
        return texta;
    }

    public String getTextb() {
        textb += "b";
        System.out.println("RuntimeUpdatesBean.getTextb() = " + textb);
        return textb;
    }

    public void save() {
        RequestContext requestContext = RequestContext.getCurrentInstance();

        if(outcome) {
            System.out.println("RuntimeUpdatesBean.save() = update panela");
            requestContext.update("panela");
            outcome = false;
        } else {
            System.out.println("RuntimeUpdatesBean.save() = update panelb");
            requestContext.update("panelb");
            outcome = true;
        }
    }
}
Kishor Prakash

Well the problem is the ID of the component that you are referring.
In JSF when you place a component inside h:form (or Some Primefaces components like TabView), that component's Id will be generated based on the h:form id too.
Here is the Example:

<h:form id="panelaForm">
   <p:panel id="panela">
       ....
   </p:panel>
</h:form>

In the above case your p:panel's id will be generated as panelaForm:panela.
In your case since you haven't provided any ID for h:form a dynamic id will be attached like for example j_xyz:panela(you can see it using you browser's Inspect Element).

So If you wan to access p:panel with Id panela inside the same h:form then no need to attach the form Id.
But If you want to access the p:panel outside h:form then you need to attach the h:form id to access it.

Solution to you problem is: use an custom ID to your h:form (which is a best practice by the way..) and access the p:panel by attaching that form ID.

<h:form id="panelaForm">
   <p:panel id="panela">
       ....
   </p:panel>
</h:form>

And in Managed bean use:

RequestContext.getCurrentInstance().update("panelaForm:panela");

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

AJAXify won't work on phpBB?

분류에서Dev

Program won't work in FireFox

분류에서Dev

Fade in Function Won't Work

분류에서Dev

Why won't this regexp in htaccess work properly?

분류에서Dev

Alsamixer won't work with bluetooth device

분류에서Dev

Post variables with ajax won't work

분류에서Dev

Simple class won't work in C++

분류에서Dev

Laravel named routes won't work on wampserver

분류에서Dev

Why won't the routing work in this code?

분류에서Dev

Now visudo won't work at all

분류에서Dev

Why won't this MySQL stored procedure work?

분류에서Dev

The hover for my buttons and links won’t work

분류에서Dev

My update queries won't work

분류에서Dev

For /F "skip=0" won't work as expected

분류에서Dev

Windows 7 computer won't boot and recovery doesn't work

분류에서Dev

Laravel facade Slug won't work for non English?

분류에서Dev

Graphql Query with express and Mongodb won't work properly?

분류에서Dev

Why std::basic_fstream<unsigned char> won't work?

분류에서Dev

Java split() method won't work a second time

분류에서Dev

jQuery delay and javascript settimeout won't work with facebook button

분류에서Dev

Google Maps won't work when API key is provided

분류에서Dev

VBA - Conditional formatting: Excel 2010 won't work, Excel 2013 will

분류에서Dev

changing genemu jquery datepicker localization seetings won't work

분류에서Dev

Lightbox won't work when my site css page is linked

분류에서Dev

Wi-Fi just won't work on Ubuntu 17.10

분류에서Dev

boot menu shows up but Ubuntu won't work

분류에서Dev

Lubuntu 16.04 - indicator applet area of the LXPanel won't work

분류에서Dev

Bash completion won't work if calling application with the absolute path

분류에서Dev

MariaDB won't work after moving database to external drive

Related 관련 기사

뜨겁다태그

보관