0

I'm making a simple program, where I try to open a dialog, set variables inside of it, then click a button at the bottom to make a new object.

<p:dialog header="Add a book" widgetVar="dlg" modal="true">
      <h:outputText value="Title: "/>
      <p:inputText label="Title: " value="#{bookController.title}" /><br/>
           
...

      <p:selectCheckboxMenu title="Author(s)" value="#{bookController.authors}" label="Author(s)" style="min-width: 15rem" multiple="true" filter="true" filterMatchMode="startsWith" panelStyle="width: 30rem" scrollHeight="250">
              <p:ajax event="itemUnselect" listener="#{bookController.onItemUnselect}"/>
              <f:selectItems value="#{bookController.allAuthors}"/>
      </p:selectCheckboxMenu><br/>

      <p:selectOneMenu value="#{bookController.language}" >
            <f:selectItems var="languages" itemValue="#{languages}" itemLabel="#{languages}" value="#{bookController.allLanguages}"/>
      </p:selectOneMenu>

      <p:commandButton action="#{bookController.addBook}" value="Submit" onclick="PF('dlg').hide();"/>
</p:dialog>

In the above mentioned code, I ran into an issue, where I can't use this dialog to add an Object if I try to use <f:selectItems> inside of a <p:selectOneMenu> or <p:selectOneListbox>.

It works like a charm without the code snippet or even just without the <f:selectItems> like below, but when I add it back and click on the button, the dialog will close without the action being done (#{bookController.addBook})

  <p:selectOneMenu value="#{bookController.language}" >
  </p:selectOneMenu>

How could I fix this issue or is there a workaround for this kind of problem?

UI: All the lists are populates and there are no constraints/field requirements, which could affect the action.


Update:

Okay, I managed to set up a converter for it, however it seems like a converter/<p:selectOneMenu> issue to me.

Converter

package aria.web.librarian;

import aria.domain.dao.LanguageDao;
import aria.domain.ejb.Language;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
import java.util.List;

@FacesConverter(value = "selectLanguageConverter")
public class SelectLanguageConverter implements Converter {

    @Inject
    private LanguageDao languageDao;

    public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
        if (modelValue == null) {
            return ""; // Never return null here!
        }

        if (modelValue instanceof Language) {
            return String.valueOf(((Language) modelValue).getLanguageId());
        } else {
            throw new ConverterException(new FacesMessage(modelValue + " is not a valid Warehouse"));
        }
    }

    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        if (submittedValue == null || submittedValue.isEmpty()) {
            return null;
        }
        try {
            List<Language> allLanguages = languageDao.getLanguages();
            Language language = languageDao.getForLanguageId(Long.parseLong(submittedValue));
            return language;
        } catch (NumberFormatException e) {
            throw new ConverterException(new FacesMessage(submittedValue + " is not a valid Languagae ID"), e);
        }
    }
}

SelectOneMenu

<p:selectOneMenu value="#{bookController.language}" converter="selectLanguageConverter">
    <f:selectItem itemLabel="Choose one" itemValue="#{null}"/>
    <f:selectItems value="#{bookController.allLanguages}" var="language"
    itemLabel="#{language.languageName}" itemValue="#{language}"/>
</p:selectOneMenu>

The problem still occurs with converter as well, but for some reason, when putting a breakpoint to the method of Submit button, it will not stop, so I can't check the values there.

However, when putting a breakpoint to the getAsObject method of my SelectLanguageConverter, I can see, that it's get called and get's the actual Language (which is selected), but the window just closes and the button does not call BookController.addBook.

What could cause this issue and how could this be solved?

Thank you in advance.

Primefaces 8.0.RC3 Maven v4.0.0

Kokusz19
  • 1
  • 2
  • Why are you on an old release candidate? – Jasper de Vries Sep 25 '21 at 20:45
  • Started out with an older "dummy project" rather than to do a full a clean start. – Kokusz19 Sep 25 '21 at 20:49
  • also it sounds like a "Converter" issue that your values are not being properly decoded to the backend because of missing POJO converters but you didn't post enough code. But most likely you are missing an "AuthorConverter". – Melloware Sep 26 '21 at 14:58

1 Answers1

0

Addig custom converter(s) (tried several types), however the only working solution I found is to use SelectItemsConverter of omnifaces.

<h:selectOneMenu value="#{bean.selectedItem}" converter="omnifaces.SelectItemsConverter">
    <f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>

with the following dependency

<dependency>
    <groupId>org.omnifaces</groupId>
    <artifactId>omnifaces</artifactId>
    <version>3.11.1</version>
</dependency>

and html tags in my *.xhtml

xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions"

After replacing my custom converters, the <p:selectOneMenu> worked as intended.

Kokusz19
  • 1
  • 2