Solved

how to call method in action class from javascript

Posted on 2010-09-20
20
1,388 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 23
jsp login check 12 32
printf performancy 11 43
Index on a Table 6 15
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

803 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