SSH中使用ajax获取json数据,报the server responded with a status of 500,附带所有源码,和80分奖励!

huwang14 2016-07-29 11:30:34
刚接触ajax不久,写了一段代码在ajax中获取action返回的json数据,从网上找了一段测试代码(直接下载的整个工程),可以跑起来,但是把这些代码copy到自己工程中却一直报错,请各位帮忙分析一下,送上80分不成敬意,谢谢了!这个问题卡了我2天了,愁死了,请各位大神指点迷津,万分感谢!!!!!!!
这是action的源代码:
package action;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class JsonAction extends ActionSupport implements ServletRequestAware{
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private String result;
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String excuteAjax(){
try {
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String position = request.getParameter("position");
Map<String,Object> map = new HashMap<String,Object>();
map.put("name", name);
map.put("age",age);
map.put("position", position);
JSONObject json = JSONObject.fromObject(map);
result = json.toString();
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
}

这是前端页面的代码:
<%@ 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>Simpleton Demo | struts+ajax返回json类型数据</title>

<link rel="shortcut icon" type="image/x-icon" href="images/Icon.png" />
<link rel="stylesheet" type="text/css" href="css/base.css" />

</head>
<body background="images/bg.gif">

<div id="div_json">
<h5>录入数据</h5>
<br />
<form action="#" method="post">
<label for="name">姓名:</label><input type="text" name="name" />
<label for="age">年龄:</label><input type="text" name="age" />
<label for="position">职务:</label><input type="text" name="position" />
<input type="button" class="btn" value="提交结果"/>
</form>
<br />
<h5>显示结果</h5>
<br />
<ul>
<li>姓名:<span id="s_name">赞无数据</span></li>
<li class="li_layout">年龄:<span id="s_age">暂无数据</span></li>
<li class="li_layout">职务:<span id="s_position">暂无数据</span></li>
</ul>
</div>

<div id="authorgraph"><img alt="" src="images/autograph.gif"></div>

<script type="text/javascript" src="js/jquery-1.8.2.js"></script>
<script type="text/javascript">
function btn(){
var $btn = $("input.btn");//获取按钮元素
$btn.bind("click",function(){

$.ajax({
type:"post",
//需要用来处理ajax请求的action,excuteAjax为处理的方法名,JsonAction为action名
url:"excuteAjaxJsonAction",
//设置数据源
data:{
name:$("input[name=name]").val(),
age:$("input[name=age]").val(),
position:$("input[name=position]").val()
},
dataType:"json",
success:function(data){
//将数据转换成json类型,可以把data用alert()输出出来看看到底是什么样的结构
var d = eval("("+data+")");
$("#s_name").text(""+d.name+"");
$("#s_age").text(""+d.age+"");
$("#s_position").text(""+d.position+"");

},
error:function(){
alert("系统异常,请稍后重试!");
}//这里不要加","
});
});
}

/* 页面加载完成,绑定事件 */
$(document).ready(function(){
btn();//点击提交,执行ajax
});
</script>
</body>
</html>

这是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>
<include file="struts-default.xml" />
<package name="ynwi" extends="struts-default,json-default">
<action name="*JsonAction" method="{1}" class="action.JsonAction">
<result name="fail"></result>
<result type="json">
<param name="root">result</param>
</result>
</action>
</package>
</struts>


这是web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>SSH Project</display-name>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>

</web-app>


以下是我引入的jar包:




这是chrome浏览器控制台报的错:

====================================================================================
报错信息在一楼!



...全文
2504 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangwwip 2016-08-23
  • 打赏
  • 举报
回复
引用 2 楼 huwang14 的回复:
原因找到了,把xwork的jar包替换成低版本的即可! struts2项目启动时报错: java.lang.ClassNotFoundException:org.apache.commons.lang.xwork.StringUtils。 是因为struts2-convention-plugin-xxxxxx.jar 某个版本的SEOActionNameBuilder类引用了 org.apache.commons.lang.xwork.StringUtils; 此类存在于xwork-core-2.1.6.jar,更高的版本已经没有这个类了。 解决的方法是,使用高版本的struts2-convention-plugin-xxxxxx.jar,最好使用struts2 lib中配套的jar。
这算自问自答吧
nikyotensai 2016-08-23
  • 打赏
  • 举报
回复
引用 2 楼 huwang14 的回复:
原因找到了,把xwork的jar包替换成低版本的即可! struts2项目启动时报错: java.lang.ClassNotFoundException:org.apache.commons.lang.xwork.StringUtils。 是因为struts2-convention-plugin-xxxxxx.jar 某个版本的SEOActionNameBuilder类引用了 org.apache.commons.lang.xwork.StringUtils; 此类存在于xwork-core-2.1.6.jar,更高的版本已经没有这个类了。 解决的方法是,使用高版本的struts2-convention-plugin-xxxxxx.jar,最好使用struts2 lib中配套的jar。
赞一个
海澜百丈冰 2016-08-22
  • 打赏
  • 举报
回复
package action;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class JsonAction extends ActionSupport implements ServletRequestAware{
    private static final long serialVersionUID = 1L;   
    private HttpServletRequest request;
    private String result;
    public void setServletRequest(HttpServletRequest arg0) {
        this.request = arg0;
    }
    public String getResult() {
        return result;
    }
    public void setResult(String result) {
        this.result = result;
    }
    /*加个返回的*/
    public String excuteAjax(HttpServletResponse response){   
        try {
            String name = request.getParameter("name");
            int age = Integer.parseInt(request.getParameter("age")); 
            String position = request.getParameter("position");
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("name", name);
            map.put("age",age);
            map.put("position", position);           
            JSONObject json = JSONObject.fromObject(map);
            result = json.toString();
            response.getWriter().print(result );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;//不需要加返回加过的response已经将转化的json传到前端了
    }   
}
Qrcode2 2016-08-19
  • 打赏
  • 举报
回复
统一管理包的好处....尽量不要自己配,自己配也要搭配着
weige_li 2016-08-18
  • 打赏
  • 举报
回复
建议使用maven管理jar包
wuyukunbi 2016-08-02
  • 打赏
  • 举报
回复
不错。。。。。。。
今天晴 2016-08-01
  • 打赏
  • 举报
回复
那为什么别人的可以,你copy过来就不可以了呢
Bug开发攻城狮 2016-08-01
  • 打赏
  • 举报
回复
nice 一起学习
I,Frankenstein 2016-07-30
  • 打赏
  • 举报
回复
不错不错,散分吧!
阳光越来越暖 2016-07-30
  • 打赏
  • 举报
回复
恭喜.....
huwang14 2016-07-30
  • 打赏
  • 举报
回复
原因找到了,把xwork的jar包替换成低版本的即可! struts2项目启动时报错: java.lang.ClassNotFoundException:org.apache.commons.lang.xwork.StringUtils。 是因为struts2-convention-plugin-xxxxxx.jar 某个版本的SEOActionNameBuilder类引用了 org.apache.commons.lang.xwork.StringUtils; 此类存在于xwork-core-2.1.6.jar,更高的版本已经没有这个类了。 解决的方法是,使用高版本的struts2-convention-plugin-xxxxxx.jar,最好使用struts2 lib中配套的jar。
huwang14 2016-07-29
  • 打赏
  • 举报
回复
这是eclipse控制台报的错: 严重: Servlet.service() for servlet [default] in context with path [/SSHProject] threw exception [Filter execution threw an exception] with root cause java.lang.ClassNotFoundException: org.apache.commons.lang.xwork.StringUtils at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1858) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1701) at org.apache.struts2.json.SerializationParams.<init>(SerializationParams.java:57) at org.apache.struts2.json.JSONResult.writeToResponse(JSONResult.java:214) at org.apache.struts2.json.JSONResult.execute(JSONResult.java:204) at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:374) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:278) at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:510) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)

81,111

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