Pass UI Component as a method parameter

UI Components for JSF
Post Reply
azroushstang
Posts: 3
Joined: 15 Jul 2014, 18:19

17 Jul 2014, 19:38

I'm trying to pass a JSF UI component as a parameter to a method; however, I'm having no luck. And frankly I'm not even sure what to search for since all of the results that come back deal in Java objects and primitive types.

I'm trying to pass the logical TabView object from the XHTML into the Java method when the button is clicked. However, I haven't figured out how to properly reference it. Passing "constraintTabViewWidget results in a null object. And I haven't figured out how to use the ID's to reference the object either.

The XHTML code:

Code: Select all

                <h:form id="MC">
                    <p:tabView id="AccordionID" widgetVar="constraintTabViewWidget">

                        <p:tab disabled="false" title="#{label.setupConstraints}">
                            <ui:include src="ConstraintSetup.xhtml">
                                <ui:param name="requiredCheck" value="true"/>
                            </ui:include>
                        </p:tab>

                        <p:tab disabled="false" title="#{label.constraintProdWeeks}" id="pw">
                            <ui:include src="ConstraintProductionWeeksSetup.xhtml">
                                <ui:param name="requiredCheck" value="true"/>
                            </ui:include>
                        </p:tab>

                    </p:tabView>

                    <div class="section actionButtons">
                        <h:panelGroup id="saveMC">                            
                            <h:commandLink action="#{constraintController.nextTab(constraintTabViewWidget)}" value="Next" immediate="true"
                                           class="cancelButton actionButtons button"/>
                        </h:panelGroup>
                    </div>
                </h:form>
The method being called:

Code: Select all

public void nextTab(TabView view)
    {
        System.out.println("NEXT TAB PLEASE" + view.getActiveIndex());
        
        logger.info("fdsajkfajf;lsajkl" + view.getActiveIndex());
    }
PrimeFaces 5; JSF 2.0

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

17 Jul 2014, 21:09

the widgetVar is a javaSCRIPT object (and not even that in 5.0.x x<1). The EL requires a JAVA object that is either available as a jsf bean, cdi or whatever.

But how to solve it I have no clue.

User avatar
andyba
Expert Member
Posts: 2473
Joined: 31 Mar 2011, 16:27
Location: Steinfeld, near Bremen/Osnabrück, DE
Contact:

18 Jul 2014, 12:33

You can pass the id of the tabview as a String and search for it in the current FacesContext.
PF 4.x (Elite versions), PF 5, Pf 5.1, PF 6.0
Glassfish 4.1, Mojarra 2.x, Java 8, Payara 4.1.1.
If you haven't read the forum rules read them now

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

18 Jul 2014, 12:41

I was also thinking in that direction but wanted to try it myself first. But it might indeed be the way to go

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

18 Jul 2014, 12:58

You can use: #{constraintController.nextTab(component)}
But "component" is the current component, so you may search the TabView via component#getParent/childs... or via SearchExpressionFacade:

Code: Select all

public viod nextTab(UIComponent button)
{
     TabView tabView = (TabView) SearchExpressionFacade.resolveComponent(FacesContext.getCurrentInstance(), button, "@form:@child(0)");
     ...
}
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

18 Jul 2014, 13:17

ahh... component is one of the 'build-in' JSF EL variables right?

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

18 Jul 2014, 13:28

Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

azroushstang
Posts: 3
Joined: 15 Jul 2014, 18:19

21 Jul 2014, 19:46

Thanks everyone for the help.

While I couldn't make any of these solutions work for me. What I was able to do was to create a binding between the tabView XHTML element and a TabView object in my bean. This allowed me to have access to the TabView element without having to even worry about passing it around. Much cleaner and simpler solution to achieve the same effective results.

Code: Select all

<p:tabView id="AccordionID" widgetVar="constraintTabViewWidget" binding="#{constraintController.tabView}">

                        <p:tab disabled="false" title="#{label.setupConstraints}">
                            <ui:include src="ConstraintSetup.xhtml">
                                <ui:param name="requiredCheck" value="true"/>
                            </ui:include>
                        </p:tab>

                        <p:tab disabled="true" title="#{label.constraintProdWeeks}" id="pw">
                            <ui:include src="ConstraintProductionWeeksSetup.xhtml">
                                <ui:param name="requiredCheck" value="true"/>
                            </ui:include>
                        </p:tab>
</p:tabView>

<h:panelGroup id="saveMC">
                            <h:commandLink action="#{constraintController.save}" value="#{label.save}" rendered="#{null != constraintController.constraint.constraintPK.cstrntId}"
                                           class="saveButton button primaryButton"/>

                            <h:commandLink action="#{constraintController.prepareList}" value="#{label.cancel}" immediate="true"
                                           class="cancelButton actionButtons button secondaryButton"/>
                            
                            <h:commandLink action="#{constraintController.previousTab}" value="Previous" immediate="true"
                                           class="cancelButton actionButtons button"/>
                            
                            <h:commandLink action="#{constraintController.nextTab}" value="Next" immediate="true"
                                           class="cancelButton actionButtons button"/>
                        </h:panelGroup>

Code: Select all

    private TabView tabView;

   public void nextTab()
    {
        int activeTab = tabView.getActiveIndex();
        List<UIComponent> children = tabView.getChildren();
        ((Tab)(children.get(activeTab+1))).setDisabled(false);
        tabView.setActiveIndex(activeTab + 1);
        
    }
    
    
    public void previousTab()
    {
        int activeTab = tabView.getActiveIndex();
        tabView.setActiveIndex(activeTab - 1);  
    }
PrimeFaces 5; JSF 2.0

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 23 guests