wood burning stoves 2.0*
The moose likes Struts and the fly likes [struts 2] Use Javascript confirm() dialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "[struts 2] Use Javascript confirm() dialog" Watch "[struts 2] Use Javascript confirm() dialog" New topic
Author

[struts 2] Use Javascript confirm() dialog

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8914
    
    8

I have a table of items:


The "Edit" link takes the user to the item edit page. Ideally, the Delete button would prompt the user "Do you want to delete Item 1?", the user would confirm that he does, and the list would be redisplayed without the deleted item. I can't think of a clean way to implement this functionality. I can display the Delete link thusly:



I can display the Javascript pop-up with onclick, but that will execute the action no matter what the user clicks. The only other thing I can think of is something like what Merrill posted here, but each Delete link would be a separate form. Not very clean.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

You can use the confirm function on the onclick event of the hyperlink. You can do something like this



And this doesn't seem to be concerned with Struts to me...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8914
    
    8

Ankit Garg wrote:
And this doesn't seem to be concerned with Struts to me...


It's not the JavaScript that's giving me the problem. It's using Struts 2 tags with the JavaScript. The code you have given works and that's fine, but it sticks out like a sore thumb in a JSP that's entirely Struts tags. I was wondering if there's a more "Struts 2" way to accomplish the same functionality.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8914
    
    8

Well, I came up with the following. In the JSP, I just reference a property ("escape=false" writes the property as-is, without escaping HTML characters):


Then in the java bean I have a property that prints out an anchor tag with the JavaScript to do the confirmation embedded:


(sorry about the image, but JForum stripped out my JavaScript code)
It creates some ugly HTML/JavaScript, but this page will only have around 30 values, so it's Good Enough. It's not a good separation of responsibility, a data bean knowing how to display an HTML link, but it makes the JSP really clean and the bean only has one weird method. It's self-contained, which is a good thing, as opposed to having a JavaScript function elsewhere that has no connection to the tag in the JSP.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

You could have done something like this

 
GeeCON Prague 2014
 
subject: [struts 2] Use Javascript confirm() dialog