Solved

how to call method in action class from javascript

Posted on 2010-09-20
20
1,392 Views
Last Modified: 2012-05-10
Dear all,
I  have small issue calling a method from javascript function
How can i call a method in action class from javascript  function.
please reply..its urgent

I have delete() in action class below is the way i am calling it but the control is not going to action class.I am using struts 2.1
function validate()
{
if confirm("want to delete")
document.forms[Jsp formName].action = 'path/delete.action'
}

0
Comment
Question by:SonyAgarwal
20 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33721886
The code as you have it written here is invalid syntax, so can you paste the exact code as you have it in your JSP file, please.
0
 
LVL 12

Expert Comment

by:jagssidurala
ID: 33722008
we can do this using webservice ajax calls.

declare a method with "webmethod" in class file(.cs file if ur using Dot net), call method using JSON in javascript file.

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33722028
" Action Class " + " struts 2.1 " + "forms[Jsp formName]" led me to believe this is definitely Java/JSP related JavaScript as the Question is zoned.  
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 40

Expert Comment

by:gurvinder372
ID: 33722358
why do you want to call other methods in action class at first place?
Just simply let the flow of execution go to execute method of action class and then from there you can move in which ever direction you want to based on request parameter values.

<<document.forms[Jsp formName].action = 'path/delete.action'>>
just check if this path is same as given in struts.xml

0
 

Author Comment

by:SonyAgarwal
ID: 33725683
yes its a java,jsp question
0
 

Author Comment

by:SonyAgarwal
ID: 33725736
The call to the action class should happen onClick of a button.i provided a button called delete when user click on it i made a javascript alert if i say Ok then it should call my delete method from action class and process the flow.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33725921
Have you tried issuing document.forms[Jsp formName].submit() after the setting of the action ?
0
 

Author Comment

by:SonyAgarwal
ID: 33725976
yes....
below is the way i am calling my action class....
<script>
          function validate()
          {
            var ConfirmMessage = confirm("Are you sure want to delete");
           if (ConfirmMessage){
          alert("in validate if cond");
                 document.RequestForm.action = '/project/delete.action';
               document.RequestForm.submit();        
            }
                  
          }
      </script>
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33726097
did adding the submit() not work?

See if I missed something, here is a reference:
http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33726111
How are you calling validate(), can you show the pertinent portion of form code -- the form tag itself and the button's tag would be fine.
0
 

Author Comment

by:SonyAgarwal
ID: 33726166
i am calling on onClick of a button....
 <s:submit type="button" value="Delete Request" action="viewRequest" onClick="validate()" />
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33726210
Guess I should have asked earlier, do you know for sure if the validate() method is even being run?  In other words, do you get the confirm box for the delete, but the action is not done OR are you not getting anything at all?

Try:
<s:submit onclick="javascript:validate();">Delete Request</s:submit>

Open in new window

0
 

Author Comment

by:SonyAgarwal
ID: 33726245
I am getting confirmation alert onclick of ok i shoudl go to delete method in action class and delte the record which i am not able to do..
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33726371
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33726388
Then you can use an overal onsubmit check to confirm if user is sure they want to proceed.
0
 

Author Comment

by:SonyAgarwal
ID: 33726735
i am using struts 2
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33726862
Sorry, you did say that in the question -- here is the proper reference for multiple submits and associating events / actions / javascript to those submit (sources).

http://struts.apache.org/2.0.14/docs/ajax-and-javascript-recipes.html
0
 

Author Comment

by:SonyAgarwal
ID: 33727160
thx....
0
 

Accepted Solution

by:
SonyAgarwal earned 0 total points
ID: 33730824
Thanks mwvisa1 for your suggestions.I resolved by creating an action and called that action from javascript method
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33730851
You are welcome!  I thought that is what you were doing in the first place that wasn't working.  Glad you found that as I didn't think to suggest it since I thought that is what you were struggling with. *laughing*
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Binding a Grid in Javascript 5 46
Explain this javascript function to me 2 29
Html Newline 7 21
On Load function does not trigger on iPhone 4 37
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question