This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Struts and the fly likes Struts-2 Action class issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts-2 Action class issue" Watch "Struts-2 Action class issue" New topic
Author

Struts-2 Action class issue

Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I have a JSP called LoadProject.jsp which contains a table with row data fetched from table...

The user for loading a project has to select a row and click on Load button.

This will take the user to another JSP

For this LoadProject.jsp I have an ActionClass called LoadProjectAction...

Everytime I click the button load,the the execute method of LoadProjectAction is being run twice...what could be the error?

Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
what could be the error?


your error could be anything, how do you expect other people on this forum to help without looking at your source code?
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Well it's huge but here's the Source code...


The LoadOK button is the value of the button click in LoadProject.jsp and loadPid is the value of the row selected in LoadProject.jsp





This is the loadProject.jsp...






Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

When the submit button is clicked javascript method forLoadVocab is invoked. That method sends an AJAX request to loadproject action. Then the page is submitted so another request is sent to loadproject action...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294


have you try to omit onclick? is this js function necessary? I don't quite know what it does and it's a lot of work reading your code, plus you use scriplets, bad practice...

could you also post any error message you had and what make you think your execute method is running twice?

Please also post only part of your source code where you think the problem located at, posting the entire source code only make it more difficult for others to help with your issue. Sorry if I sound kind of harsh but if you really want to take advantage of this site and help of others you have to do your part.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Davie Lin wrote:Please also post only part of your source code where you think the problem located at, posting the entire source code only make it more difficult for others to help with your issue.

This is absolutely correct. Posting no code and posting long irrelevant code, both decreases your chances of getting a good reply...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336



Davie Lin wrote:Please also post only part of your source code where you think the problem located at, posting the entire source code only make it more difficult for others to help with your issue.


This is absolutely correct. Posting no code and posting long irrelevant code, both decreases your chances of getting a good reply...



I thought it would give a clear picture if complete code is present though strenous but yes I would take that!

have you try to omit onclick? is this js function necessary?


----- Yes that JS function is used to send the values by an Ajax call where one of the values is a primary key helping in loading the related values for the next Jsp...

plus you use scriplets, bad practice...


Well,I tried with S:Iterator but I am loading all the values of that particular ArrayList in each column rather than one at a time ...

The code is below where the 'value' is the name of the ArrayList .





could you also post any error message you had and what make you think your execute method is running twice?




Suppose I selected Row 1,I am sending the rowId in the Ajax call and ,

This is the output :
---------------------------

In LoadProject Action !!!
The loadProjectId is :---> 1
The value passed is ---> 1
The values is :--> 1
In LoadProject Action !!!
The loadProjectId is :---> 0
The value passed is ---> 0
The values is :--> 0

---------------------------------------

code for the above output---->




and the method for the Italicized-bold line



I do not have any other object of LoadProjectAction anywhere!!!

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Personally speaking, I'm really not sure what you are trying to do with your code. I already told you why your action is being called twice (one ajax call and one form submit). And as Davie said, your ajax call seems useless to me if what happens after the ajax call is that the form is submitted...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Yes Ankit, you are spot on... It's been called twicence by Ajax and also by button click...

Well I would explain that...

I have projectTable where projectId is the primary key(The table shown in loadProject.jsp)
I have a Vocab Table where projectId is Foreign key referencing the projectTable(The table in the next jsp)

I should know the projectId and I am storing it based on rowSelected, and passing it in my Ajax call

Based on the value passed , I want to use that id value, to load the values corresponding to projectID(foreign key) in the next jsp(Vocab Table) on 'loadOK' button.

Hope you understood that!

Yes, how to overcome the call twice?

Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Yes, I am ready to Avoid the Ajax call ! I think it's only to set the value of proId.

But td tag doesnt have a 'name' to match with the variable in Action class...

How can I pass the value of the row selected to Action class?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Create a hidden text field in your form, on the click of the submit button, set the proId of the selected row into the hidden field uisng javascript. Name the hidden text field loadPId (that's what I can figure out from your code) and then let the form submit...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts-2 Action class issue
 
Similar Threads
edit table using button
to pass variable to servlet from html
back button issue...
prevent a page from loading
controlling submit button in struts-2