|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Working with multiple records (JSP)
Hello, I'm new here. *grins hopefully*
I'm building a web site for work (first time) that uses JSP calling Java classes to provide database access, and there's just one problem I can't work out. If a search returns multiple records, how do I select only ne to be updated/deleted? Currently, if I put my jsp:setProperty tag above the For loop that prints the records, I can only peg the first record returned. If I put it inside the loop, nothing. I know the problem is the value of my "currentRecord" bean is not being changed, but how do I fix that? (The udpate/delete does work on the first record, along with everyting else... Finally). Any suggestions? (If anyone wants to know how I got this far, I'd be happy to help). OConnor |
|
#2
|
|||
|
|||
|
We need more information. Can you post all the code that is relevant to the page you are talking about?
|
|
#3
|
|||
|
|||
|
Here it is without the exessive HTML. The method names are should be self-explanatory.
<head> ***Usual stuff*** <jsp:useBean class="nfmc.ContactList" id="contactsList" scope="page" /> <jsp:useBean class="nfmc.Contact" id="currentContact" scope="page" /> <jsp:useBean class="nfmc.Contact" id="searchedContacts" scope="session" /> </head> <body> <form method="POST" action="admContacts.jsp"> <%-- If doing update, don't zonc values --%> <% if (request.getParameter("B4") == null) { %> <% searchedContacts.clearContact(); %> <jsp:setProperty name="searchedContacts" property="type" /> <jsp:setProperty name="searchedContacts" property="contact" /> <jsp:setProperty name="searchedContacts" property="company" /> <% } %> **The form code is long because it involves loading database values into list boxes. Needless to say it has widgets named type, contact & contact name.*** </form> <% if (request.getParameter("B1") != null) { %> <%-- Do search --%> <% contactsList.search(searchedContacts); %> <%-- Tell user how many records were found --%> <p align="center"><font face="Arial" size="3"><%= contactsList.sendRecordsFound() %> </font></p> <% } %> <table border="0" width="100%"> <form method="POST" action="admContacts.jsp"> <jsp:setProperty name="currentContact" property="*" /> <% for (int i = 0; i < contactsList.getTotalRecord(); i++) { %> <% if (contactsList.getTotalRecord() > 0) { %> <% currentContact = contactsList.getCurrent(); %> <% } %> <tr> <td width="20%"><font face="Arial" size="3">Contact's Name:</font></td> <td width="20%"><input type="text" name="contact" value="<%=currentContact.getContact() %>"></td> <td width="20%"><font face="Arial" size="3">Title:</font></td> <td width="20%"><input type="text" name="division" value="<%=currentContact.getDivision() %>"></td> </tr> ***etc,etc*** </tr> <% contactsList.nextRecord(); %> <td width="100%" colspan="4"><%-- just a record seperator--%> ----------------------------------------------- --------------------------------------------------------------------------------</font></td> </tr> <% } %> <%-- End for loop --%> </table> <%-- Only show update & delete buttons if a search has been done --%> <% if (contactsList.getTotalRecord() > 0) { %> <input type="submit" name="B4" value="Update Record"><input type="submit" value="Delete Record" name="B5"> <% } %> </font> <%-- Update button code --%> <% if (request.getParameter("B4") != null) { %> <font face="Arial"><%= currentContact.update() %></font><br> <font face="Arial"><%= currentContact.sendError() %></font> <% } %> </form> ***Basically same thing for the delete button*** </body> |
|
#4
|
|||
|
|||
|
OK, this appears to be a problem with using jsp tags and scriptlets (<% %>) tags together. Jsp tags are supposed to "save" us from dreaded programming code in our jsp's, but sometimes it just confuses us to whats going on.
I made a sample page and looked at the code that was generated by the web server. What appears to be happening is kind of a scope issue. When you use a jsp:useBean tag, the server creates a bean of the specified type as a local variable in the service method. It also puts a copy of that in the context that you specify. in this case it puts a copy of the bean in it's pageContext. So in essence we have 2 references to the same bean. Now, when you call this line of code: Code:
<jsp:setProperty name="currentContact" property="*" /> What it is doing is retrieving the bean from the pageContext and setting that objects values to the values passed in the request. Fine. However, when you call this line of code: Code:
<% currentContact = contactsList.getCurrent(); %> you are changing the local method variable reference to something other than what is stored in the pageContext. So, if you put the setProperty property="*" in the for loop, all it is changing is the values of the bean stored in the pageContext, not the local variable you are dereferencing in your loop. Whew. I hope you followed that, cause it can get confusing. Personally, what I would do is remove the jsp:useBean tag and instantiate the bean yourself the 'normal' way. I.E., Code:
Instead of:
******
<jsp:useBean class="nfmc.ContactList" id="contactsList" scope="page" />
<jsp:useBean class="nfmc.Contact" id="currentContact" scope="page" />
<jsp:useBean class="nfmc.Contact" id="searchedContacts" scope="session" />
Use something like this:
******
<%
nfmc.ContactList contactList = new nfmc.ContactList();
nfmc.Contact currentContact = new nfmc.Contact();
nfmc.Contact searchedContacts = (nfmc.Contact)session.getAttribute("searchedContacts");
if(searchedContacts == null) {
searchedContacts = new nfmc.Contact();
}
%>
Try removing all the jsp:setProperty tags and setting the properties yourself and see if this doesnt start working. I have attached the jsp source as created by the web server for anyone interested. |
|
#5
|
|||
|
|||
|
Thank you very much. I shall foward this on to the programmer here at work who told me to do it this way, haha.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Working with multiple records (JSP) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|