Solved

JSP/Struts URGENT question!!!!

Posted on 2003-10-28
18
927 Views
Last Modified: 2010-04-01
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
Comment
Question by:bonza
  • 9
  • 5
  • 3
  • +1
18 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 9639949
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
 

Author Comment

by:bonza
ID: 9640119
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
 
LVL 35

Expert Comment

by:TimYates
ID: 9640404
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
 

Author Comment

by:bonza
ID: 9640419
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 9643857
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 9643882
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
 

Author Comment

by:bonza
ID: 9646448
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
 
LVL 6

Expert Comment

by:kotan
ID: 9646481
<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
 

Author Comment

by:bonza
ID: 9646522
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 6

Expert Comment

by:kotan
ID: 9646600
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
 

Author Comment

by:bonza
ID: 9647314
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
 

Author Comment

by:bonza
ID: 9647376
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
 

Author Comment

by:bonza
ID: 9647429
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 9651745
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
 
LVL 14

Accepted Solution

by:
kennethxu earned 500 total points
ID: 9651755
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
 

Author Comment

by:bonza
ID: 9654741
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 9658284
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
 

Author Comment

by:bonza
ID: 9660286
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now