您的位置:首页 > 编程语言 > Java开发

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包



有人还说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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts ajax jar json