// @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)));
}
}
在转换器失败的其中一个项目上:
无法转换ÎÎÎÎÎÎÎÎÏηÏÎÎÎÎÎÎÎÎÎÎÎÎÏÏ到船员
对于其余的转换过程(但不是验证,这是另一件事我必须在Crew中定义equals()
)。
值正确显示 - 我应该调整转换器还是问题可能更深(在服务器配置中)?
我正在使用glassfish 4
编辑 - 形式:
<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 :当我点击“添加”按钮时会显示消息 - 当我从表单中选择alt-tab(因此onBlur被触发)时,转换有效!示例输出(在eclipse控制台上)
在alt标签上:
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
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
有趣的是,当显示这个mojibake时,它实际上让我再次按下“添加”按钮,方法addCrewMember()
被称为,导致违反约束(我期待)。
答案 0 :(得分:0)
将按钮更改为:
<h:commandButton value="Add" action="#{movieController.addCrewMember}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
解决了它。