This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Struts and the fly likes struts link onclick question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts link onclick question" Watch "struts link onclick question" New topic

struts link onclick question

Gregg Kelly

Joined: Feb 10, 2006
Posts: 6
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 ---

function setact(action){


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

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.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63348

Moved to the Struts forum.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Periakaruppan Thiagarajan
Ranch Hand

Joined: Jul 26, 2005
Posts: 65

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?

Gregg Kelly

Joined: Feb 10, 2006
Posts: 6

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?

Periakaruppan Thiagarajan
Ranch Hand

Joined: Jul 26, 2005
Posts: 65
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?

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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="">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 ]

Consultant, Sima Solutions
I agree. Here's the link:
subject: struts link onclick question
It's not a secret anymore!