正在回答 回答被采纳积分+1
1、首先你的jsp文件中的问题,没有写上下面的语句,所以ajax是不起作用的,注意你的js的名字是否和我贴出的代码一致
2、jsp中的<tbody>没有写id,这样的话存数据的时候就找不到cont了。
3、下面代码中idd写成id了,</tr>写成<td>了,decription最好换个名字。
4、Servlet注意名字不要和JSON中的JSONArray重名,你重写了就不能用json.jar包中的了
改后的代码如下,注意jsp中的servlet也要改
package cn.java.servlet1; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONArray; /** * Servlet implementation class JSONArray */ @WebServlet("/JSONArray1") public class JSONArray1 extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String flag = request.getParameter("flag"); // 创建五条数据 Goods good1 = new Goods("001", "手机1", 101, "电子商品1"); Goods good2 = new Goods("002", "手机2", 102, "电子商品2"); Goods good3 = new Goods("003", "手机3", 103, "电子商品3"); Goods good4 = new Goods("004", "手机4", 104, "电子商品4"); Goods good5 = new Goods("005", "手机5", 105, "电子商品5"); // 创建List列表,将goods对象填进列表 List<Goods> list = new ArrayList<Goods>(); list.add(good1); list.add(good2); list.add(good3); list.add(good4); list.add(good5); // 创建JSONArray对象,将list存进去 JSONArray jsonArray =new JSONArray(list); // 将数组传递回页面 response.getOutputStream().write(jsonArray.toString().getBytes("utf-8")); } }
祝学习愉快!
老师我把冒号改了,但是为什么还是不能显示数据呢
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class JSONArray
*/
@WebServlet("/JSONArray")
public class JSONArray extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String flag=request.getParameter("flag");
//创建五条数据
Goods good1=new Goods("001","手机1",101,"电子商品1");
Goods good2=new Goods("002","手机2",102,"电子商品2");
Goods good3=new Goods("003","手机3",103,"电子商品3");
Goods good4=new Goods("004","手机4",104,"电子商品4");
Goods good5=new Goods("005","手机5",105,"电子商品5");
//创建List列表,将goods对象填进列表
List<Goods> list=new ArrayList<Goods>();
list.add(good1);
list.add(good2);
list.add(good3);
list.add(good4);
list.add(good5);
//创建JSONArray对象,将list存进去
JSONArray jsonArray=new JSONArray();
//将数组传递回页面
response.getOutputStream().write(jsonArray.toString().getBytes("utf-8"));}
}
public class Goods {
private String id;
private String name;
private Integer num;
private String description;
public Goods(){
}
public Goods(String id,String name,Integer num,String description){
this.id=id;
this.name=name;
this.num=num;
this.description=description;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>商品查询</h1>
<p>
<input type="submit" value="查询" id="search">
</p>
<table width="800px" cellspacing="0px" border="1px" >
<thead>
<tr>
<th>商品id</th>
<th>商品名称</th>
<th>数量</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list }" var="good">
<tr>
<td>${good.id}</td>
<td>${good.name}</td>
<td>${good.num}</td>
<td>${good.description}</td>
</tr>
</c:forEach>
</tbody>
</table>
<script type="text/javascript">
/***根据条件查询**/
$("#search").click(function(){
$.ajax({
url:"<%=basePath%>/JSONArray",
type:"post",
dataType:"json",
success:function(result){
var list=eval(result);
var content=null;
for(var i in list){
var idd=list[i].id;
var goodName=list[i].name;
var goodNum=list[i].num;
var description =list[i].description;
content =content+"<tr><td>"+id+"</td><td>"+goodName+"</td><td>"
+goodNum+"</td><td>"+description+"</td><td>";
$("#cont>tr").remove();
}
$("#cont").html(content);
}
})
})
</script>
</body>
</html>
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星