p:selectManyMenu报告验证错误(非法选择选项)

时间:2014-01-04 13:32:26

标签: jsf primefaces

我使用的是primefaces的p:selectManyMenu。提交值时,它始终报告验证错误Illegal selection options。你能帮忙解决这个问题吗?

JSF代码:

 <h:outputLabel for="scenario" value="GivenStories:" /> 
    <p:selectManyMenu id="givenstoryselect" value="#{anotherDynaFormController.selectedGivenStory}" converter="scenarioConverter" var="g" style="width:500px;height:70px;align:left;" showCheckbox="true" > 
    <f:selectItems value="#{anotherDynaFormController.givenStorys}" var="scenario" itemLabel="#{scenario.name}" itemValue="#{scenario}"  /> 
    <p:column>#{g.name}</p:column>
    </p:selectManyMenu>

Bean类:

//converter    
@FacesConverter(value="scenarioConverter")
public class ScenarioConverter implements Converter {       
    public static List<Scenario> scenarioDB = new ArrayList<Scenario>();
    private ScenarioDao scenarioDao = new ScenarioDao();

    public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
        if (submittedValue.trim().equals("")) {
            return null;
        } else {
            try {
                int number = Integer.parseInt(submittedValue);
                scenarioDB = scenarioDao.findAll();
                for (Scenario p : scenarioDB) {
                    if (p.getId() == number) {
                        return p;
                    }
                }

            } catch(NumberFormatException exception) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid scenario"));
            }
        }        
        return null;
    }  
    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
        if (value == null) {
            return null;
        } else {
            return String.valueOf(((Scenario) value).getId());
        }
    }
}
@ManagedBean  
@ViewScoped  
public class AnotherDynaFormController extends SuperBean implements Serializable  {        
    private List<Scenario> givenStorys;
    private List<Meta> selectedMetas;
    private List<Scenario> selectedGivenStory;
    private List<ProjectTree> parents;

public DynaFormModel getModel() { 
  givenStorys = scenarioDao.findAll();
  ...
}
...
}

2 个答案:

答案 0 :(得分:0)

试试这个通用转换器,看看是否还有问题。

import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.WeakHashMap;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "entityConverter")
public class EntityConverter implements Converter {

    private static Map<Object, String> entities = new WeakHashMap<Object, String>();

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object entity) {
        synchronized (entities) {
            if (!entities.containsKey(entity)) {
                String uuid = UUID.randomUUID().toString();
                entities.put(entity, uuid);
                return uuid;
            } else {
                return entities.get(entity);
            }
        }
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String uuid) {
        for (Entry<Object, String> entry : entities.entrySet()) {
            if (entry.getValue().equals(uuid)) {
                return entry.getKey();
            }
        }
        return null;
    }

}

答案 1 :(得分:0)

非常感谢。我找到了这个问题的原因。我应该覆盖此对象中的equals和hashcode方法。然后就行了。

@Override
    public boolean equals(Object obj) {
            if(obj == null)
                    return false;
            if(!(obj instanceof Scenario))
                    return false;
            return ((Scenario)obj).getId().equals(this.id);
    }

    @Override
    public int hashCode() {
        int hash = 1;
        return hash * 31 + name.hashCode();
    }