?
Solved

how to call method in action class from javascript

Posted on 2010-09-20
20
Medium Priority
?
1,453 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 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