File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Preventing submit of html:cancel using javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Preventing submit of html:cancel using javascript" Watch "Preventing submit of html:cancel using javascript" New topic

Preventing submit of html:cancel using javascript

Charanjit Sidhu

Joined: Apr 04, 2005
Posts: 13
Hi All,

I have a small problem.. I have a button named 'Make Changes' on which I just want to use the browser back functionality. But I am not able to do it. The button is:

<html:cancel onClic="fnGoBac()" > Make Changes </html:cancel >

and the javascript code is:

function fnGoBack()
alet("In fnGoBack")

(Note: history,onClick and alert are misspelled as histori,onClic and alet so that I can post this on this site.)
Onclick the alert appears but instead of going back to previous page, I get the page as defined by the action associated with this page.

However if I change the button code to normal html code (as shown below), it works fine.. but for consistency sake, I want to follow all sturts html

<input type="button" value="Make Changes" onclic="fnGoBack()" >

Please let me know how can I achieve this by using Struts HTML:Cancel or some other tag..

Charanjit Sidhu<br />SCJP 81%
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

You want to use html:button instead of html:cancel. Struts' cancel button maps to a submit button. It then has all the characteristics of a submit button - submitting the page, doing an action, etc. On the other hand, html:button renders into the input button type that you used in the HTML code sample.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Anant Rao
Ranch Hand

Joined: Nov 12, 2004
Posts: 126
> html:button value="Back" property="Back" onclic="return OnButtonClick();"<>/html:button<

This might work for you.

Kamesh Loganathan

Joined: Jan 21, 2006
Posts: 20
I think that a more refined way of doing this is to use the html:cancel button, no java script and check for iscancelled in the action and fwd appropriately to the previous page.

Using javascripts history looks to be crude to me.
Correct me if i am wrong.
Charanjit Sidhu

Joined: Apr 04, 2005
Posts: 13
Thanks everybody for your responses.

I implemented the solution as suggested by Jeanne & Infyniti and it worked for me.

I just needed to show the previous screen with all the user's input in it. So I just used 'browser back functionality' for this purpose.
Actually I am learning Struts and didn't know how to repopulate the screen with same form data which had rows of input data having select and input boxes in each row. So I thought the quickest way will be to just do a 'Browser back'.

Thanks again!!

Joined: May 09, 2005
Posts: 1
I have a better implementation of the cancel button. You can actually use EL and send a hidden field of ${header.Referer} which refers to the previous page and then submit the page to that value.
Hope this helps!
I agree. Here's the link:
subject: Preventing submit of html:cancel using javascript
It's not a secret anymore!