Search code examples
javajstl

C:for:each not getting the values if a pass a parameter to the List method


I have a java class where i have a method and that method is taking some parameter like

below is my java code which has a getoutlet method

public List<String> getoutlet(String idDB) throws ClassNotFoundException, SQLException {
    List<String> list = new ArrayList<String>();
    con = DBConnection.createConnection();
    statement = con.createStatement();
    String sqlOutlet="select CUSTOMERDESCRIPTOR from ecustomer where CUSTOMERIDENTIFIER in(select CUSTOMERIDENTIFIER from mt_distributrol where mt_distributr_vcdistributrcode = '"+idDB+"')";

    try {

        ResultSet resultSet = statement.executeQuery(sqlOutlet);
        while (resultSet.next()) {
            list.add(resultSet.getString("CUSTOMERDESCRIPTOR"));

        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return list;
}

if i put system.out.print it gives me the list [jayanagar,malleshwaram,kolar,ol1]

but it is throwing an error while called from the UI (jsp) using c:for:each

i am passing idDB as a parameter now in a list i am getting like this

[jayanagar,malleshwaram,kolar,ol1]

now i am calling this method by c:for:each in my jsp to populate this in a select option but the problem is it is throwing error

like my c:for:each code is

    <jsp:useBean id="obj" class="com.touchpoint.Dao.Outlet" scope="page" />
<select id="all" name="outlet">
                 <option>ALL</option> 
                <c:forEach var="item" items="${obj.outlet}">
                    <option>${item}</option>
                </c:forEach>
            </select> 

if iam not passing any parameter to my getoutlet method then its working fine but now i have to pass some parameter as per requirnment,The error it is showing is '${obj.outlet}' Property 'outlet' not found on type com.touchpoint.Dao.Outlet Outlet is my java class name so anyone out there please help me out

this is my java class

 public class Outlet {
    Connection con = null;
    Statement statement = null;
    ResultSet resultSet = null;

    public List<String> getoutlet(String idDB) throws ClassNotFoundException, SQLException {
        List<String> list = new ArrayList<String>();
        con = DBConnection.createConnection();
        statement = con.createStatement();
//      String sqlOutlet="select CUSTOMERDESCRIPTOR from ecustomer where CUSTOMERIDENTIFIER in(select CUSTOMERIDENTIFIER from mt_distributrol where mt_distributr_vcdistributrcode = '"+idDB+"')";
        String sqlOutlet="select * from ecustomer')";
/*System.out.println(idDB);*/
        try {

            ResultSet resultSet = statement.executeQuery(sqlOutlet);
            while (resultSet.next()) {
                list.add(resultSet.getString("CUSTOMERDESCRIPTOR"));

            }



        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }



}

it is returning me [jayanagar,malleshwaram,kolar] now i want to show this list in my select option dropdown


Solution

  • Here is an approach (a work around).

    The Bean Class:

    public class MyBean {
    
        private String paramInfo;
    
        public void setParamInfo(String p) {
            paramInfo = p;
        }
    
        public String getParamInfo() {
            return paramInfo;
        }
    
        public List<String> getStuff(String param) {
            param = paramInfo; // substitute the value here
            List <String> list1 = Arrays.asList("A", "B", "C");
            List<String> list2 = new ArrayList<>();
            list2.addAll(list1);
            list2.add(param); // use the param value here
            return list2;
        }
    }
    


    NOTE: I have not changed the signature of the method getStuff(String param); this still accepts a String parameter. I have added a new bean property which is a substitute for the parameter value. Note the usage of the bean's new property value paramInfo.


    The JSP Page:

    <BODY>
        <h2>Testing JSP</h2>
        <jsp:useBean id="obj1" class="app.MyBean" scope="page" />
        <c:set var="param1" value="Z"/>
    
        <jsp:setProperty name="obj1" property="paramInfo" value="${param1}" />
        <c:set var="list1" value="${obj1.getStuff('')}"/>
    
        <c:forEach var="item" items="${list1}">
            <br/>ITEM: ${item} 
        </c:forEach>
    
        <br><br>
        <select name="list">
            <c:forEach items="${list1}" var="item">
                <option value="${item}"><c:out value="${item}" /></option>
            </c:forEach>
        </select>
    </BODY>
    


    NOTE: The bean's method signature still remains same. The value passed to the method is a blank String. The tag <jsp:setProperty ... actually sets the parameter value.

    This way you get to use the parameter value in the paramInfo property within the getStuff method.


    The Result:

    ITEM: A 
    ITEM: B 
    ITEM: C 
    ITEM: Z
    


    NOTE: One can change the doStuff method in the bean class not to accept the parameter, with this approach of setting the parameter separately.