• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to call action class using ajax

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i am new to struts i want to fill <td> with some data i get using ajax. but the action does not get called using this lines of javascript
but i DO NOT want to use dojo or jqery, i want to use traditional ajax

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

be yourself wrote:hi i am new to struts


This doesn't seem to have much to do with Struts.

but i DO NOT want to use dojo or jqery, i want to use traditional ajax


Why? Unless it's to learn how Ajax works under the hood before using one of the libraries for real code, it's like a carpenter saying "I want to cut this board, but I don't want to use a saw, I want to use my bare hands."

For example, the code you posted will not work across all browsers in common use today.

In any case, turn on the debugger in the browser and observe the network behavior of the Ajax call. Is it going to the right place? Is it being made at all?
 
teddy clayton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why? Unless it's to learn how Ajax works under the hood before using one of the libraries for real code, it's like a carpenter saying "I want to cut this board, but I don't want to use a saw, I want to use my bare hands."





thank you, it was my first post on this forum and i did not know much rules.
about ajax, the reason i want to use the old way of calling ajax is that i don't know jquery, and i actuality i want t fill the <td> by the result return when the user click a search button.
i could not find how to do it using jquery.

if you can help me i will be gratefull
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teddy clayton wrote:thank you, it was my first post on this forum and i did not know much rules.


No problem. But it's all about helping you to get responses to your questions. The easier you make it for people to help you, the more help you'll get.

about ajax, the reason i want to use the old way of calling ajax is that i don't know jquery, and i actuality i want t fill the <td> by the result return when the user click a search button.
i could not find how to do it using jquery.


Well, first of all, learning how to do it with raw Ajax isn't a bad thing when you're learning, as I pointed out. But when it comes time to make real code, there are too many nuances and pitfalls with Ajax to trust it to raw code. That's where a library such as jQuery comes in.

if you can help me i will be gratefull


Using jQuery for Ajax is as easy as pie.

Let's say that the element that you want to "fill" with the response has an id of fred. To load an HTML fragment returned from an Ajax response would be:

That's it. And it will work flawlessly in all browsers.

If you want more details on jQuery, see the online docs, and there's always my book (see link in my sig).
 
teddy clayton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:
Using jQuery for Ajax is as easy as pie.

Let's say that the element that you want to "fill" with the response has an id of fred. To load an HTML fragment returned from an Ajax response would be:

That's it. And it will work flawlessly in all browsers.



hmmm, sorry for asking this :$ but, can you give me the whole code of how to let a <input /> button call the struts action say "myAction" before filling it in "fred" <td> using JQuery and ajax.

just the javascipt part and HTML part.

please
 
Ranch Hand
Posts: 34
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Teddy, it sounds like the focus of your question is how to make an ajax call to a Struts action and then how to use the response. I will try to point you in the right direction. You didn't mention which version of Struts you are using, but I am going to assume Struts2. The ajax framework that I use is dojo.

Let's say, with ajax (using the dojo framework), you wanted to post a staffId, get back the associated staff name from an action named staff. and then populate a td with id "staffNameDisplay". Your html might look something like this:

Staff Id: <input type="text" name="staffNum" id="staffNum" value="5" />

<input type="button" id="searchButton" value="Search" onClick="postStaffId()" />

....
<td id="staffNameDisplay"></td>


.... And your javascript function might be like this:

function postStaffId() {
var staffNum = document.getElementById("staffNum").value;
dojo.xhrPost( {
url: "staff",
sync: false,
content: {
staffId: staffNum
},
handleAs: "json",

// The LOAD function will be called on a successful response.
load: function(response, ioArgs) {
dojo.byId("staffNameDisplay").innerHTML = "The associated staff name is " + response.staffName;
},

// The ERROR function will be called in an error case.
error: function(response, ioArgs) {
console.error("HTTP status code: ", ioArgs.xhr.status);
return response;
}
});
}

The url property must be the name of your action (e.g. staff) and the handleAs refers to the format in which you are expecting the response (e.g. json). Content are name/value pairs of variables you are setting in the Action (in this case I am setting the variable staffId).

If you are using dojo to make the ajax post, you must reference the dojo javascript file at the top of the javascript code with:
<script type="text/javascript" src="js/dojo/dojo.js"></script>
Obviosuly, you need to have the dojo.js file in the js/dojo directory of your web application.

To make your action named staff be returned in json format, you should extend the json-default package and specify a return type of json in your struts.xml:

<package name="ajax" extends="json-default" >
<action name="staff" method="JSONStaff" class="myclass.actions.StaffAction" >
<result name="success" type="json" />
</action>
</package>

You can see from the above declaration that a method called JSONStaff will be executed when the staff action is invoked.
In your JSONStaff method you would set the value of variables such as the staffName (perhaps via a database lookup using the staffId)
and return SUCCESS.

When your Action is returned as type json, unless you explicitly include or exclude properties, all of your Action variables will be accessible in the response using dot syntax (e.g. response.staffName)

To enable you to return your Action as a JSONObject, you need to install the following 2 jars:
json-lib-2.1-jdk15
struts2-json-plugin-2.3.1.2

I hope this helps get you on track.
 
teddy clayton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much. your explanation was very helpful.
ok now i got how to deal with this in struts 2

hmm, i read previously that Dojo is depricated and now Jquery is in use.

is that true??
 
Vanessa Danin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pleasure Teddy! No, Dojo is definitely not deprecated. The Struts Dojo plugin was deprecated. In other words, you should not use Struts tags to make Dojo Ajax calls but using plain Dojo (as I have done in my example) is perfectly fine.
 
reply
    Bookmark Topic Watch Topic
  • New Topic