This is where I generate the URL (e.g. it will look like: 'CodeSnipetById.action?codeid=5')
<table> <% Session s = HibernateUtil.getSessionFactory().getCurrentSession(); s.beginTransaction(); CodeSnipet snipet = new CodeSnipet(); Criteria c = s.createCriteria(CodeSnipet.class); c.addOrder(Order.desc("idcodesnipets")).setMaxResults(20); out.print("<tr>"); out.print("<td>Name</td>"); out.print("<td>File</td>"); out.print("<td>Date</td>"); out.print("</tr>"); List l = c.list(); for(Integer i = 0; i < l.size(); i++) { snipet = (CodeSnipet)l.get(i); String linkBegin = "<a href='CodeSnipetById.action?codeid="; String linkEnd = "'>"; out.print("<tr>"); out.print("<td>" + linkBegin + snipet.getIdcodesnipets().toString() + linkEnd + snipet.getCodetitle() + "</a></td>"); out.print("<td>" + snipet.getCodefilename() + "</td>"); out.print("<td>" + snipet.getStamp().toString() + "</td>"); out.print("</tr>"); } %> </table>So then within your struts.xml file your action will look like:
<!-- this goes to show snipet, just with a GET request parameter! -->
<action name='CodeSnipetById'>
<result>/hibernateTesting/showSnipet.jsp</result>
</action>
Finally, in the JSP page that handles this, you will pull the data in via request.getParameter("stringparametername"). If your request is not set it will return 'null' so you can test for a null value like you would test with isset($_GET['whatever']) in PHP. Here is an example of handling the get request and passing into hibernate to pull up some records:
Session s = HibernateUtil.getSessionFactory().getCurrentSession(); s.beginTransaction(); CodeSnipet snipet = new CodeSnipet(); if(request.getParameter("codeid") == null) { Criteria c = s.createCriteria(CodeSnipet.class); c.addOrder(Order.desc("idcodesnipets")).setMaxResults(1); List l = c.list(); snipet = (CodeSnipet)l.get(0); } else { try { Integer myId = Integer.parseInt(request.getParameter("codeid")); snipet = (CodeSnipet) s.get(CodeSnipet.class, myId); } catch (Error e) { Integer myId = 1; snipet = (CodeSnipet) s.get(CodeSnipet.class, myId); } }
Our 'snipet' object is the Bean we pull the data from.
No comments:
Post a Comment