h:commandButton can NOT submit parameters to backi

2020-07-24 04:37发布

问题:

I would like to submit a key value to my backing bean so that I know which person within a collection user trying to update. I think I need to used f:param to do so, but somehow it does not work. It will submit the value just fine if I use af:commandButton instead of h:commandButton.

Here is my button:

<h:commandButton styleClass="cntctmBtn" value="Update" action="#{pullForm.updateDependent}">
   <f:param name="selectedIndex" value="#{loop.index}" />
   <f:param name="selectedEDI" value="#{eachOne.identifier.dodEdiPnId}" />
</h:commandButton>

and here is how I am trying to get my submitted values out.

FacesContext context = FacesContext.getCurrentInstance();
Map map = context.getExternalContext().getRequestParameterMap();
String edi_tmp = (String)map.get("selectedEDI");

But I got the ArrayIndexOutOfBound Exception, please help, thanks.

回答1:

If the button is inside a <h:dataTable> or any other UIData component, then you should be retrieving the "current" row object by UIData#getRowData() or DataModel#getRowData(). No need to pass the row identifier around as parameter or so.

E.g.

@ManagedBean
@ViewScoped
public class Bean {
    private List<Person> persons;
    private DataModel<Person> personModel;

    public Bean() {
        persons = loadItSomehow();
        personModel = new ListDataModel<Person>(persons);
    }

    public void update() {
        Person selectedPerson = personModel.getRowData(); // There it is.
        // ...
    }

    // Add/generate getters/setters/etc.
}

with

<h:form>
    <h:dataTable value="#{bean.personModel}" var="person">
        <h:column>
            <h:commandButton value="update" action="#{bean.update}" />
        </h:column>
    </h:dataTable>
</h:form>

Keep it simple.

See also:

  • CRUD example with @ViewScoped