Solved

URGENT JSP/Struts/Java Question

Posted on 2003-11-14
15
520 Views
Last Modified: 2010-04-01
Hi

I am working on a Diary Page for a client seeing a personal trainer.
I display a table of repeating values which shows line after line of exercise
details (name of exercise, expected reps, expected sets, expected weight lifted) and also on the line are textboxes for a client to fill in - (actual reps, actual sets, actual weight lifted).
These values are all displayed directly from the database and the textboxes holding the actual values are shown initially as 0 before the client completes them all and clicks submit to write their actual results to the database.

As I create each row I am storing a hidden value called workoutExerciseId and this is printing out correctly when I test with a text box, however when I write the values back, all the values in the first line are all writing the same to every other line in the database.  (Eg: I have lines of workoutExerciseId 1,2,3,4,5,6 - then every line is written as the values I input for id 1 so obviously in my code this first number is sticking).

What is the best way to attack this (other than putting a submit button on every line)?  

Thanks Bonza.


0
Comment
Question by:bonza
  • 6
  • 5
  • 4
15 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9751859
How are you identifying the text box contents within your JSP/Servlet when the user presses submit?
0
 

Author Comment

by:bonza
ID: 9751880
The following is the code that puts the table rows on my form.  (ie: I'm setting each property as I iterate through).  Thanks for responding so fast - fantastic!


<logic:iterate id="workout" name="workouts">
  <tr>
    <td><html:text name="workout" property="workoutExerciseId"/></td>
      <%temp[count]= "+workoutExerciseId+";%>
   
    <td width="14%"><b><bean:write name="workout" property="exerciseName"/></b></td>
    <td width="9%"><b><bean:write name="workout" property="plannedSet"/></b></td>
    <td width="9%"><b><bean:write name="workout" property="plannedRep"/></b></td>
    <td width="9%"><b><bean:write name="workout" property="plannedIntensity"/>%</b></td>
    <td width="9%"><b><bean:write name="workout" property="plannedWeight"/>kg</b></td>
    <td width="4%"><b></b></td>
    <td width="9%"><b><html:text size="5" name="workout" property="actualSet"/></b></td>
    <td width="9%"><b><html:text size="5" name="workout" property="actualRep"/></b></td>
    <td width="10%"><b><html:text size="5" name="workout" property="actualIntensity"/>%</b></td>
    <td width="18%"><b><html:text size="5" name="workout" property="actualWeight"/>kg</b></td>
      <%count++;%>
   </tr>
</logic:iterate>

0
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 100 total points
ID: 9751988
>> Thanks for responding so fast - fantastic!

Whoa!  Hold on there ;-)  I'm no struts guru.  I'm just asking for clarification at the moment to see if I can help :-)

OK.  It looks like you'll be receiving (in your servlet/JSP), a number of fields from this form.  Each row will repeat the text field "name" value, so you'll receive many values with the same parameter name.

This means that if you use something like:  request.getParameter("actualSet"), you will only get the first value from the data that was sent from the client.  In order to get all the values, you need to use request.getParameterValues("actualSet").

Now for the bad news.  As I understand it, the order in which the data is returned is not necessarily related to the order in which the fields appear on the page :-(  I've found in the past that in situations like this, data tends to be in reverse order.

Again, AFAIK, this means that even if you do this:

String[] exIds = request.getParameterValues("workoutExerciseId");
String[] sets = request.getParameterValues("actualsSet");

I'm not sure that the order within the arrays will match up :-(

The way I've worked around this before is to append the ID value for each row to the end of the item name.  In your case, this would be

actualSet1, actualRep1, actualIntesity1, actualWeight1
actualSet2, ....

Then I strip the number off the end (within the servlet) in order to identify which row the parameter relates to.

There may be (or should I say, there probably is) a better way to do this, either using the struts framework or generally.  Because I've only need this once and the above idea worked OK for me, I haven't looked into this further yet.

I'll do a bit of research, but don't hold your breath for me.  Hopefully kennethxu will appear soon ;-)
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 15

Expert Comment

by:jimmack
ID: 9752018
Sorry, that description about my solution was probably a bit misleading.  What I should have said is:

I use a loop and append the id to the requested parameter.  A quick example:

String[][] exValues = new String[numberOfExercises][4];
for (int i = 0; i < numberOfExcercises; i++)
{
    exValues[i][0] = request.getParameter("actualSet" + i);
    exValues[i][1] = request.getParameter("actualRep" + i);
    exValues[i][2] = request.getParameter("actualIntensity" + i);
    exValues[i][3] = request.getParameter("actualWeight" + i);
}

Note that when generating the original page, you would need to append the count to the text field name, eg:

    <td width="9%"><b><html:text size="5" name="workout" property="actualSet" + count/></b></td>


0
 

Author Comment

by:bonza
ID: 9752101
Hi

Thanks for that!  I think instead of using a count, I will need to actually append the workout_exercise_id to the end of each of the actual properties.  I have been trying to do this but can't get the syntax right.

eg: <td width="9%"><b><html:text size="5" name="workout" property="actualSet"+workoutExerciseId/></b></td>

etc, but nothing is working.
Also, because Struts is expecting to be able to set a variable of the name property, and it can't find one for a property of actualSet1 etc, it won't compile. - Does this mean I need to set up a double array in the jsp page, to match up to the double array I create in the java code as you've shown above?

Wow, I hope that's clear what I"m saying!!  I really appreciate your input!

0
 
LVL 15

Expert Comment

