Sami
Sami

Reputation: 2331

How to submit same form two times?

I have to say, that I am not sure is that a problem, but I think so. I have a from where is two submit buttons. With first one I fetch product infos via serial number and with two I submit the whole form again.

Search button and code:

BUTTON 1

<p:column><h:outputLabel value="#{bundle.newRma_label_sarjanumero}" for="sarjanro" /></p:column>
                <p:column><p:inputText id="sarjanro" value="#{MainController.selected.sarjanro}" style="width: 200px;" /></p:column>
                <p:column><p:commandButton icon="ui-icon-search"
                                           action="#{MainController.haeTiedotSarjanrolla}" style="width: 100px;" update=":messagePanel"/></p:column>

 //Fetching product info with serial number
public String haeTiedotSarjanrolla() {
    log.info("sarjanro=" + current.getSarjanro());
    Sarjanumerot s = null;

    s = helper.getSerialInfo(current.getSarjanro());
    if (s != null) {
        current.setAn8(s.getAn8());
        current.setDsc1(s.getDsc1());
        current.setDsc2(s.getDsc2());
        current.setSarjanro(s.getId().getLotn());
        current.setTuotenro(s.getId().getLitm());
    }

    // Back to same page!
    return "newRma";
}

Another button (save)

<p:column><p:commandButton action="#{MainController.talletaUusiRma}" value="#{bundle.newRma_tallenna}" immediate="true" style="width: 220px;"/></p:column>

Save (Button 2) is working, BUT request is empty, there is not any data. I tried to check it like that and it is always NULL:

another field what is not populated after first submit:

<p:row>
            <p:column></p:column>
            <p:column><p:outputLabel value="#{bundle.newRma_shortdesc}" for="shortdesc"/></p:column>
            <p:column><p:inputTextarea rows="4" cols="30" id="shortdesc" value="#{MainController.selected.shortdesc}" style="width: 200px;"/></p:column>

        </p:row>

Map<String, String> parameterMap = (Map<String, String>) FacesContext.getCurrentInstance()
                     .getExternalContext().getRequestParameterMap();
    String temp = parameterMap.get("shortdesc");

temp is always NULL!!

Is there any best practise to handle this with primefaces, ajax, js or something else?

Thanks! Sami

Upvotes: 0

Views: 165

Answers (1)

BalusC
BalusC

Reputation: 1108587

This works only if the bean is @ViewScoped and if you return null or void from action method.

If you return non-null or void, such as follows

// Back to same page!
return "newRma";

then a brand new view scoped bean instance will be created, hereby trashing the initial one.

Further, I'm not sure why you used immediate="true" on the second button, this makes no sense, but are you aware that this way the data of non-immediate inputs won't be processed at all and thus you should do it with the data of the previous non-immediate submit?

By the way, the way how you collected submitted data makes also no sense. You don't need to traverse the request parameter map (you used an invalid parameter name by the way). Just directly access the bean properties which are been bound to input values.

Upvotes: 2

Related Questions