-1

I need to create a program that accesses a database and passes the values to a dropdown in JSF. I am trying to figure out how that can be done. I Got it working for the 1st dropdown, but the second dropdown is tricky, as it needs a input parameter to work. Here is my JSF code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
<head>    
<title>Dropdown List</title>
</head>
<body>
    <select id="mySelect" onchange="myFunction()">
        <h:outputText value="#{helloWorld.getResultSet1()}" escape="false" />    
        <f:attribute name="action" value="1" />
    </select>
    <br />
    <select id="mySelect1">
    </select>
    <script>
        var x1 = document.getElementById("mySelect1");    
        x1.style.display = 'none';
        function myFunction() {
            x1.style.display = 'block';
            x1.innerHtml = '<h:outputText value="#{helloWorld.getResultSet2()}" escape="false" />';
        }
        document.getElementById('myForm:hidden2').value = new_value;
    </script>
</body>
</html>

And here is my java code:

public String id = "1";

    public void print(String name) {
        System.out.println(name);
    }
    public String getResultSet2(String name) {
        String toReturn = "";

        try {
            rs = st.executeQuery("SELECT * FROM CS_EXAMS where name ="+ name);
            System.out.println(name);
            while (rs.next()) {
                toReturn = toReturn + "<option>" + rs.getString("exam_name") + "</option>";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return toReturn;
    }

    public String getResultSet1() {
        String toReturn = "";

        try {
            rs = st.executeQuery("SELECT * FROM CS_EXAMS");
            System.out.println("name");
            while (rs.next()) {
                toReturn = toReturn + "<option>" + rs.getString("exam_name") + "</option>";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return toReturn;

    }
Jasper de Vries
  • 13,693
  • 6
  • 54
  • 86
  • Especially the first part of the accepted answer is relevant: _"This is really not the way to make dropdowns in JSF. I would recommend to read some JSF beginners guides..."_ It makes me wonder what you were using before, why you decided to start using jsf and what tutorial you used.... – Kukeltje May 01 '17 at 09:05

2 Answers2

2

This is really not the way to make dropdowns in JSF. I would recommend to read some JSF beginners guides, and even HTML beginners guides might help (for example, there is no <form> in your code).

To start of, JSF uses component to create the user interface. There is no need for you to create HTML select and option elements. If you use selectOneMenu, it will take care of creating the HTML for you. More importantly, you will also get stuff like value binding, validation, change listeners, etcetera out of the box.

So try to create a selectOneMenu by reading:

If you need POJO objects in your dropdown, please read:

Now, to solve the problem of updating the second dropdown based on the selection of the first dropdown, please read:

Community
  • 1
  • 1
Jasper de Vries
  • 13,693
  • 6
  • 54
  • 86
  • The approach in the original question even prevents lots of 'automatic 'JSF features from functioning correctly. You cannot have bean-validation and put the errors on the correct corresponding UI inputs (instead of the hidden ones). It introduces CRSF that JSF by default prevents and more... For this reason I downvoted the question and commented why in one of the tree other related questions by the same poster on why I did this. – Kukeltje May 01 '17 at 09:45
0

Strings should be between 'name' :

rs = st.executeQuery("SELECT * FROM CS_EXAMS where name = '" + name + "'");

But you have to use PreparedStatement instead :

try (PreparedStatement ps = connection.prepareStatement(
        "SELECT * FROM CS_EXAMS where name = ?"")) {
    ps.setString(1, name));

    rs = ps.executeQuery();

    while (rs.next()) {
         toReturn = toReturn + "<option>" + rs.getString("exam_name") + "</option>";
    }
    //...complete your code
}
Graham
  • 6,577
  • 17
  • 55
  • 76
YCF_L
  • 49,027
  • 13
  • 75
  • 115