• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

struts link onclick question

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For starters I'm using:
tomcat 5.5.12
struts 1.2.8
os - solaris 10

I have a pretty simply web page/form that have several images (GO ->) that sets a formbean field to the appropiate number of the image clicked. This all works just fine and the code follows:

xx.jsp ---

<script>
function setact(action){
document.forms[0].action.value=action;
}
</script>

...

<html:form action="/myAction">
<html:hidden name="myForm" property="action"/>
...
<html:image src="/images/go.gif" on*lick="setact('2')"/>
</html:form>

Now, I wanted to do to same thing with a link --
<html:link action="/myAction" on*lick="setact('6')">Link</html:link>

Unfortunately, this does absolutely nothing. No errors anywhere but does not set the "action" field like the image tag does. Any thoughts why?? I'm sure I'm missing something simple but just don't see it.

Thanks!
>>>G
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to the Struts forum.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gregg,

I have few questions running around me. Pls clarify it to get some answers for you.

1. How did you verified that sets a form bean field to the appropriate number of images clicked? In this case, the value you are expecting is 2. Have you verified from client or server side?. Have not seen any codes to submit the form.

2. Why did you included action attribute in html:link tag?

Thanks,
Pk
 
Gregg Kelly
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pk,

Thanks for helping me think this through. And your second question is excellent!

1) On the server side I simply write out the value of my FormBean property/field and check the log file:

System.out.println("action: ",myForm.getAction());

2) Your question about the action attribute, I think identifies the problem here. Correct me if I'm wrong ...

the image tag is used inside a form -- where it's action attribute is set appropriately to the myAction class. A hidden tag is used to map the form (hidden) field "action" to the FormBean field/property "action". So when the image tag is clicked two things happen, 1) "action" field is set inside the javascript and 2) the form is submitted and the request goes back to the server myAction.

I thought if I used the link tag INSIDE the form I could also have the form fields avaiable (hidden "action" field). But apparently this is not the case. When the link is clicked, the request is sent to myAction.

So the question is this -- how can I use a html:link tag (outside a form tag) that when clicked, uses a javascript function to set the "action" field/property in my FormBean?

Thanks!
>>>G
 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gregg,
just a little push. extremely sorry for the delayed response.

I think, any form elements or the hidden variables must be enclosed with in <html:form/> tag.

Can you confirm, with the current code, while clicking the link, is the control goes to javascript?

One more question, why do u want to place the link outside the form tag? any specific reasons?

Thanks,
Pk
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No matter where a link is placed, whether inside or outside of a <html:form></html:form> tag pair, it does notcause the form to be submitted. It merely calls the URL in the link wihout passing any of the parameters in the form to it.

There are two possible solutions to this:

1-Make the link refer to something non-operational, and use the onclick event to submit the form. Something like this:

<a href="JavaScript:void(0)" onklick="setact('6');document.forms[0].submit()">click here</a>

2-If you just want to submit the form with the "action" parameter and no others, you could simply incorporate the parameter into the URL like this:

<a href="myAction.do?action=6">click here</a>

P.S. When using JavaScript in a form it is not a good idea to name one of it's properties "action" (or submit, or reset either for that matter) because by doing so you are replacing one of the form's own properties. The "action" property of a form normally refers to the action called by the form, but by naming one of your properties "action", you've overrriden it, thereby making it impossible to refer to the action called by the form in JavaScript.
[ July 03, 2006: Message edited by: Merrill Higginson ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic