Link to home
Start Free TrialLog in
Avatar of bonza
bonza

asked on

JSP/Struts URGENT question!!!!

Hi

I am working on a registration page for a site using Jakarta Struts and JSP.  Before I get the user to fill out all their registration details, I want them to select from a list of personal trainers, which I will then allocate the chosen one to them, and then take them to the registration page.

Currently, as soon as they click to register, I have extracted an arraylist of personal trainers (name, photo, biog etc) and display them all on a jsp page with a submit button each.  However, I am having trouble in making each separate submit button belong to that specific personal trainer id.  I want the user to be able to click on whichever submit button belongs to the personal trainer they are using, and then get taken to the next page, which is the registration page, which will then populate a text box with the personal trainers id.

I am not sure whether I should set that personal trainer as a session constant, or if I can
simply pass the personal trainer's id over to the registration page as a parameter?????

This is EXTREMELY urgent, so I would really appreciate some help as soon as possible.  I have been going round and round in circles but nothing seems to work.

Thanks Bonza
Avatar of kotan
kotan
Flag of Malaysia image

You can have a hidden field to store personal id and then for each submit button, add onclick event to call javascript function to assign personal id to that hidden field.
Below is the sample code.

<script>
function chooseThis(id)
{
  document.myform.personalId.value = id;
  document.myform.submit();
}
</script>

<form name="myform">
<input type="hidden" name="personalId">

<!-- personal id: 1-->
<input type="button" name="submit" onclick="chooseThis(1)">

<!--  personal id: 2 -->
<input type="button" name="submit" onclick="chooseThis(2)">

</form>
Avatar of bonza
bonza

ASKER

Hi there - thanks for that code - great.  Only thing is that even though I am sending the trainerId through on an iterate (and am even doing a bean:write to test the values), the
first trainerId is the only id that gets sent.  

ie:  At present I am testing with 2 trainers - one with an id of 2 and another with an id of 7.
When I do a <bean:write/> as below, it prints out 2, and 7.  However, when I set the trainerId as you have shown, and then do a print out from the action class (which gets the parameter trainerId), both printouts are 2, not 2 then 7, so obviously only the first gets set, which means both submit buttons are set at a value of 2.

Below is the code I am using.

Thanks Bonza.



<html:form action="/returnReg">

<logic:iterate id="trainers" name="trainers">
      <html:hidden name="trainers" property="trainerId"/>
     <bean:write name="trainers" property="trainerId"/>
     <html:submit property="trainerId" onclick="chooseThis(trainerId)">Select Trainer <bean:write name="trainers" property="trainerId"/></html:submit>
</logic:iterate>

</html:form>
      
<html:javascript formName="trainerForm"
        dynamicJavascript="true"
         staticJavascript="false"/>
             
<script>
function chooseThis(id)
{
  document.trainerForm.trainerId.value = id;
  document.trainerForm.submit();
}
</script>
Avatar of TimYates
Don't set your submit button to a property, else it will try to set that property with the text value of the button...

     <html:submit onclick="chooseThis(trainerId)">Select Trainer <bean:write name="trainers" property="trainerId"/></html:submit>
Avatar of bonza

ASKER

Hi, I have taken the property off as suggested, however the same problem exists, I choose the submit button which should have the value of 7, and still it gives me 2???

Thanks Bonza
1. you shouldn't use trainers for both name and id in logic:iterate
2. you should define the hidden field one, not in the loop.

solutions A: (change your.trainder.bean.ClassName to the real full class of your trainer bean)
<html:hidden property="trainerId"/>
<logic:iterate id="trainer" name="trainers" type="your.trainder.bean.ClassName">
     <bean:write name="trainer" property="trainerId"/>
     <html:submit property="trainerId" onclick='<%="chooseThis(" + trainer.getTrainerId() + ")"'>Select Trainer <bean:write name="trainer" property="trainerId"/></html:submit>
</logic:iterate>

