1

I have this JSF (Java EE 7, provided by GlassFish 4.1 + PrimeFaces 5.1) form containing database connection information like host name, port number, etc. Also part of this form is a URL field. I want this field to be editable, but I also want to be able to set the value based on the other fields.

To do so I created a button with an action listener where I'm reading the posted data from the request parameter map and generate the new URL value. Then I want to put the new value in the URL field and use that value instead of the posted data. What I tried is to get the component as EditableValueHolder and set the submitted value and render the response. I also tried setting the component's value and calling resetValue.

The best result was the URL field being updates after two clicks.

XHTML:

<p:inputText id="url"
             size="50"
             value="#{database.url}"/>
<p:commandButton icon="ui-icon-arrowrefresh-1-w"
                 immediate="true"
                 actionListener="#{database.createConnectionURL('namingContainer')}">
  <p:ajax update="url" />
</p:commandButton>

Bean (using OmniFaces):

UIComponent urlComponent = Faces.getViewRoot().findComponent(namingContainer + "url");
if (urlComponent instanceof EditableValueHolder) {
  EditableValueHolder editValHolder = (EditableValueHolder) urlComponent;
  editValHolder.setSubmittedValue(urlValue);
}
Faces.getContext().renderResponse();
Jasper de Vries
  • 13,693
  • 6
  • 54
  • 86

1 Answers1

1

The immediate="true" is a leftover from JSF 1.x, when it was not possible to process only a specific set of inputs and/or buttons. It was then more than often abused to process only a specific set of inputs and/or buttons instead of to prioritize validation. You'd better not use it when you've JSF2 ajax at hands.

With JSF2 ajax you can just use execute="..." or in case of PrimeFaces process="..." to execute/process only a specific set of inputs/buttons.

<p:inputText id="url"
             size="50"
             value="#{database.url}" />
<p:commandButton icon="ui-icon-arrowrefresh-1-w"
                 process="@this"
                 action="#{database.createConnectionURL('namingContainer')}" 
                 update="url" />

Then you can just update the model value.

public void createConnectionURL(String namingContainer) {
    // ...
    url = urlValue;
}

Note that I moved back <p:ajax update> into the <p:commandButton>. Perhaps you was mixing with <h:commandButton>.

See also:


Unrelated to the concrete problem, to deal with components with OmniFaces, better use Components utility class.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452