struts,ajax,json的结合老出错
2016-05-15 22:44
621 查看
最近在研究struts,ajax,json的结合,真的搞了好久啊,网上查了很多资料,但是还是会有有些问题,最终发现原来是jar包的问题。
测试了好久,发现会出现以下问题:
java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
1,可能是jar包缺少
2,commons.lang的jar包问题,我就是在这里搞了好久,使用json是要依赖, ezmorph.jar,commons-collections.jar commons-lang.jar commons-beanutils.jar,json-lib.jar,struts2-json-plugin。commons-beanutils.jar要有commons-collections.jar的支持,
commons-collections.jar要有commons-lang.jar的支持(而commons-collections.jar又需要到commons-lang***3***.jar)
所以这里要同时拥有两个 commons-lang.jar(一个json用,另一个collection用缺少后者会报以上错)!!!!
已经写对了action但还是会报Could not find action or result这个错误,就是缺少了struts2-json-plugin.
这是我用的jar包
![](http://img.blog.csdn.net/20160515223942569)
有人还说struts2版本过高还会影响json,这个我就没有试过了,读者可以试下。
index.jsp
action:
struts.xml
测试了好久,发现会出现以下问题:
java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
1,可能是jar包缺少
2,commons.lang的jar包问题,我就是在这里搞了好久,使用json是要依赖, ezmorph.jar,commons-collections.jar commons-lang.jar commons-beanutils.jar,json-lib.jar,struts2-json-plugin。commons-beanutils.jar要有commons-collections.jar的支持,
commons-collections.jar要有commons-lang.jar的支持(而commons-collections.jar又需要到commons-lang***3***.jar)
所以这里要同时拥有两个 commons-lang.jar(一个json用,另一个collection用缺少后者会报以上错)!!!!
已经写对了action但还是会报Could not find action or result这个错误,就是缺少了struts2-json-plugin.
这是我用的jar包
有人还说struts2版本过高还会影响json,这个我就没有试过了,读者可以试下。
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script> <script type="text/javascript"> $(document).ready(function (){ $("#btn").click(function () { var user = $("#user").val(); var psd = $("#psd").val(); alert(user +"***"+ psd); $.ajax({ type:"post", url:"StrutsAjaxJson", data:{user:user,psd:psd}, dataType:"json", success:function(getdata) { alert(getdata); alert("成功"); } }); }); }); </script> </head> <body> <input type="text" id="user" name="user"/> <input type="password" id="psd" name="psd"/> <input type="button" value="提交" id="btn"/> </body> </html
action:
public String execute() { // System.out.println("ddd"); Map<String, String> map = new HashMap<String, String>(); map.put("name", this.user); map.put("psd", this.psd); JSONObject jo = JSONObject.fromObject(map); this.result = jo.toString(); // System.out.println(this.result); return "success"; } //setter, getter省略
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.devMode" value="true" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="json" extends="json-default" namespace="/"> <action name="StrutsAjaxJson" class="test.StrutsAjaxJson"> <result name="success" type="json"> <!-- root的值对应要返回的值的属性 --> <!-- 这里的result值即是 对应action中的 result --> <param name="root">result</param> </result> </action> </package> </struts>
相关文章推荐
- JQuery+Strusts1.x无刷新登录
- 使用 Python 处理 JSON 格式的数据
- Ajax
- $.ajax()方法详解
- jQuery ajax - ajax() 方法
- 使用Ajax实现异步用户名验证
- 使用ajax实现用户登录验证(升级版)
- XML 与 JSON 优劣对比
- 解决Ajax悬停效果,无法遮蔽FLASH的问题
- VBA将excel数据表生成JSON文件
- 再谈Jquery Ajax方法传递到action(补充)
- Dom在ajax技术中的作用说明
- newtonsoft.json解析天气数据出错解决方法
- 使用Ajax实时检测"用户名、邮箱等"是否已经存在
- 探讨Ajax中同步与异步之间的区别
- vbs 解析json jsonp的方法
- Ruby和Ruby on Rails中解析JSON格式数据的实例教程
- Extjs4如何处理后台json数据中日期和时间
- C#实现将类的内容写成JSON格式字符串的方法
- ajax中data传参的两种方式分析