Solved

how to call method in action class from javascript

Posted on 2010-09-20
20
1,434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
20 Comments
 
LVL 60

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 60

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 60

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 60

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 60

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 60

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 60

Expert Comment

by:Kevin Cross
ID: 33726371
0
 
LVL 60

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 60

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 60

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 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