Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 验证失败时PF自动完成显示错误标签_Jsf_Primefaces - Fatal编程技术网

Jsf 验证失败时PF自动完成显示错误标签

Jsf 验证失败时PF自动完成显示错误标签,jsf,primefaces,Jsf,Primefaces,我对primefaces的自动完成有一种奇怪的行为 当我提交一个没有验证错误的表单,或者其他表单字段有错误时,该组件工作得非常好。但是,如果自动完成验证失败,标签将替换为item@Id字段 我怀疑我使用的转换器有问题。转换器所做的基本上是获取实体的@Id值,并将实际实体插入组件的属性映射中,其中@Id值作为其键 这是我的xhtml: <p:autoComplete id=

我对primefaces的自动完成有一种奇怪的行为

当我提交一个没有验证错误的表单,或者其他表单字段有错误时,该组件工作得非常好。但是,如果自动完成验证失败,标签将替换为item
@Id
字段

我怀疑我使用的转换器有问题。转换器所做的基本上是获取实体的@Id值,并将实际实体插入组件的属性映射中,其中@Id值作为其键

这是我的xhtml:

                            <p:autoComplete
                                id="autoComp"
                                value="#{action.timeTable}"
                                completeMethod="#{action.timeTables}"
                                var="tt"
                                itemLabel="#{tt.description}"
                                itemValue="#{tt}"
                                dropdown="true"
                                minQueryLength="3"
                                forceSelection="true"
                                converter="entityConverter"
                                size="30"
                                required="true"
                                maxResults="10">

                                <f:validator validatorId="customValidator" />

                            </p:autoComplete>

这是我的转换器代码:

@FacesConverter("entityConverter")
public class EntityConverter implements Converter {
@Override
public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
    if (value != null) {
        return component.getAttributes().get(value);
    }

    return null;
}

@Override
public String getAsString(FacesContext ctx, UIComponent component, Object obj) {

    if (obj instanceof String) {
        return obj.toString();
    }

    if (obj != null) {
        String id;

        try {
            id = this.getId(getClazz(ctx, component), obj);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new ConverterException("msg");
        }

        id = id.trim();

        component.getAttributes().put(id, getClazz(ctx, component).cast(obj));

        return id;

    }
    return null;
}

private Class<?> getClazz(FacesContext facesContext, UIComponent component) {
    //get entity's class
}

private String getId(Class<?> clazz, Object obj) throws NoSuchFieldException, IllegalAccessException {
    // get entity's ID value
}

}
@FacesConverter(“entityConverter”)
公共类EntityConverter实现转换器{
@凌驾
公共对象getAsObject(FacesContext ctx、UIComponent、字符串值){
if(值!=null){
返回component.getAttributes().get(值);
}
返回null;
}
@凌驾
公共字符串getAsString(FacesContext ctx、UIComponent、Object obj){
if(字符串的obj实例){
返回obj.toString();
}
如果(obj!=null){
字符串id;
试一试{
id=this.getId(getClazz(ctx,component),obj);
}捕获(NoSuchFieldException | IllegalacessException e){
抛出新的ConverterException(“msg”);
}
id=id.trim();
component.getAttributes().put(id,getClazz(ctx,component.cast(obj));
返回id;
}
返回null;
}
私有类getClazz(FacesContext FacesContext,UIComponent){
//获取实体的类
}
私有字符串getId(类clazz,对象obj)抛出NoSuchFieldException、IllegalAccessException{
//获取实体的ID值
}
}

您的转换器是正确的,因为验证失败不会触发值的转换,因此p:autoComplete在验证失败时使用提交的值来显示,而不是之前设置的正确值。我们通过更改
自动补全器
来修复此问题,方法是替换

if(ac.isValid()) {
    requestMap.put(var, ac.getValue());
    itemLabel = ac.getItemLabel();
}
else {
    Object submittedValue = ac.getSubmittedValue();
    itemLabel = (submittedValue == null) ? null : String.valueOf(submittedValue);

    if(itemLabel == null && ac.getValue() != null) {
        requestMap.put(var, ac.getValue());
        itemLabel = ac.getItemLabel();
    }
}

if(ac.isValid()) {
    requestMap.put(var, ac.getValue());
    itemLabel = ac.getItemLabel();
}
else {
    // Display label of previously set value if validation fails
    itemLabel = ac.getItemLabel();

    if(itemLabel == null && ac.getValue() != null) {
        requestMap.put(var, ac.getValue());
        itemLabel = ac.getItemLabel();
    }
}