0

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

  • Here is a post with a similar issue: [How to call parameterized method from JSP using JSTL/EL](https://stackoverflow.com/questions/7121303/how-to-call-parameterized-method-from-jsp-using-jstl-el). – prasad_ Nov 15 '18 at 08:20
  • @prasad_ all answer there have some issues :( –  Nov 15 '18 at 09:25
  • Yes, but one can try some work around. Are you allowed to change some code in the `com.touchpoint.Dao.Outlet` Java class? – prasad_ Nov 15 '18 at 09:50
  • @prasad_ yes for sure –  Nov 15 '18 at 09:56

1 Answers1

0

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.

prasad_
  • 12,755
  • 2
  • 24
  • 36
  • hey i have a doubt –  Nov 15 '18 at 10:19
  • Please ask any clarifications freely. – prasad_ Nov 15 '18 at 10:20
  • i have updated my java class look its giving me list [jayanagar,malleshwaram,kolar] i just want to populate them in a select option tag as a dropdown –  Nov 15 '18 at 10:26
  • Here is one: [Filling HTML – prasad_ Nov 15 '18 at 10:29
  • In your code: The bean's _getter_ method signature must be of format _getProperty_. So, `getoutlet(String idDB)` need to be `getOutlet(String idDB)`. – prasad_ Nov 15 '18 at 10:32
  • can you pass jayanagar,malleshwaram and,kolar in array list as static values and please show me in a dropdown in my jsp using jstl, can you modify my code with static values rather then query....actually i am very new to this so facing trouble like hell :( –  Nov 15 '18 at 10:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183699/discussion-between-dheeraj-kumar-and-prasad). –  Nov 15 '18 at 10:39
  • I have added the JSP code to show the select-option drop down list box. This will show the values A, B, C, Z in a HTML drop-down list. – prasad_ Nov 15 '18 at 10:44
  • The solution is based on the code posted in the answer, above (with some differences). Please see the _discussion on the chat_ session. – prasad_ Nov 15 '18 at 13:53
  • hey i have a doubt in your answer if you will be free please let me know –  Nov 20 '18 at 07:54
  • Yes, we can chat sometime. – prasad_ Nov 21 '18 at 01:38
  • i have found the solution :) –  Nov 21 '18 at 05:02