by:jimmack
ID: 9752167
Hmm.  My lack of struts knowledge is going to get in the way now :-(

What is the error when you compile?  If it is because you are doing:

 property="actualSet"+workoutExerciseId

within this line, is it possible to simply assign that to a String and use that instead.  eg:

<% String actSetString = "actualSet" + workoutExcerciseId; %>

<td width="9%"><b><html:text size="5" name="workout" property=actSetString/></b></td>
0
 

Author Comment

by:bonza
ID: 9752269
Hi!

Yes, that would be the way round it - however, I have realised I can't use that way, because Struts expects anything that is set to a property, to have a getter and setter associated with that property (ie: whatever all the values of actSetString are, there needs to be getters and setters for everyone, so I can't use this unfortunately).

I think I may have to feed all the values into a double array on the actual jsp page, as you did above, ie:  my code is:

      <%temp[count][0]= "+workoutExerciseId+";%>  
      <%temp[count][1]= "+actualSet+";%>  
      <%temp[count][2]= "+actualRep+";%>  
      <%temp[count][3]= "+actualIntensity+";%>
      <%temp[count][4]= "+actualWeight+";%>  
      <%count++;%>

and then if I can submit this temp array, I should be able to access this double array from my java action class.

I am trying to submit this temp array, using a javascript function I have used in the past, but when I used this before, it was expecting one id value, and I am getting run-time errors, saying "Expected ']'" - which I don't understand, because I used the exact same code for a submit button on another page, but just replaced the variable with temp.  (Could this be because the javascript function is not expecting a double array? - I am not familiar with javascript so don't know how to change this to suit a double array).  I really think if I can get that syntax correct, I could get the values I need over to my java class!

The code I am using on my submit is:
<p align="left"><html:submit onclick='<%="chooseThis(" + temp + ")"%>' >Submit</html:submit></p>

<script>
function chooseThis(id)
{
  document.forms[0].temp.value = id;
  document.forms[0].submit();
}
</script>

0
 

Author Comment

by:bonza
ID: 9752583
Hi, me again.

I have realised perhaps I am sending temp as a string, and it is a double array in the submit code above, however if I use

<p align="left"><html:submit onclick='<%="chooseThis(temp)"%>' >Submit</html:submit></p>

it does not recognise the variable temp?
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9752981
how is your struts form bean looks like?
in your action class, after the form is submited. you'll get an array of the id and an array for each property.

you should loop through the id array and update db with corresponding array element of properties.

if you have further enquires, please show us your action form bean and action class.
0
 

Author Comment

by:bonza
ID: 9753005
I am not even getting anything to the form class, because the <html:submit> tag is not working.  That is my main problem, I cannot even set the variable temp in my form class because I have an error in my jsp page code, so that when I use temp in my action class there are no values populated.

Below is the code for my diaryForm bean for the temp array that I should be posting to in my above code:  (I don't know much javascript so unsure what the error is:)


bean for DiaryForm  details:

      public String[][] getTemp()
      {
            return temp;
      }
      public void setTemp(String[][] temp)
      {
            this.temp=temp;
      }

Code for action class:  - the print does not get executed as there is no temp.

String[][] temp = f.getTemp();
for (int i=0;i<workouts.size();i++)
{                                                
Workout workout = (Workout)workouts.get(i);            
System.out.println("here  ? " + temp[i]);
}

Thanks Bonza


0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9753014
why are u using temp? your original approach is correct.
<logic:iterate id="workout" name="workouts">
  <tr>
    <td><html:text name="workout" property="workoutExerciseId"/></td>
     <%temp[count]= "+workoutExerciseId+";%>
   
    <td width="14%"><b><bean:write name="workout" property="exerciseName"/></b></td>
    <td width="9%"><b><bean:write name="workout" property="plannedSet"/></b></td>
    <td width="9%"><b><bean:write name="workout" property="plannedRep"/></b></td>
    <td width="9%"><b><bean:write name="workout" property="plannedIntensity"/>%</b></td>
    <td width="9%"><b><bean:write name="workout" property="plannedWeight"/>kg</b></td>
    <td width="4%"><b></b></td>
    <td width="9%"><b><html:text size="5" name="workout" property="actualSet"/></b></td>
    <td width="9%"><b><html:text size="5" name="workout" property="actualRep"/></b></td>
    <td width="10%"><b><html:text size="5" name="workout" property="actualIntensity"/>%</b></td>
    <td width="18%"><b><html:text size="5" name="workout" property="actualWeight"/>kg</b></td>
     <%count++;%>
   </tr>
</logic:iterate>
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 400 total points
ID: 9753027
and you should have properties in form bean as (I assumed datatype)
int[] workoutExerciseId
String[] actualSet
String[] actualRep
double[] actualIntensity
int[] actualWeight

then the first rows is workoutExerciseId[0]...actualWeight[0], 2nd rows is workoutExerciseId[1]...actualWeight[1], and etc, etc.
0
 

Author Comment

by:bonza
ID: 9753174
WOW!!!!!!!!  I just tried, and all my work for 2 days is FINISHED - YEE HAAAAAAAA!!!!!!!!!

I never realised that setting up an array in the form bean would mean the values automatically
fed into that - wow - awesome!!!

THANKYOU soooo much!!
(Shall I split the points?? )
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9754042
glat do know your problem is solved :-)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9754500
Thank you ;-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to group array of objects ? 5 69
servlet init param and contect param 3 67
How can I   ajax html table  rows? 20 78
going to wrong jsp page 2 33
Starting your own business is always a daunting process, and for most people it is brand new experience. Avoid the common pitfalls by following these tips to start on the road to success.
There's a lot of hype surrounding blockchain technology. Here's how it works and some of the novel ways it' s now being used - including for data protection.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question