aspose file tools*
The moose likes Struts and the fly likes Struts 2 - altering form action and submitting via a hyperlink Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 - altering form action and submitting via a hyperlink" Watch "Struts 2 - altering form action and submitting via a hyperlink" New topic
Author

Struts 2 - altering form action and submitting via a hyperlink

Mark Brownengland
Greenhorn

Joined: Apr 30, 2006
Posts: 26
I have the following form tag:



inside the form, I have the following hyperlink



When clicking on this hyperlink, instead of going to: viewA_doSomething.action, the href is used and attempts to go to 'blah'.

If I use an input (I do however want to use a hyperlink), this does work and submits the form having substituted its action to: viewA_doSomething.action




Do I need to use a different tag to get my hyperlink to correctly submit the form or am I missing something obvious?

Thanks in advance.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't really see any link inside the anchor tag, so it's hard to say what might be going wrong.
Mark Brownengland
Greenhorn

Joined: Apr 30, 2006
Posts: 26

But that's the whole point. I'm not setting anything in the anchor as I've got an event intercept i.e. onclick which alters the form's action & then submits the form. I'm expecting that on form submission, the action I've altered my form to execute would be invoked upon calling submit(). This does not happen and instead, the location in the href is simply invoked.

I've debugged to check that the action viewA_doSomething.action is not being invoked & I can confirm this is the case.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

add a return false; at the end of the onclick event i.e. after submit(); is called. This will stop the browser from following the hyperlink and submit the form...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Mike: I'm saying that when you originally posted your message there was no link, no event ahandlers, no nothing--just the "rel" attribute.

There's no way I could have diagnosed or helped with the information given originally.

Ankit is correct--this is an HTML issue, not Struts. I guess you're not writing your app to degrade gracefully if the user has their JavaScript turned off--that's ok, but it's a decision that should be made consciously.
Mark Brownengland
Greenhorn

Joined: Apr 30, 2006
Posts: 26
Sorry for the earlier confusion. When I first posted, the a link had changed to rel=nolink which I failed to spot but subsequently re-corrected through using alternative character encoding.

I have now rectified this particular problem although I still have the problem in a related post concerning retrieving hidden (s:hidden) attributes in an action that implements RequestAware. I will re-post on the related thread.

In answer to your javascript dependency comment, I wish to make my app interoperable across browsers & local settings as possible as I will have little control over the environment of each user. I have updated my a tag to also forward to the same action, setting each request parameter as part of the URL string.
Mark Brownengland
Greenhorn

Joined: Apr 30, 2006
Posts: 26
.
Mark Brownengland
Greenhorn

Joined: Apr 30, 2006
Posts: 26


After further testing, it seems that adding return false has had no impact. To differentiate between submitting from the onclick and the href, I have purposefully made the href a random value in the example quoted below:



On my browser (safari v4.0.3), the return false has no effect & the browser instead attempts to go to the href location i.e. differentAction_mB.action

Is this a browser issue or have I add the return value incorrectly.

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Is it actually submitting? I wouldn't have thought just putting in submit() would work--I'd guess it's just causing a JavaScript error, which should be obvious if you're using something like Firebug, and doing the regular link processing once the JS errors out. Still not a Struts issue, though.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 2 - altering form action and submitting via a hyperlink