Problem setting component visibility

UI Components for JSF
Post Reply
burferd
Posts: 234
Joined: 01 May 2010, 16:15

16 May 2010, 20:35

I put together a simple project to test making a component display or not, based on the render property.
This was created in NetBeans 6.9, JSF2, PrimeFaces 2.0.1, Internet Explorer 7.
It consists of a simple index.xhtml page with two composite components (form1 and form2).
Each component has an h:outputText and p:commandButton.

Pushing the command button for form1 will set the rendered property for form1=false and form2=true.
Pushing the command button for form2 will set the rendered property for form1=true and form2=false.
The object is to push a button to make that component invisible and make the other component visible.
The effect should be to be able to toggle between components.

What actually happens is that when a button is pushed, that component will become invisible, but the other does not become visible.
If you refresh the browser, the other component displays.
Basically the the screen needs to be refreshed before the missing component is rendered.
I suspect that this has to do with the component not being included when the page is originally rendered, so the page needs to be re-rendered in order for it to be included.

I was able to do this using ICEfaces and was hoping I could do the same thing using PrimeFaces, by using the update property to refresh that component.

Any ideas on how I can do this?
I have included the code below.

Thanks.

index.xhtml

Code: Select all

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:comp="http://java.sun.com/jsf/composite/ezcomp"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <comp:comp1 id="comp1"/>
        <comp:comp2 id="comp2"/>
    </h:body>
</html>
comp1.xhtml

Code: Select all

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:cc="http://java.sun.com/jsf/composite"
      xmlns:p="http://primefaces.prime.com.tr/ui">

  <!-- INTERFACE -->
  <cc:interface>
  </cc:interface>

  <!-- IMPLEMENTATION -->
  <cc:implementation>
      <h:form id="form1" style="position: absolute; border-style: groove; border-width: 2px; height: 200px; width: 200px" rendered="#{sessionBean.disp1}" >
          <h:outputText value="Component 1"/>
          <p:commandButton update="comp1:form1,comp2:form2" action="#{sessionBean.comp1_action}" value="Component 1"/>
      </h:form>
  </cc:implementation>
</html>
comp2.xhtml

Code: Select all

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:cc="http://java.sun.com/jsf/composite"
      xmlns:p="http://primefaces.prime.com.tr/ui">

  <!-- INTERFACE -->
  <cc:interface>
  </cc:interface>

  <!-- IMPLEMENTATION -->
  <cc:implementation>
      <h:form id="form2" style="position: absolute; border-style: groove; border-width: 2px; left: 220px; height: 200px; width: 200px" rendered="#{sessionBean.disp2}" >
          <h:outputText value="Component 2"/>
          <p:commandButton update="comp1:form1,comp2:form2" action="#{sessionBean.comp2_action}" value="Component 2"/>
      </h:form>
  </cc:implementation>
</html>
SessionBean

Code: Select all

import java.io.Serializable;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;

@Named(value="sessionBean")
@SessionScoped
public class SessionBean implements Serializable{

    /** Creates a new instance of SessionBean */
    public SessionBean() {

    }

    public void comp1_action(){
        disp1 = false;
        disp2 = true;
    }

    public void comp2_action(){
        disp1 = true;
        disp2 = false;
    }

    private boolean disp1 = true;
    public void setDisp1( boolean b ) {disp1=b;}
    public boolean getDisp1() {return disp1; }

    private boolean disp2 = true;
    public void setDisp2( boolean b ) {disp2=b;}
    public boolean getDisp2() {return disp2; }
}
Using PrimeFaces 3.4, Mojarra 2.1.6, Glassfish 3.1.2, NetBerans 7.2, Hibernate 3.2.5 (sometimes)
Windows 7.

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

19 May 2010, 15:04

Well in PrimeFaces or JSF2 ajax, you can't update a component that has no corresponding DOM element existing page, that's why you need to add container element like p:outputPanel and update it instead.

Code: Select all

<p:commandButton update="panel" />

<p:outputPanel id="panel">
   <h:form rendered="true or false based on a condition">
   </h:form>
</p:outputPanel>

burferd
Posts: 234
Joined: 01 May 2010, 16:15

19 May 2010, 23:25

That works, thanks.
Using PrimeFaces 3.4, Mojarra 2.1.6, Glassfish 3.1.2, NetBerans 7.2, Hibernate 3.2.5 (sometimes)
Windows 7.

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

24 May 2010, 15:32

Glad to hear!

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 33 guests