Solved

how to call method in action class from javascript

Posted on 2010-09-20
20
1,380 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now