0

I have been trying to display my JSON data returned from action class in my JSP, but in vain...

I am using struts 2.1.8..

The following is the struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources">      </constant>

<package name="sampleStrutapp" extends="struts-default">

    <action name="login" class="struts2.LoginAction">
       <result name="success">/checkValidation.jsp</result>
       <result name="error">/HelloWorld.jsp</result>
    </action>

</package>

<package name="struts2" extends="json-default">

  <action name="populate" class="struts2.Populating">
    <result type="json">
     <param name="root">popdata</param>
     <param name="noCache">true</param>
    </result>

  </action>


</package>

  </struts>

My action class is the following..

 package struts2;



import java.util.List;



 import com.opensymphony.xwork2.*;

 import net.sf.json.JSONObject;

 import org.apache.catalina.connector.Request;

 import data.Details;

 import com.Various_functions;
  public class Populating extends ActionSupport {



/**
 * 
 */
private static final long serialVersionUID = 1L;

public String usrname;
public String email;
public String lang;
public String gender;
public String comp;
public String desg;
public int rownum;
public int row;
public int getRow() {
    return row;
}

public void setRow(int row) {
    this.row = row;
}
private String popdata;

public String getPopdata() {
    return popdata;
}

public void setPopdata(String popdata) {
    this.popdata = popdata;
}

public int getRownum() {
    return rownum;
}

public void setRownum(int rownum) {
    this.rownum = rownum;
}

public String execute(){


Various_functions funct = new Various_functions();

List<Details> dev = funct.populate(this.getRow());



this.popdata = dev.get(0).getCompName()+","+dev.get(0).getDesig()+","+dev.get(0).getEmailId()+","+dev.get(0).getGender()+","+dev.get(0).getLang()+","+dev.get(0).getUserName();



System.out.println("In action class");
return "success";
}

public String getUsrname() {
    return usrname;
}
public void setUsrname(String usrname) {
    this.usrname = usrname;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getLang() {
    return lang;
}
public void setLang(String lang) {
    this.lang = lang;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getComp() {
    return comp;
}
public void setComp(String comp) {
    this.comp = comp;
}
public String getDesg() {
    return desg;
}
public void setDesg(String desg) {
    this.desg = desg;
}



 }

and my javascript is the following..

 function populate(rownum){

alert(rownum);

var inputData = { "row" : 1
};
//  alert(inp);
$.post('populate.action', inputData, populatecallback, "json");



}


function populatecallback(popdata) {


alert("in call back!!");
var result = popdata.split(",");

 usrname= result[5];
 email= result[2];
 desg= result[1];
 lang= result[4];
 comp= result[0];
 gender= result[3];

 $('#txtName').val(usrname);
 $('#txtEmail').val(email);
 $('#txtCmp').val(comp);
 $('#txtDesg').val(desg);
 $('#lan').val(lang);
 $('#gen').val(gender);




}

Now, the jquery post is not working.

The following is the current library structure..

commons-collections-3.1.jar, commons-fileupload-1.2.1.jar, commons-io-1.3.2.jar, commons-logging-1.1.1.jar, freemarker-2.3.15.jar, slf4j-api-1.5.8.jar, slf4j-simple-1.5.8.jar, struts2-core-2.1.8.1.jar, struts2-jquery-plugin-1.8.3.jar, xwork-core-2.1.6.jar

My js files are the following..

jquery-1.6.4.min.js, json.js

Suggestions appreciated.

Thanks and regards, hemanth.

3
  • 1
    Please list the libraries you're deploying, and their versions-right now it looks like a library mismatch, from the exception. Oct 18, 2011 at 6:55
  • antlr-2.7.6.jar commons-collections-3.1.jar commons-fileupload-1.2.1.jar xwork-core-2.1.6.jar xwork-2.1.3.jar struts2-core-2.1.8.1.jar jsonplugin-0.34.jar json-lib-2.4-jdk15.jar freemarker-2.3.15.jar commons-io-2.0.1-javadoc.jar commons-io-2.0.1-sources.jar commons-logging-1.0.4.jar commons-io-2.0.1.jar
    – h-kach
    Oct 18, 2011 at 7:11
  • Hi dave, the above is my list..
    – h-kach
    Oct 18, 2011 at 7:11

2 Answers 2

1

Might not be relevant now, but back in 2009, there was an issue with Struts and writing json, namely:

http://code.google.com/p/jsonplugin/issues/detail?id=91

Perhaps its a similar issue you are hitting.

4
  • Hi Chris, thanks for early response. I had seen the link that you sent , but that was for struts 2.1.7, i am using struts 2.1.8. But either way, do you have any work around for the above issue??
    – h-kach
    Oct 18, 2011 at 6:51
  • 1
    I noticed - what version of XWorks are you using? The bug is still open... you could check the source of 2.1.8 and see if it includes the suggested patch. Presumably upgrading to a more recent version of Struts is not an option? Oct 18, 2011 at 6:53
  • chris, i am using the following xwork , xwork-core-2.1.6.jar xwork-2.1.3.jar.. How to apply the patch in the source of xwork, can u suggest please.
    – h-kach
    Oct 18, 2011 at 7:13
  • See Dave's answer - you have mismatched library versions Oct 18, 2011 at 7:42
1

The exception is caused by a library version mismatch; in your case you're deploying multiple versions of XW, which is even worse. S2.1.8.1 requires xwork-core 2.1.6. Having both on the path is a bad idea. Consider using Maven to handle your dependency management, and avoid these kinds of headaches.

I did not verify the remaining versions on your list; you need to do so. The easiest way is to use Maven, or check against something like mvnrepository.com (where I got the S2.1.8.1 XW dependency from).

6
  • Hi dave, I have removed all the multiple matches, but its still giving the same exception..
    – h-kach
    Oct 18, 2011 at 8:16
  • @hemanth Please update your question with the currently-deployed libraries. I think Chris's answer is still more on the right track; it looks like the JSON plugin you're using expects an older version of XW? Is there another way you could write the JSON, or can you update S2, etc? Oct 18, 2011 at 8:39
  • @hemanth See my second comment--it looks like that's a known issue as Chris stated, with a patch, although it may be easier to write the JSON some other way. Oct 18, 2011 at 8:59
  • I have not got any other way to write JSON for my requirement, so far. I thought this was a feasible method, since I had seen it getting implemented elsewhere. can you please suggest?
    – h-kach
    Oct 18, 2011 at 9:17
  • Use a different JSON library and write it to the response, use the convention plugin, patch the jsonplugin, try rolling back XW and seeing if it works, ... Oct 18, 2011 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.