[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 624
  • Last Modified:

Struts : How to display data on JSP page ?

Hi there,
I have a bean object , whose methods corresponds to a SQL Query's column names. So each row of the query is a rown-bean. And this bean is 'add' to a Collection.

How do I retrieve the value of this collection and display it in the JSP page with the nested:iterate tag ??



The method in my Helper Class that forms the collection is :


     public java.util.Collection executeQueryReturnCollection(Object target, java.util.Map mapSQL, java.sql.Connection conn)
     {
        java.util.ArrayList list = new java.util.ArrayList();
        try
        {
             java.sql.PreparedStatement pstmt = createPreparedStatement(mapSQL, conn);
             if (pstmt == null)
                return null;
             java.sql.ResultSet rs   = pstmt.executeQuery();
             java.sql.ResultSetMetaData metaData = rs.getMetaData();
             int cols = metaData.getColumnCount();

             Class factory = target.getClass();
             while (rs.next())
             {
                java.util.HashMap map = new java.util.HashMap(cols, 1);
                for (int i = 1; i <= cols; i++)
                {
                   System.out.println("i> " + i + ":" + metaData.getColumnName(i) + " : " + rs.getString(i));
                   map.put(metaData.getColumnName(i), rs.getString(i));
                }
                try
                {
                   Object bean = factory.newInstance();
                   BeanUtils.populate(bean, map);
                   list.add(bean);
                }
                catch (Exception e)
                {
                   AppLogger.log("com.ecomkid.services.Utility.executeQueryReturnCollection : *BEAN*MAP* " + e.toString());
                }
             }

             metaData = null;
             try { rs.close(); } catch(Exception e) {}
             try { pstmt.close();} catch(Exception e) {}
             rs    = null;
             pstmt = null;
        }
        catch (Exception e)
        {
             AppLogger.log("com.ecomkid.services.Utility.executeQueryReturnCollection() *BEAN*MAP* " + e.toString());
             e.printStackTrace();
        }
        finally
        {
             return ( (java.util.Collection) list);
        }


     }


And the other class is :


public class UserViews extends Object implements java.io.Serializable
{

     private java.util.Collection colMainQueryLinksScreen    = null;

     public java.util.Collection getMainQueryLinksScreen()
     {
        return this.colMainQueryLinksScreen;
     }

     public void setMainQueryLinksScreen(String username, java.sql.Connection conn)
     {
        String strSQL = "";

        strSQL = " SELECT GK_REF_QUERY_TYPES.QUERYTYPE_ID QUERYTYPE_ID, GK_REF_QUERY_TYPES.QUERYTYPE QUERYTYPE, " +
        " GK_REF_QUERY_TYPES.DESCRIPTION DESCRIPTION, " +
        " GK_REF_QUERY_TYPES.FORWARD_JSP_NAME FORWARD_JSP_NAME FROM GK_REF_QUERY_TYPES WHERE GK_REF_QUERY_TYPES.QUERYTYPE_ID IN " +
        " (SELECT GK_REF_GROUP_QUERY_REL.QUERYTYPE_ID FROM GK_REF_GROUP_QUERY_REL WHERE GK_REF_GROUP_QUERY_REL.GROUP_ID  IN  " +
        " (SELECT GK_REF_GROUPS.GROUP_ID FROM GK_REF_GROUPS WHERE GK_REF_GROUPS.GROUP_ID IN " +
        " (SELECT GK_REF_USERS_GROUPS.GROUP_ID FROM GK_REF_USERS_GROUPS WHERE GK_REF_USERS_GROUPS.USER_ID = " +
        " (SELECT GK_REF_USERS.USER_ID FROM GK_REF_USERS WHERE GK_REF_USERS.ORG_ID = UPPER(TRIM(?))))))";

        java.util.Map mapSQL = new java.util.HashMap();
        mapSQL.put("SQL", strSQL);

        String[][] strArr = new String[1][2];
        strArr[0][0] = "1";
        strArr[0][1] = username;
        mapSQL.put("PSTMT.STRING", strArr);

        this.colMainQueryLinksScreen = ((new Utility()).executeQueryReturnCollection ( (new QueryUserViewsMainQueryLinksScreen()) ,mapSQL, conn));
     }

}



and the class QueryUserViewsMainQueryLinksScreen is :




public class QueryUserViewsMainQueryLinksScreen
{
     private String QUERYTYPE_ID = "";
     private String QUERYTYPE = "";
     private String DESCRIPTION = "";
     private String FORWARD_JSP_NAME = "";

     public String getQUERYTYPE_ID()
     {
        return this.QUERYTYPE_ID;
     }
     public void setQUERYTYPE_ID(String text)
     {
        this.QUERYTYPE_ID = text;
     }


     public String getQUERYTYPE()
     {
        return this.QUERYTYPE;
     }
     public void setQUERYTYPE(String text)
     {
        this.QUERYTYPE = text;
     }



     public String getDESCRIPTION()
     {
        return this.DESCRIPTION;
     }
     public void setDESCRIPTION(String text)
     {
        this.DESCRIPTION = text;
     }



     public String getFORWARD_JSP_NAME()
     {
        return this.FORWARD_JSP_NAME;
     }
     public void setFORWARD_JSP_NAME(String text)
     {
        this.FORWARD_JSP_NAME = text;
     }



}


And in my the ActionForm execute part the relevant part in

.
.
.

             UserViews uView = new UserViews();
             uView.setMainQueryLinksScreen(username, conn);
             request.setAttribute("MainScreenDisplayObjects",uView);


...


And in my jsp page, I want to retrieve the query resulset. I am using the following tags there :

      <bean:define id="mainScreenView" name="MainScreenDisplayObjects" type="com....UserViews"/>

      <nested:iterate id="queryLinksRows" name="mainScreenView" property="mainQueryLinksScreen">
            <nested:iterate id="queryLinksColumns" name="queryLinksRows" indexId="iCnt" type="java.lang.String">
                              Employee: <nested:write name="QUERYTYPE"/>
                              E-mail:   <nested:write name="DESCRIPTION"/>
            </nested:iterate>
      </nested:iterate>



OK, Now what I am doing wrong ??? I know I have missed something here..




0
ecomkid
Asked:
ecomkid
  • 5
  • 5
  • 2
  • +1
1 Solution
 
ecomkidAuthor Commented:
just wanted to add that, by using the conventional JSP scripting, I can see the data fetched by the query :

<%

      UserViews uView = (UserViews) request.getAttribute("MainScreenDisplayObjects");
      java.util.Collection coll = uView.getMainQueryLinksScreen();
      java.util.Iterator itr =  coll.iterator() ;
      while(itr.hasNext())
      {
         QueryUserViewsMainQueryLinksScreen qry = (QueryUserViewsMainQueryLinksScreen) itr.next();
         out.println(qry.getQUERYTYPE_ID() + " -- " + qry.getQUERYTYPE() + " -- " + qry.getDESCRIPTION() + " -- " + qry.getFORWARD_JSP_NAME() + "<br>" );
      }
%>
0
 
TimYatesCommented:
DOes:

     <nested:iterate id="queryLinksRows" name="mainScreenView" property="mainQueryLinksScreen">
         Employee: <nested:write name="queryLinksRows" property="QUERYTYPE"/>
         E-mail:   <nested:write name="queryLinksRows" property="DESCRIPTION"/>
     </nested:iterate>

work?
0
 
ecomkidAuthor Commented:
no :(. it doesnt work. any other suggestions ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TimYatesCommented:
Do you get any errors?
0
 
ecomkidAuthor Commented:
no errors, tim. it just displayed blank

like the below line :

 Employee:   E-mail:  



but, as I said before, the JSp scriptlet tag did produce the result . but not the struts tags :(
0
 
TimYatesCommented:
Ok, what about:

     <nested:iterate id="qry" name="mainScreenView" property="mainQueryLinksScreen">
         <%= ((QueryUserViewsMainQueryLinksScreen)qry).getQUERYTYPE_ID() %> --
         <%= ((QueryUserViewsMainQueryLinksScreen)qry).getQUERYTYPE() %> --
         <%= ((QueryUserViewsMainQueryLinksScreen)qry).getDESCRIPTION() %> --
         <%= ((QueryUserViewsMainQueryLinksScreen)qry).getFORWARD_JSP_NAME() %>
     </nested:iterate>

What does that do?
0
 
ecomkidAuthor Commented:
no..

Since fork is true, ignoring compiler setting.
    [javac] Compiling 1 source file
    [javac] Since fork is true, ignoring compiler setting.
    [javac] C:\tomcat\work\Standalone\localhost\gatekeeper\common\main_jsp.java:199: cannot resolve symbol
    [javac] symbol  : variable qry
    [javac] location: class org.apache.jsp.main_jsp
    [javac]       out.print( ((QueryUserViewsMainQueryLinksScreen)qry).getQUERYTYPE_ID() );
0
 
vikraman_bCommented:
Hi check my code..
FOr me its working fine..i used dispatched action...


//My DAO class
      public List findByCountry(int countryID) throws SQLException, Exception {
            String sql = SELECT+" and loc=?";
            List values = new ArrayList();

            values.add(new Integer(countryID));

            return readXXX(sql, values);
      }

      public List readxxx(String sql, List args) throws SQLException,
                  Exception {
            StringBuffer buffer = new StringBuffer();
            Connection conn = null;
            PreparedStatement stmt = null;
            ResultSet rs = null;
            List objects = new ArrayList();

            buffer.append(sql);

            
            try {
                  conn = //get the connection   getConnection();
                  stmt = conn.prepareStatement(buffer.toString());
                  if (args.size() > 0) {
                        for (int i = 0; i < args.size(); i++) {
                              stmt.setObject(i + 1, args.get(i));
                        }
                  }
                  logger.debug("SQL:" + buffer.toString());

                  rs = stmt.executeQuery();
                  objects = new ArrayList();
                  while (rs.next()) {
                        Loc object = new Loc();//Loc is a class which contains all ur database columns set/get

methods.

                        object.setXXX(rs.getLong("id"));
                        object.setYYY(rs.getString("name"));
                        objects.add(object);

                  }
            } catch (SQLException ex) {
                  
                  throw ex;
            } catch (Exception ex) {
                  
                  throw ex;
            } finally {
                  //close connection            
            }
            return objects;
      }

//My action class

      public ActionForward scrollPage(ActionMapping mapping, ActionForm form,
                  HttpServletRequest request, HttpServletResponse response)
                  throws Exception {

            String target = "success";
            if(!isLoggedIn(request)){
              target="Login";
              return mapping.findForward(target);
        }
            String action = (String) request.getParameter("submit");
            PageController page = (PageController) getSessionObject(request,
                        "dataCollection");

            if (!page.getPageValueHander().isEmpty()
                        && page.getPageValueHander() != null) {
                  page.getPage(action);
            }

            return mapping.findForward(target);

      }

//My jsp

<%@ page language="java"%>


<html:form action="ee.do" name="Formname" type="com.cc.Form" >       
      <table class="tablelist" width="700" border="0" cellpadding="0" cellspacing="1">
            <tr class="tdheader">
                  <td width=100 style="color:white;font-weight: bold;">Employee</td>
                  <td width=100 style="color:white;font-weight: bold;">mail</td>
                  <td width=120 style="color:white;font-weight: bold;">Name</td>                  
                  <td width=100 style="color:white;font-weight: bold;">Manager</td>
            </tr>
            <logic:iterate id="locationRow"  name="dataCollection" property="pageValueHander" >      
            
                  <%
                   i ++;
                  
                   if(i % 2 == 0) {
                  %>
                  <tr class="tddetailodd">
                  <%
                    }
                        else {
                  %>
                  <tr class="tddetaileven">
                  <%
                    }
                  %>      
                  <html:hidden name="locRow" property="locC"/>
                  <html:hidden name="locRow" property="locM"/>
                  <td><bean:write name="locRow" property="Employee"/></td>      
                  <td>
                              <a  href="ee.do?method=edit&location=<bean:write name="locationRow"

property="mail"/>">
                              <bean:write name="locationRow" property="objid" /></a>
                  </td>
                  <td><bean:write name="locationRow" property="Name"/></td>      
                  <td><bean:write name="locationRow" property="Manager"/></td>      
            </logic:iterate>
</html:form >

just follow like this..i did along with pagenation.
Regards

0
 
vikraman_bCommented:
SELECT = ur select query string
0
 
ecomkidAuthor Commented:
Ok guys, I have solved the issue. sorry for all the trouble


Its so silly !!! Been struggling with it for over 4 days, and just realised that I havent included the

      <taglib>
            <taglib-uri>struts-nested.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
      </taglib>

in the web.xml !!!!!!!

So no matter what I did with the nested tags in the JSP page, the compiler just ignored it !
0
 
TimYatesCommented:
LOL :-D
0
 
TimYatesCommented:
That would explain why you got the

[javac] C:\tomcat\work\Standalone\localhost\gatekeeper\common\main_jsp.java:199: cannot resolve symbol
   
with the 2nd method I proposed...  the logic:iterate was completely ignored ;-)

It's all too obvious now ;-)

Tim
0
 
PAQ_ManCommented:
PAQed with points refunded (200)

PAQ_Man
Community Support Moderator
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now