• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[struts 2] Use Javascript confirm() dialog

 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could have done something like this

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic