0

So, I have a session scoped bean that has a 2 lists of string values. This bean is called AgreementBean.java. I'm showing these lists in a page called agreementDetail.xhtml like this

<h:dataTable id="servers" value="#{agreement.licenseServerNames}" var="licenseServerName">
                        <h:column>
                            <h:inputText value="#{licenseServerName}"/>
                        </h:column>
                    </h:dataTable>
                    Computer IDs<br/>
                    <h:dataTable id="idNames" value="#{agreement.computerIdNames}" var="computerIdName">    
                        <h:column>
                            <h:inputText value="#{computerIdName}"/>
                        </h:column>
                    </h:dataTable>  

As you can see, I expect user input on these values. I need to make an Ajax call to update those values when the customer clicks on a "Save button". Here's the button's jsf code.

<script type="text/javascript">
                        function showAlert(data){                          
                                 alert("SAVED!");   
                        }
                    </script>
                    <h:commandButton value="Save" immediate="true" type="submit" action="#{agreement.save}">
                        <f:ajax onevent="showAlert"/>
                    </h:commandButton><br/><br/>    

The "Save" bean method does nothing right now, except for logging the values stored in both lists. When clicking on the button, 2 things are happening right now. If the customer changed the values on the inputFields, the bean's list's value is set to null. If the customer didn't change anything, then the bean's original value is kept.

How can I fix this? Thanks!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nacho321
  • 1,911
  • 7
  • 33
  • 55

1 Answers1

7

There are 2 problems with your command button:

<h:commandButton value="Save" immediate="true" type="submit" action="#{agreement.save}">
    <f:ajax onevent="showAlert"/>
</h:commandButton>
  1. immediate="true" causes that only input elements which also have immediate="true" set will be processed. However, your inputs don't have this attribute set.

  2. <f:ajax execute> defaults to @this, causing that only the command button itself is processed during form submit. Your inputs are therefore skipped in processing.

Get rid of the misplaced attribute and tell <f:ajax> to execute the entire form.

<h:commandButton value="Save" type="submit" action="#{agreement.save}">
    <f:ajax execute="@form" onevent="showAlert"/>
</h:commandButton>

See also:


Then there's a potential problem with your data model. You seem to be supplying a List<String> to the data table instead of a List<SomeBean>. The String is immutable and doesn't have a setter for the value. A <h:inputText value="#{string}"> is never going to work. For the first table, you really need to have a LicenseServer bean with a private String name property. You can then use it as follows:

<h:dataTable value="#{agreement.licenseServers}" var="licenseServer">
    <h:column>
        <h:inputText value="#{licenseServer.name}"/>

See also:


Unrelated to the concrete problem, are you aware that onevent is invoked 3 times? For the purpose you'd probably like to check if ajax event status equals to "success".

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You are spot on on the onevent thing... I'll fix that pretty soon, thanks! Also, now, something weird is going on when clicking the button. Instead of updating the list values, it's adding the original value twice to the list. So, instead of changing to , the list has . Ideas? – Nacho321 Aug 12 '13 at 19:41
  • 1
    Hard to tell based on the code provided so far. It's too much oversimplified that it wouldn't work at all. You should have something like `` and not ``. – BalusC Aug 12 '13 at 19:44
  • The thing is that it's a list handled by a dataTable. The property is the list itself, not the values contained in it, and those values are the inputText fields. :/ (Sorry if I'm bothering you... I'm new to JSF) – Nacho321 Aug 12 '13 at 19:47
  • 1
    I didn't mean that. You seem to have a `List` instead of a `List`. Strings are immutable and it's impossible to update the values this way. If that's the *real* code, then well, you got there another cause. – BalusC Aug 12 '13 at 19:48
  • Ok, I'll try to find another way to handle inputFields on string lists. Google time! Thanks for your help, sir! – Nacho321 Aug 12 '13 at 19:50