Go Premium for a chance to win a PS4. Enter to Win

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

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
0
bonza
Asked:
bonza
  • 9
  • 5
  • 3
  • +1
1 Solution
 
kotanCommented:
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>
0
 
bonzaAuthor Commented:
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>
0
 
TimYatesCommented:
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>
0
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!

 
bonzaAuthor Commented:
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
0
 
kennethxuCommented:
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>

0
 
kennethxuCommented:
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>
0
 
bonzaAuthor Commented:
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??????
0
 
kotanCommented:
<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>
0
 
bonzaAuthor Commented:
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>
0
 
kotanCommented:
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();
}
0
 
bonzaAuthor Commented:
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???
0
 
bonzaAuthor Commented:
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??

0
 
bonzaAuthor Commented:
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????
0
 
kennethxuCommented:
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>
0
 
kennethxuCommented:
didn't copy the last line
<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>
</logic:iterate>
0
 
bonzaAuthor Commented:
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!!!!!!!!!)
0
 
kennethxuCommented:
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.
http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20784258.html
0
 
bonzaAuthor Commented:
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!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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