0

Tweaked the converter from this answer

// @FacesConverter(forClass = Crew.class) // no injection
@ManagedBean
@RequestScoped
public static class CrewConverter implements Converter {

    @EJB
    private CrewService cs;

    @Override
    public String getAsString(FacesContext context, UIComponent component,
            Object value) {
        return (value instanceof Crew) ? ((Crew) value).getName() : null;
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component,
            String value) {
        if (value == null) {
            return null;
        }
        System.out.println("value : " + value);
        List<Crew> allCrew = cs.allCrew();
        for (Crew crew : allCrew) {
            if (crew.getName().equals(value)) {
                System.out.println("value found: " + value);
                return crew;
            }
        }
        throw new ConverterException(new FacesMessage(String.format(
            "Cannot convert %s to Crew", value)));
    }
}

On one of the items the converter fails with:

Cannot convert ÎανάÏÎ·Ï ÎÎ­Î³Î³Î¿Ï to Crew

For the rest conversion passes (but not validation which is another matter I had to define equals() in Crew!)

The value is displayed correctly - should I tweak the converter or the problem may lie deeper (in the server config) ?

I am on glassfish 4

EDIT - the form:

<h:form id="movie_crew_form"
    rendered="#{movieController.movie  != null}" >
<h:panelGrid columns="2">
    <h:selectOneListbox id="crewMember" redisplay="true" size="8"
        value="#{movieController.crewMember}"
        converter="#{movieController$CrewConverter}">
        <f:selectItems value="#{movieController.allCrew}" var="entry"
            itemValue="#{entry}" itemLabel="#{entry.name}" />
        <f:ajax event="blur" render="crewMemberMessage" />
    </h:selectOneListbox>
    <h:message id="crewMemberMessage" for="crewMember" />
</h:panelGrid>
<h:commandButton value="Add" action="#{movieController.addCrewMember}" />
</h:form>

EDIT2: the message appears when I hit the "Add" button - when I alt-tab out of the form (so onBlur is triggered) the conversion works ! Sample output (on eclipse console)

On alt tab:

INFO: value : ??????? ??????
FINE: SELECT IDCREW, NAME FROM CREW
INFO: value found: ??????? ??????
FINE: SELECT IDCREW, NAME FROM CREW
FINE: SELECT IDCREW, NAME FROM CREW

On "Add":

INFO: value : ??????? ??????
FINE: SELECT IDCREW, NAME FROM CREW
INFO: value found: ??????? ??????
FINE: SELECT IDCREW, NAME FROM CREW
FINE: SELECT IDCREW, NAME FROM CREW
INFO: value : Î?ανάÏ?ηÏ? Î?έγγοÏ?
FINE: SELECT IDCREW, NAME FROM CREW
FINE: SELECT IDCREW, NAME FROM CREW

The funny thing is that when this mojibake is displayed it actually lets me press the "Add" button a second time and the method addCrewMember() is called resulting in a constraint violation (which I expect).

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • Your question is confusing. The converter seems to work fine. What's the current state of the problem? That Eclipse fails to properly print characters outside CP1252 / ISO-8859-1 range to the stdout? – BalusC Feb 15 '14 at 09:08
  • @BalusC: No no - when I press "Add" I see the message on the page `Cannot convert ÎανάÏÎ·Ï ÎÎ­Î³Î³Î¿Ï to Crew`. I just posted the printouts in eclipse - when I have selected the name with unicode chars and hit "Add" first time it runs it says `value : ??????? ??????` - which is alright (I know I have to set the console in UTF-8 in eclipse), does the same on alt tab out of the browser window - and second time it shows `value : Î?ανάÏ?ηÏ? Î?έγγοÏ?` - here it fails to convert. – Mr_and_Mrs_D Feb 15 '14 at 14:29
  • @BalusC: why it runs two time and who feeds it the (already garbled) input the second time is the issue – Mr_and_Mrs_D Feb 15 '14 at 14:38
  • But the symptom of `?`s being printed suggests that Eclipse isn't at all instructed to use UTF-8 in stdout. – BalusC Feb 17 '14 at 10:04
  • @BalusC: yes - and it turns out it's not simple either cause there is no run configuration to set. But eclipse is not the problem - the ??? is unicode read correctly and displayed badly but why the `Î?ανάÏ?ηÏ? Î?έγγοÏ?` ? - I solved it (see my answer), but I don't understand why - it is a lifecycle something I miss. I'd accept an answer – Mr_and_Mrs_D Feb 17 '14 at 11:20

1 Answers1

0

Changing the button to this:

<h:commandButton value="Add" action="#{movieController.addCrewMember}">
    <f:ajax execute="@form" render="@form" />
</h:commandButton>

solved it.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361