0

I'm working with JSF and want to save a choose from a dropdown, but it doesn't save it, what can I do?

xhtml:

<h:form>
    <h:selectOneMenu value="#{spielController.ausgewaehlteKategorie}"  >
        <f:selectItems value="#{spielController.alleKategorien}"/>
    </h:selectOneMenu>
</h:form>
<br/>
<h:form>
    <h:commandButton action="#{spielController.ladeFrage()}" value="Spielen"/>
</h:form> 

BackingBean:

@Named(value = "spielController")
@SessionScoped
public class SpielController implements Serializable {

    private String ausgewaehlteKategorie;

    private ArrayList<String> alleKategorien = new ArrayList<>();

    public SpielController() throws SQLException {

        for (String kategorie : kdao.getKategorien()) {
            alleKategorien.add(kategorie);
        }
    }


    public String getAusgewaehlteKategorie() {
        return ausgewaehlteKategorie;
    }

    public void setAusgewaehlteKategorie(String ausgewaehlteKategorie) {
        this.ausgewaehlteKategorie = ausgewaehlteKategorie;
    }


    public ArrayList<String> getAlleKategorien() {
        return alleKategorien;
    }

    public void setAlleKategorien(ArrayList<String> alleKategorien) {
        this.alleKategorien = alleKategorien;
    }

}

I get the things from a DAO-class and it works, but when I press the button, it doesn't work because the choose I made above wasn't saved

  • sorry but it's not a problem with the button, its a problem with the , it doesn't use the setter method, i don't know why – albionspahija Mar 22 '20 at 17:39
  • Please read the title... 'or the value not setter updated.'... A selectOneMenu has a value... that needs to be set via a setter... Then read #1 in the duplicate and the links in it... Solution is really there. It **is** a duplicate – Kukeltje Mar 22 '20 at 18:51
  • From #2 of abovelinked duplicate: *"You can use UIForm components in parallel, but they won't process each other during submit. "*. This is your problem. Simply put related form elements within the same form. Note that this is not specific to JSF. This is specific to HTML. So you would have had exactly the same problem when using any other web MVC framework which generates HTML output. – BalusC Mar 22 '20 at 19:24

1 Answers1

0

Move

<h:commandButton action="#{spielController.ladeFrage()}" value="Spielen"/>

to h:form

<h:form>
    <h:selectOneMenu value="#{spielController.ausgewaehlteKategorie}"  >
        <f:selectItems value="#{spielController.alleKategorien}"/>
    </h:selectOneMenu>
    <br/>
    <h:commandButton action="#{spielController.ladeFrage()}" value="Spielen"/>
</h:form> 

This is the reason why your setter not invoked.

abosancic
  • 1,776
  • 1
  • 15
  • 21
  • Like the link in #1 in the suggested duplicate points to. https://stackoverflow.com/questions/3681123/how-to-get-hinputtext-values-from-gui-xhtml-into-java-class-jsf-bean – Kukeltje Mar 22 '20 at 18:54