出錯信息如下:
Method public java.lang.String org.apache.commons.lang.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of judd The problematic instruction: ---------- ==> ${msgs[0][0]} [on line 76, column 25 in org/apache/struts2/dispatcher/error.ftl] ---------- Java backtrace for programmers: ---------- freemarker.template.TemplateModelException: Method public java.lang.String org.apache.commons.lang.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of judd at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130)
解決方案:
jsonobject只能轉換map類型或者是bean類型的數據,而不能轉變action中的屬性。
如果需要action返回一個json字符串,只需要注解即可;
jsonarray只能轉換list對象;
jsonobject.fromobject(括號中是bean或者是map對象,不能是action中的屬性)
注意!
另外小計如下:
8 json通過struts2后台傳遞json對象或者是json字符串;
8.1 如果是簡單的傳入字符串,只需要加入一個struts2-json-plugin-2.3.1.2.jar包即可;
8.2 如果是需要傳入前台的是一個jsonobject,那么需要的有額外的6個包,必不可少,一定要注意:
1.commons-beanutils-1.7.0.jar
2.commons-collections-3.2.1.jar
3.commons-lang-2.3.jar
4.commons-logging-1.0.4.jar
5.ezmorph-1.0.3.jar
6.json-lib-2.1.jar
另外必須包如下:
7.struts2-json-plugin-2.3.1.2.jar
以上的包都可以在struts2中找到,注意事項:
8.2.1 jsonobject的使用方法,可以轉換一個對象,也可以自己加入一個對象;
8.2.2 jsonobject和jsonarray只是新增了一個json中的對象,而不是action中返回的json對象只有這一個轉化后的json對象;
8.2.4 在action中使用jsonobject或者是jsonarray后不會改變已有的json返回對象;