Frage

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).

War es hilfreich?

Lösung

Changing the button to this:

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

solved it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top