0

I get the following exception during form submit with my customized FacesConverter. But the methods of AccountConverter returns the right values (id and account).

transactionForm:account: Überprüfungsfehler: Wert ist ungültig.

That's German and correspondents to the title of my posting.

In my project I use JSF 2.2, so I am forced to use @Named and @RequestScope to have @EJB support.

Here are my implementation:

AccountConverter.java

    @Named
    @RequestScoped
    public class AccountConverter implements Converter {

    /* CONSTANTS */

    private static final Logger LOGGER = Logger.getLogger(AccountConverter.class.getSimpleName());

    /* EJB */

    @EJB
    private AccountDaoImpl accountDao;

    //

    @PostConstruct
    public void onCreate() {
        LOGGER.info("+++ accountConverter");
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {

        if(submittedValue == null || submittedValue.isEmpty()) {
            return null;
        }

        // database access

        try {
            return this.accountDao.find(Integer.valueOf(submittedValue));
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object modelValue) {

        if(modelValue == null) {
            return null;
        }

        if(modelValue instanceof Account) {
            return Integer.toString(((Account) modelValue).getId());
        }
        else {
            throw new ConverterException(new FacesMessage(modelValue + " is not a valid account entity"));
        }
    }

    }

transaction.xhtml

    <!-- TRANSACTION -->
    <h:form id="transactionForm">

        <!-- ACCOUNT -->
        <div class="form-group">
            <label for="account">Account:</label>
            <h:selectOneMenu id="account" value="#{transactionCtl.transaction.account}" converter="#{accountConverter}" class="form-control">
                <f:selectItem noSelectionOption="true" itemLabel="no Type" />
                <f:selectItems value="#{accountCtl.accounts}" var="account" itemLabel="#{account.name}" itemValue="#{account}" />
            </h:selectOneMenu>
            <h:message for="account" infoClass="bg-info" warnClass="bg-warning" errorClass="bg-danger" />
        </div>

        <!-- BUTTON: SAVE -->
        <h:commandButton id="saveBtn" value="Speichern" action="#{transactionCtl.save()}"
            rendered="#{not empty transactionCtl.transaction}" styleClass="btn btn-primary btn-block">
            <f:ajax execute="@form" render="@form :globalMessages :transactionsForm" />
        </h:commandButton>

    </h:form>

So, I hope that anybody could tell me what's wrong. Thanks for help.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
schth
  • 1
  • Suggestion: change your development environment to English. Exception/error messages in English give so many more useful search results. – BalusC Nov 08 '15 at 10:40
  • @BalusC thank you for your quick answer. I had forgotten to override the hashCode and equals methods in the account entity. – schth Nov 13 '15 at 22:02

0 Answers0