Question

Is it not possible to get the values for doubleselect from database in struts2? I am trying for the past 3 days but of no use.

The code that I tried is below.

<%@taglib uri="/struts-tags" prefix="s" %>
<%@ page import="java.sql.*" %>
<h2>Double Select tag example in struts2 framework</h2>
<s:form action="double">
    <%
        Connection con = null;
        PreparedStatement ps = null;
        try {
            String url = "jdbc:sqlserver://xxx.xxx.xx.xxx:1089;user=abc;password=abc;databaseName=xyz";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(url);
            String sql1 = "SELECT distinct pr_iden FROM ip_dailyeff";
            ps = con.prepareStatement(sql1);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                String id = rs.getString("pr_iden");
                String sql2 = "SELECT pe_logn FROM ip_dailyeff where pr_iden=+id";
                ps = con.prepareStatement(sql2);
                ResultSet rs1 = ps.executeQuery();
                while (rs1.next()) {
                    String logn = rs.getString("pe_logn");
    %>
                    <s:set name="nameList" value="#{'<%=id %>': {'<%=logn %>',},}" />
    <%
                }
            }
        } catch(Exception e) {
            out.println(e);
        } finally {
            con.close();
        }
    %>
    <s:doubleselect
        label="Person"
        name="Project"
        list="#nameList.keySet()"
        doubleName="Person"
        doubleList="#nameList[top]"/>
    <s:submit value="submit" name="submit"/>
</s:form>

The error that I get is

The requested list key #nameList.keySet() could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

I understand from this that passing the resultset values directly to #nameList.keySet() does not result in an array. I am not sure how to proceed from this.

Was it helpful?

Solution

I have performed the task of doubleselect using jquery and 2 different jsp pages.

My index.jsp has

<script>
    function showTeam() {
        var e = document.getElementById("project");
        var str = e.options[e.selectedIndex].value;
        if (str == "") {
            //if null the it'll show blank space
            document.getElementById("javaquery").innerHTML = "";
            return;
        }
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("javaquery").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "data.jsp?q=" + str);
        xmlhttp.send();

    }
</script>

<div class="menu">
    <h2>Select the Member and period</h2>
    <form>
        <table class="veryx">

            <tr>
                <td>Project</td>
                <td><select class="veryx" name="project" id="project">
                        <option>--Select--</option>
                        <jsp:scriptlet>try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager
                    .getConnection("jdbc:sqlserver://xxx.xxx.xx.xxx:1089;user=abc;password=abc;databaseName=xyz");
            PreparedStatement ps = con
                    .prepareStatement("SELECT pr_iden,pr_name FROM ip_project order by pr_name asc");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {</jsp:scriptlet>
                        <option value="<%=rs.getString(1)%>"
                            <%=(rs.getString(1).equals(session
                        .getAttribute("prjct"))) ? "selected" : ""%>>
                            <%=rs.getString(2)%></option>
                        <jsp:scriptlet>}
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }</jsp:scriptlet>
                </select></td>
                <td>
                    <button type="button" class="go" onclick="showTeam()">Go</button>
                </td>
            </tr>
        </table>
    </form>
    <div class="javaquery" id="javaquery"></div>
</div>

my data.jsp has

<%
    session.setAttribute("prjct", request.getParameter("q"));
%>

<form action="effort" method="post">
        <table class="veryx">
            <tfoot>
                <tr>
                    <td colspan="2" align="center" height=""><input type="submit"
                        class="veryxButton" value="Submit" /></td>
                </tr>
            </tfoot>

            <tr>
                <td>Team Member</td>
                <td><select class="veryx" name="logn">
                        <%
                            String q = request.getParameter("q");
                            try {
                                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                                Connection con = DriverManager
                                        .getConnection("jdbc:sqlserver://192.168.12.185:1089;user=iPlanClient;password=iPlanClient;databaseName=iPlan_VERYX");
                                PreparedStatement ps = con
                                        .prepareStatement("SELECT distinct pe_logn FROM ip_dailyeff where pr_iden="
                                                + q);
                                ResultSet rs = ps.executeQuery();
                                while (rs.next()) {
                        %>
                        <option><%=rs.getString(1)%></option>
                        <%
                            }
                        %>
                </select></td>
            </tr>
            <tr>
                <td>From</td>
                <td><input type="date" name="fromDate" /></td>
            </tr>
            <tr>
                <td>To</td>
                <td><input type="date" name="toDate" /></td>
                <jsp:scriptlet>
                con.close();
            } catch (Exception e) {
                out.println(e);
            }</jsp:scriptlet>
            </tr>
        </table>

    </form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top