File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes struts-2 (set values to action class) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts-2 (set values to action class)" Watch "struts-2 (set values to action class)" New topic
Author

struts-2 (set values to action class)

Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I want to pass values from html elements(of JSP) to my Action class.How to do this via an Ajax request...

suppose I have :

in my JSP

and



How to get the value of text entered in Action clas via an Ajax request (Struts-2+Ajax) ,or are there any other types of Action classes/methods to achieve this??

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Are you using a custom AJAX request?? If yes, then its easy to do. Follow this w3c tutorial and you'll be good to go (specifically adding parameters to ajax request is given on this page in that tutorial)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
How will my URL be? most of them are either ' .asp or .php' and I use neither.So for JSP how would it be?




and how is it set in Action class?
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Ok,Now I have an Ajaxfunction which looks like :




The parameters in my Action class are :--->




var url="http://localhost:8084/Struts_V1.0/VocabularyAction.action?concept="+concept&"attribute="+attribute&"datatype="+dataType;

Issues:
1) I have to stay in same JSP and i cannot give so only and this doesn't communicate with Action class .
2) How to set the values from URL to Action class then???
3) In the URL since I have to stay in the same page, should it be vocabulary.jsp or vocabularyAction.action??



Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17


The above line is syntactically incorrect. It should be

Other than that everything looks fine to me. You don't need to use a submit button to call this method. Are you calling this JS method using the onclick event of the button??
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Yes I am calling the addRow JS method which inturn calls this method !!! and the URl is modified.

Now, In Action class how are these values recieved??

should I implement ServletResponseAware interface?? It''s not even entering the Action class...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

should I implement ServletResponseAware interface??

No need for that. The request parameters will be automatically mapped to the properties in your action class. I just noticed that your action properties name differ from your parameters in the AJAX request. Try to use this URL


You said that the control is not even entering the action class. Is the URL correct?? Is your app running on the 8084 port and the context path is Struts_V1.0?? Also is the action name VocabularyAction (watch the capitalization). Also in your JS code I don't see a call to xmlhttp.send, this is the line that actually makes the AJAX request...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Yes Ankit !!! It's going to Action class... whew! but with a clause...


Modified Code Again:



---> This way it's setting everything to null since it I am sending 'null' I guess !!!

what should be sent in:

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

The send method is the one that makes the actual AJAX call, so you can't remove it. Inside the send method, we send the body of the AJAX request, but since we are using a GET request, so there won't be any request body. Are there text fields in your JSP with the ID conName, atrName and dtName?? Basically is the alert("concept is :--->"+concept); giving you a value or null or undefined?? How did you check that the properties in your action class are null?? You've also changed the name of one of the request parameters (addDTList), double check if that's correct...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Actually the print statements of [concept,attribute,datatype] are perfectly printing the values i entered in my form...

Yes my TextFields have the ID's as conName,atrName,dtName...

here is the Action class then,




All the print statements in the above are giving 'null' by which I could find out it's enetring the Action class...


here's the output :
------------------------

Nov 4, 2009 6:05:54 PM org.apache.catalina.core.StandardContext start
INFO: Container org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/Struts_V1.0] has already been started
---- in setAttributeName
---- in setConceptName
Inside Vocabulary Action Class
The addVocOk is :-->null
saveVocabulary-->null
gotoRuleset-->null
voc target is :-->null
Nov 4, 2009 6:06:16 PM com.opensymphony.xwork2.validator.ActionValidatorManagerFactory <clinit>


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

I'm completely confused here . These are the println statements in your action


They are displaying values of properties that we didn't pass in the request parameters. Check the value of the 3 properties that we sent in the request i.e. conceptName, attributeName and addDTList. They must not be null. The request parameters conceptName, attributeName and addDTList won't map to action properties addVocOk, saveVocabulary and gotoRuleset. The output
---- in setAttributeName
---- in setConceptName

clearly shows that the attributeName and conceptName properties are being set. And the println statement

is never actually executed because of the condition if(addVocOk!=null). Am I missing something here??
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Actually the println's are the other elements in my JSP...

addVocOK is a property of 'OK' button, saveVocabulary is a property of 'Save' button,gotoRulesets a property of 'GotoRuleset' button,

Actually the form is submitted on the click on the above 'OK' button, then the JS is called which inturns calls out AJaxfunction...

should we pass the button click also as a parameter??

the code..its huge... I would highlight those things for you...
-----------------------------






Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

addVocOK is a property of 'OK' button, saveVocabulary is a property of 'Save' button,gotoRulesets a property of 'GotoRuleset' button,

Since we are making an AJAX request, so there won't be any request parameters other than what we send as query string. You need to pass any value used in the action in the AJAX request.

PS: this seems to have nothing to do with struts, just ajax problem, I'm tempted to move it to HTML forum but I'm leaving it here for now...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Thanks Ankit... I didn't code a thing without your reply !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts-2 (set values to action class)