wood burning stoves 2.0*
The moose likes Struts and the fly likes struts2 + json plugin problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts2 + json plugin problems" Watch "struts2 + json plugin problems" New topic
Author

struts2 + json plugin problems

Pj Casaro
Ranch Hand

Joined: Jul 13, 2010
Posts: 47
I'm using the json plugin that comes with struts 2 (json-lib-2.1.jar) and trying to follow the website to set it up.

Here's my struts.xml



but I get this warning:
SEVERE: Unable to find parent packages json-default

Is there something else I'm supposed to do?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What else have you done besides use the "extends" attribute? The json plugin isn't called "json-lib-2.1.jar"; that's just the JSON part--are you deploying the actual plugin?
Pj Casaro
Ranch Hand

Joined: Jul 13, 2010
Posts: 47
I actually didn't have the correct plugin! Ok, so I put in the right plugin, but still some problems.


However, I don't think the response is going from the RetrieveUser class to the javascript. I'm using firebug and no request gets sent.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Without knowing how you're making the request, it's impossible to help diagnose why it's not happening.
Pj Casaro
Ranch Hand

Joined: Jul 13, 2010
Posts: 47
Yes I understand, sorry I'm new to this so not too sure what things are relevant.

Here's everything I have.

1) The user clicks a link which triggers this action:


2) This just populates a list of users, and then automatically returns this result:


3) Here's my RetrieveUser class:


4) And my javascript file:


5) And finally my struts.xml:


In firebug, I can see that when I change an item in the select menu (triggering the onchange=fetchUser) I get this error in the javascript here:
that says "setting a property that has only a getter". I don't think the javascript is the problem because I've gotten this javascript to work before just not with this json plugin.
Pj Casaro
Ranch Hand

Joined: Jul 13, 2010
Posts: 47
Ok I found two problems.

The first is that I can't use the variable name "console" in my javascript when I'm using firebug I think. So I changed that.

The second is, now that I have a json result (finally!), I can't figure out how to parse it correctly.

Here's what it looks like (part of it):


The json is stored under jsonObject so I'm trying to access the user by saying "jsonObject.jsonModel[0]" but that's undefined. The problem is, it serialized the whole thing as an object and I don't know what "myusername" is going to be, so I can't just say jsonModel.myusername.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're getting an error in JavaScript but don't think it's in the JavaScript?

I'm pretty sure it is, *if* you're actually getting an error in the JavaScript. That's some pretty old-school JavaScript, too--yuck.

In any case, I don't understand why that would be an error you'd get in JavaScript itself, since JavaScript has no concept of getters and setters, but if it's a JavaScript error, it's most assuredly an error in JavaScript or the DOM with which it is interacting.
Pj Casaro
Ranch Hand

Joined: Jul 13, 2010
Posts: 47
The code is from Struts 2 in action, a book I've been going through to try and learn struts.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Pj Casaro wrote:The json is stored under jsonObject so I'm trying to access the user by saying "jsonObject.jsonModel[0]" but that's undefined. The problem is, it serialized the whole thing as an object and I don't know what "myusername" is going to be, so I can't just say jsonModel.myusername.

Then it's probably not reasonable to serialize it like that, or you'll have to iterate over the properties to find out what the user name actually is.
Pj Casaro
Ranch Hand

Joined: Jul 13, 2010
Posts: 47
I figured as much and went ahead and changed it to serialize over my "User" object instead of the whole class. Now it almost works. However I do have maps as part of the user and the maps get converted to maps again in json, so I do have to iterate over keys at some point anyway. Is there a numkeys/size/length command for the number of keys in something?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

For JavaScript questions your best bet is to post in the HTML/JavaScript forum.
Pj Casaro
Ranch Hand

Joined: Jul 13, 2010
Posts: 47
OK thanks! I solved all my issues relevant to this post. Thanks for all the help
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Glad you worked it out :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts2 + json plugin problems
 
Similar Threads
jquery + struts2 => JSON autocompleter example?
Cannot resolve package json-default, hibernate-default
struts2 (2.1.8.1) json result type problems
Handling Ajax response in struts2
Autocompleter