solution B: (if you don't know the class name for you trainer)
<html:hidden property="trainerId"/>
<logic:iterate id="trainer" name="trainers">
     <bean:define id="trainerId" name="trainer" property="trainerId"/>
     <bean:write name="trainerId"/>
     <html:submit property="trainerId" onclick='<%="chooseThis(" + trainerId + ")"' >Select Trainer <bean:write name="trainerId"/></html:submit>

sorry, there are problems in the myu last post. should be:
solutions A: (change your.trainder.bean.ClassName to the real full class of your trainer bean)
<html:hidden property="trainerId"/>
<logic:iterate id="trainer" name="trainers" type="your.trainder.bean.ClassName">
     <bean:write name="trainer" property="trainerId"/>
     <html:submit property="trainerId" onclick='<%="chooseThis(" + trainer.getTrainerId() + ")"%>'>Select Trainer <bean:write name="trainer" property="trainerId"/></html:submit>
</logic:iterate>

solution B: (if you don't know the class name for you trainer)
<html:hidden property="trainerId"/>
<logic:iterate id="trainer" name="trainers">
     <bean:define id="trainerId" name="trainer" property="trainerId"/>
     <bean:write name="trainerId"/>
     <html:submit property="trainerId" onclick='<%="chooseThis(" + trainerId + ")"%>' >Select Trainer <bean:write name="trainerId"/></html:submit>
Avatar of bonza

ASKER

Hi,

Thanks for that - I used your code, however now when I use my action class to see what trainerId is using the following

String trainerId = request.getParameter("trainerId");
System.out.println("trainer id " + trainerId);

What prints out is whatever is actually ON the submit button, ie: if I don't define a value it gives me submit, or if I have Select Trainer 2 this is what it gives me, so rather than the actual trainerId being sent, it is the value of the submit button!

I have done a <%System.out.println(trainerId);%> straight after the calling of the chooseThis function to test the value of trainerId, and it prints out exactly as I need (ie: 2 then 7), so I can't understand why these values are not being sent, instead of the value of the submit button???  Surely when I'm calling the onclick even of the submit button, it should send through the trainerId as the code looks like it will??????
<html:submit property="trainerId" onclick='<%="chooseThis(" + trainerId + ")"%>' >Select Trainer <bean:write name="trainerId"/></html:submit>

As TimYates suggested, take out the property

<html:submit onclick='<%="chooseThis(" + trainerId + ")"%>' >Select Trainer <bean:write name="trainerId"/></html:submit>
Avatar of bonza

ASKER

HI, once I take that property out, I am now getting an error saying that document.trainerForm.trainerId is null or not an object.  I am not familiar with javascript, so am not sure on this part.  Is there any other way of passing the trainerId to the register page via: Struts without using javascript?  Otherwise, does anyone have any ideas on why I would be getting that error now, after removing the property?

<script>
function chooseThis(id)
{
  document.trainerForm.trainerId.value = id;
  document.trainerForm.submit();
}
</script>
do you have this  
  <html:hidden property="trainerId"/>
in the form?

what is your form name? is it "trainerForm"?

if you only have 1 form only in the page, you can just write
function chooseThis(id)
{
  document.forms[0].trainerId.value = id;
  document.forms[0].submit();
}
Avatar of bonza

ASKER

Yes, I have a hidden property of trainerId - that is not a problem, I have tested printing the trainerId each iteration, and it prints them out as it should ie: 2, then 7, it is just when I try to use the javascript method that problems start happening.
Not sure why but the error saying document.trainerForm.trainerId is null has stopped happening - however the value being passed is now 0.  I implemented your above advice, and it is still saying 0.  

I have the following defined at the top of my JSP page, as it is doing an action called returnReg in the Struts config.
<html:form action="/returnReg">

I have the form defined at the bottom of the JSP page as follows so the form name is trainerForm:

<html:javascript formName="trainerForm"
        dynamicJavascript="true"
         staticJavascript="false"/>


Is there any other way in Struts to pass a parameter???
Avatar of bonza

ASKER

Bonza here again!!

I have just discovered that the correct paramater is getting sent, because when I include the following:

<html:text property="trainerId"/>   the output is 0,

yet when I put
<bean:write name="trainer" property="trainerId"/>  the output is 2 or 7 as it should be???

So, 0 is getting sent, so that part is working, so that's not where the problem is!!!!!!!!!!!!!!  So at least now I can send the value - but does anyone have any idea why this would happen??

Avatar of bonza

ASKER

Bonza AGAIN!!

I have changed the hidden box from

<html:hidden property="trainerId"/>

<html:hidden name="trainer" property="trainerId"/>

and now, the value is posting through as 2, however both the 7 and the 2 are going through as 2, so 2 must be sticking????
if this doesn't work for you (please copy and paste), post your code.

<html:hidden property="trainerId"/>
<logic:iterate id="trainer" name="trainers">
     <bean:define id="trainerId" name="trainer" property="trainerId"/>
     <bean:write name="trainerId"/>
     <html:submit onclick='<%="chooseThis(" + trainerId + ")"%>' >Select Trainer <bean:write name="trainerId"/></html:submit>
ASKER CERTIFIED SOLUTION
Avatar of kennethxu
kennethxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bonza

ASKER

Kennethxu YOU ARE FANTASTIC and have MADE MY DAY!!!!!

YES!!! It is working - THANK YOU SOOOOOOOOOOOOOO MUCH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
YEEEEEEEEEEEEEEEE HAAAAAAAAAAAAA!!!!!!!!!  (Sorry bout this over-enthusiasm but
I am SOOO happy!!!!!!!!!)
It's my pleasure and happy to know that your problem is solved.
BTW, I think kotan should have get 50% of points since he initially gave the idea.

kotan, I have posted a 250 points here, please come and get it, thanks.
https://www.experts-exchange.com/questions/20784258/points-for-kotan.html
Avatar of bonza

ASKER

Ooh yes, sorry about that Kotan!!  (I was too over-excited!!)
Thanks for settling that Kennethxu!
And thank you both for all your help - AWESOME!!