0

I am using JSF and I have the following problem:

I have 2 command buttons:

<h:commandButton id="btn1" value="Button 1" action="#{bean.action1()}"
   onclick="#{rich:component('confirmation')}.show();" /> 

<h:commandButton id="btn2" value="Button 2" action="#{bean.action2()}"
   onclick="#{rich:component('confirmation')}.show();" /> 

So, when they are clicked, a popup should show (with id="confirmation").

Here is the popup:

<rich:popupPanel id="confirmation" header="Note" modal="true"
            width="525" height="180" resizable="false" headerClass="rf-p-hdr"
            autosized="false">

        <a4j:queue ignoreDupResponses="true" requestDelay="500" />

        <f:facet name="controls">
            <h:panelGroup>
                <a4j:commandButton
                    image="#{resource['default:img/close_button.png']}"
                    styleClass="close_button" action="#{bean.cancel()}"
                    render="@all"
                    oncomplete="#{rich:component('confirmation')}.hide(); return false;"
                    status="ajaxProgress" />
            </h:panelGroup>
        </f:facet>

        <s:div styleClass="help_embedded"
            style="margin-bottom: 20px; margin-top: 20px;">
            <h:outputText value="Here I want to display a message that tells the user which button was pressed." />
        </s:div>

        <s:div style="margin-left: 82px;">

            <s:decorate template="/layout/field.xhtml">

                <a4j:commandButton value="#{messages['allPages.button.save']}"
                    action="#{bean.save()}" execute="@this"
                    oncomplete="#{rich:component('confirmation')}.hide(); return false;"
                    render="@all"
                    status="ajaxProgress" />

                <a4j:commandButton value="#{messages['allPages.button.cancel']}"
                    styleClass="cancel" action="#{bean.cancel()}"
                    execute="@this"
                    oncomplete="#{rich:component('confirmation')}.hide(); return false;"
                    render="@all"
                    status="ajaxProgress" />

            </s:decorate>

        </s:div>


    </rich:popupPanel>

My problem is: if you look at the h:outputText inside the popup - I want it to display a message to the user based on which command button was pressed (btn1 or btn2). For example, if the user pressed btn1, the popup message will be - "You just pressed button 1. Are you sure you want to perform this action?". And if the user pressed button 2, the message would read: "You just pressed button 2. Are you sure you want to perform this action?".

I have tried updating a backing bean value each time a button is pressed and then rerendering the popup, but is seems like the popup tag is generated only once when the page loads and rerendering it does nothing.

I would also like a solution where all dynamic changes to the UI occur on the front - end only, without bothering the server to regenerate the page - so pressing button 1 will modify a javascript front end variable and the h:outputText in the popup will read this value to display the message.

Do you have an idea how this can be achieved?

1 Answers1

0

Using "local" variables to perform this in framework like JSF is quite contradictory. In my opinion you should use the update attribute in the commandButtons to specify what component of the page must be updated after you execute your code in the bean so the whole page don't need to be reloaded and the component is updated with the new info.

Take a look at this question for an extended explanation.

Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
  • 1
    JSF is not heavily specifically session based, developers (can ab)use it that way and yes I use it too in many cases but you can do many things with shorter scopes (especially the deltaspike viewaccessscoped is interesting. And btw OP '(ab)uses' `render="@all"` making the oncomplete closing dialogs superfluous... – Kukeltje Dec 13 '18 at 18:18
  • @Kukeltje I was trying to explain the situation to someone who seems to be learning how the basic render/update cycle works in JSF. Of course there are many more efficient ways to solve it, including the use of shorter scopes. – Josep Oriol Soler Dec 15 '18 at 09:26
  • I appreciate that, but stating 'jsf is heavily session scoped' is precisely why some say JSF should not be used. Especially beginners should get the **right** information. And wit JSF you can still do client-side things in many cases (in the end it is all just html, css and javascript on the client). I combine things a lot and certainly behind the 'scenes' extended JSF components like the PrimeFaces datatable used client-side javascript based local variables a lot. – Kukeltje Dec 15 '18 at 10:20