0

I have a probleme in SelectOneMenu in my accueil.jsf page

xhtml page:

<h:outputText value="Code type opération : " />
<h:selectOneMenu id="op" value="#{mainController.operation}">                       
    <f:selectItem itemLabel="Choisir Code Opération "
        itemValue="#{null}" noSelectionOption="true" />
    <f:selectItems value="#{mainController.listeTypeOperations}" />
    <f:converter converterId="TypopConverter" />
</h:selectOneMenu>

Converter class:

public class TypOpConverter implements Converter {
    @Override
    public Object getAsObject(FacesContext facesContext, 
                              UIComponent component, 
                              String value) {
        ClassPathXmlApplicationContext context = 
            new ClassPathXmlApplicationContext("spring-web.xml");
        BanqueService banqueService = 
            (BanqueService)context.getBean("banqueService");
        TypeOperation typop;
        if (value == null) {
            typop = null;
        } else {
            Long id = new Long(value);

            typop = banqueService.getTypeOperationParId(id);
            if (typop == null) {
                return null;
            }
        }
        return typop;
    }

    @Override
    public String getAsString(FacesContext facesContext,
                              UIComponent component,
                              Object value) { 
        if (value == null) {
            return "";
        }
        if (value instanceof String) {
            return (String) value;
        }
        return String.valueOf(((TypeOperation) value).getId());
    }
}

My managed Bean and Controller

@ManagedBean(name="mainController")
@ViewScoped
public class MainController implements Serializable{

private TypeOperation operation;
// get and set

public List<TypeOperation>  getListeTypeOperations(){.....}

The converter run and i did some print to show the string and object but the selected value can't be stored into #{mainController.operation} i think

the error:

j_idt7:op: Validation Error: Value is not valid 
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • your selectItems should be like `` – Sam Sep 01 '16 at 21:38
  • @ElSam i did not understand what should i put in var, itemValue, itemLabel. Can u tell me exactly ? – Amine.Kenna Sep 01 '16 at 22:32
  • it's just a variable name to iterate the list, what you should modify is the `itemLabel="#{type.someProperty}"` put some string property like `itemLabel="#{type.name}"` – Sam Sep 01 '16 at 23:01
  • think of it like a forEach loop , `for(TypeOperation type : listeTypeOperations ) {System.out.print(type.name);}` – Sam Sep 01 '16 at 23:03
  • Next time when you get an error, use it as [search keyword](https://www.google.com/search?q=Validation+Error%3A+Value+is+not+valid). – BalusC Sep 02 '16 at 07:26
  • SOLVED. I had to override equals() and hashcode() in the model class – Amine.Kenna Sep 03 '16 at 15:09
  • right click ---> source generate override and hashcode by id – Amine.Kenna Sep 03 '16 at 15:10

0 Answers0