我读过一些关于同一组件的质量检查,但我觉得我错过了一些东西,因为我落后了一步。
当我使用 primefaces 自动完成组件时,我什至无法打开页面。
它的片段是:
<p:autoComplete value="#{indirizzoCtrl.selectedCodiceNazione}"
completeMethod="#{indirizzoCtrl.completeNazione}"
var="nazione" itemLabel="#{nazione.nome}"
itemValue="#{nazione.codiceNazione}" />
Nazione 是一个 Pojo 类,其中CodiceNazione
and Nome are
两个字符串字段(肯定有 getter 和 setter)。completeNazione
是 ManagedBean 上的一个方法,它返回List<Nazione>
。
看BalusC的解释here https://stackoverflow.com/a/7653775/333223,在我看来,我不需要涉及任何转换器,因为 itemValue 和 value 属性都映射到字符串属性。
无论如何,当我打开包含此自动完成片段的页面时,它会因以下错误而崩溃:
javax.el.PropertyNotFoundException: /Cliente/Indirizzo.xhtml @23,56 itemValue="#{nazione.codiceNazione}": itemValue="#{nazione.codiceNazione}": Property 'codiceNazione' not found on type java.lang.String
为什么会发生这种情况?我实在是看不懂了。 CompleteNazione 方法还没有被调用,所以它不应该知道任何Nazione
然而。
它出什么问题了?
Edited:按照建议,我尝试添加转换器,但仍然遇到相同的错误。
这是我的转换器:
public class NazioneConverter implements Converter {
final static Logger log = Logger.getLogger(NazioneConverter.class);
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value.trim().equals("")) {
return null;
} else {
try {
IndirizzoRepository ir = new IndirizzoRepository();
List<Nazione> nazioni = ir.getNazioneByName(value);
if (nazioni.size()==1) return nazioni.get(0);
else throw new Exception();
} catch (Exception e) {
String msg = "Errore di conversione";
log.error(msg, e);
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, "Non è una nazione conosciuta"));
}
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null || value.equals("")) {
return "";
} else {
return String.valueOf(((Nazione) value).getNome());
}
}
}
现在视图中的组件如下所示:
<p:autoComplete value="#{indirizzoCtrl.indirizzo.nazione.codiceNazione}"
completeMethod="#{indirizzoCtrl.completeNazione}"
var="nazione" itemLabel="#{nazione.nome}" converter="#{nazioneConverter}"
itemValue="#{nazione.codiceNazione}" forceSelection="true" />
但还是不行。转换器甚至没有被调用:我在 faces-config.xml 文件中注册了它。
我还尝试了 itemValue="#{nazione}" 就像 primefaces 展示中那样,但问题变成了ItemLabel
属性,映射到nazione.nome
。
我究竟做错了什么?