JSF SelectOneMenu und der Converter

Headhunter_X

Mitglied
Hallo,

ich hab die Suche schon benutzt, nur leider hilft mir das bei meinem Problem nicht weiter.
Ich bekomme,wenn ich etwas aus dem SelectOneMenu auswähle, in dem Formular nach dem Klick auf den Button immer die Fehlermeldung:form:application: Validierungs-Fehler: Wert nicht gültig


Ich habe ein SelectOneMenu:
Code:
<h:form id="form">
     <h:selectOneMenu id="application" value="#{ApplicationController.applications}"  converter="StringConverter">
               <f:selectItems value="#{ApplicationController.applications}" />
      </h:selectOneMenu>
</h:form>

Dann den Controller:
Java:
public class ApplicationController
{
    private String test ="";
    private Application application;
    private ArrayList<SelectItem> allRoles = new ArrayList<SelectItem>();

    public ApplicationController()
    {
        allRoles.add(new SelectItem("Wählen Sie"));
        allRoles.add(new SelectItem("Option1"));
        allRoles.add(new SelectItem("Option2"));
    }

    public void setApplication(Application application)
    {
        this.application = application;
    }

    public ArrayList<SelectItem> getApplications()
    {
        return this.allRoles;
    }

    public Application getApplication()
    {
        return application;
    }
}

Nächste Klasse
Java:
public class Application
{
    private String application;

    public Application(String application)
    {
        this.application = application;
    }

    public void setApplication(String application)
    {
        this.application = application;
    }

    public String getApplication()
    {
        return this.application;
    }
}

Und mein Converter der wohl noch Probleme macht:
Java:
public class StringConverter implements Converter
{
    public StringConverter()
    {}

    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {
        SelectItem item=null;
        

        try{
            item = new SelectItem(value.toString());
            System.out.println("Durchlaufen: " +item.getValue());
        }catch( UnsupportedOperationException e){ System.out.println("GetAsObject failed." +e); }

        return item;
    }

    public String getAsString(FacesContext context, UIComponent component, Object value)
    {
        String myString="";

        try{
            myString = value.toString();

        }catch( UnsupportedOperationException    e){ System.out.println("GetAsString failed." +e); }

        return myString;
    }

Ich hab gerade ein Brett vorm Kopf und wäre über Hilfe dankbar.

Gruß
 
Okay .. versuchen wir es mal 🙂
1.
Java:
        allRoles.add(new SelectItem(-1,"Wählen Sie"));
        allRoles.add(new SelectItem(1,"Option1"));
        allRoles.add(new SelectItem(2,"Option2"));

Warum?
Damit du nachher das gewählte Element wieder über die ID ansprechen kannst.
2.
[XML]
<h:selectOneMenu id="application" value="#{ApplicationController.selectedOptionsId}">
<f:selectItems value="#{ApplicationController.allRoles}" />
</h:selectOneMenu>
[/XML]
Warum?
Erstmal müssen die selectItems natürlich auch vom Typ (JAVA) her SelectItem sein.
Was du als value angibst macht überhaupt keinen Sinn.
Der ganze code des ApplicationController scheint völlig sinnfrei zu sein. Bzw. so merkwürdig benannt, dass man nicht wirklich weiß was du damit machen willst.
Java:
public class ApplicationController
{
    private String test ="";
    private ArrayList<SelectItem> allRoles = new ArrayList<SelectItem>();
 
    public ApplicationController()
    {
        allRoles.add(new SelectItem(-1,"Wählen Sie"));
        allRoles.add(new SelectItem(1,"Option1"));
        allRoles.add(new SelectItem(2,"Option2"));
    }
 
   // getter und setter noch bauen....
   public int selectedOptionsId=-1;
  
   public ArrayList<SelectItem> getAllRoles() {
        return this.allRoles;
   }
 
   // setter noch erstellen
  public setAllRoles (ArrayList<SelectItem> allRoles) {
      this.allRolles = allRoles; 
  }
}

Was du auch immer mit dem Converter erreichen willst..... lass es lieber 😀
Der Converter macht z.B. aus "1" = "Ja" oder sowas und dient lediglich der Darstellung

Gruß
 
Hallo,

danke schon Mal für deine Hilfe.

Jetzt merk ich erst, dass ich folgendes nicht erwähnt habe 😳
Der Sinn des Converters war, dass ich ja das ausgewählte Element aus dem SelectOneMenu speichern und zum Test im Textfeld ausgeben möchte.

Nachtrag: Super jetzt klappts. Danke für deine Hilfe. War doch einfacher als gedacht.



Gruß
 
Zuletzt bearbeitet:

Zurück
Oben