1. 程式人生 > >struts2 java.lang.ClassNotFoundException: org.apache.commons.lang.xwork.StringUtils

struts2 java.lang.ClassNotFoundException: org.apache.commons.lang.xwork.StringUtils

struts2.3  action 返回JSON資料除錯過程中報錯

最終定位到json-plugin的jar包版本不一致導致

struts2-core-2.3.16.3.jar

struts2-json-plugin-2.3.16.jar

struts2-spring-plugin-2.3.16.3.jar

xwork-core-2.3.16.3.jar

action返回json格式方法如下:(引至網際網路)

1.傳統方式JSON輸出
HttpServletResponse response = this.getResponse();
response.setContentType("application/json;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter pw = response.getWriter();
String str = JSONArray.fromObject(list).toString();
pw.write(str);
pw.flush();
pw.close();

頁面上就獲取到了 

2.struts 配置方式
struts.xml:
<package ...... extends="json-default">
<action name="xxxxx">
     <result type="json" name="success">
           <param name="root">jsonMap</param>
     </result>
</action>

xxx.action:
Map jsonMap;
set...get...//生成set、get方法

方法裡邊:
jsonMap = new HashMap<String Object>();
jsonMap.put("list",list);

頁面:
回撥函式:function(result){
       var list = result.list;
        for(var i=0;i<list.length;i++){
            var name = list[i].name;//假設list[i]裡邊有個屬性name;
       }
}

參考:

http://blog.sina.com.cn/s/blog_64e467d60101i8ez.html

http://www.cnblogs.com/doit8791/p/4294276.html