求助Struts2 的ajax如何接收List

Poom_soFt 2017-12-15 04:50:36
1、在action中,用JSONArray存list,后用json.toString;还是response.getWriter().print(json);
2、前台接收,要么没值,要么unfind。
真的头都大了,弄了一下午,研究不明白的。。。
...全文
588 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
疼碍 2018-01-05
  • 打赏
  • 举报
回复
前台断电看是否返回数据 拿到在线json解析看是否符合 符合的话直接转为json数据

  function showResponse(message) {
        message = eval("("+message+")");
        //处理数据
    }
qq_29781727 2017-12-29
  • 打赏
  • 举报
回复
后台这么写

前台这么写 前台需这个包jquery-1.12.4.js
Obviouss 2017-12-27
  • 打赏
  • 举报
回复
你在前台把err方法加上,把err的错误信息打出来看看报的什么错
Poom_soFt 2017-12-26
  • 打赏
  • 举报
回复
引用 12 楼 ylovep 的回复:
接收list 肯定没有现成的方法去接收 前端传过来的无非就是list格式的json 自己后台需要转换下
问题就是转换用.toString() 还是toArray(); 而且前台不走success 而是error 找不到原因
ylovep 2017-12-26
  • 打赏
  • 举报
回复
接收list 肯定没有现成的方法去接收 前端传过来的无非就是list格式的json 自己后台需要转换下
Poom_soFt 2017-12-26
  • 打赏
  • 举报
回复
引用 10 楼 sky993 的回复:

 return success("保存成功",JSONArray.fromObject(集合));

protected String success(String message,Object data) {
	   JSONObject json=new JSONObject();
	   json.put("status", status);
	   json.put("data", data);
	   return ajaxJsonObject(json);
   }
protected String ajaxJsonObject(JSONObject json) {
       return ajax(json, "text/html");
   }
protected String ajax(Object content, String type) {
        HttpServletResponse response=null;
        try {
            response=getResponse();
            response.setContentType(type + ";charset=UTF-8");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.getWriter().print(content);
        } catch (IOException e) {
            LOG.error(e.getMessage());
        }
        finally{
            try {
                response.getWriter().flush();
                response.getWriter().close();
            } catch (Exception e) {
                LOG.error(e.getMessage());
            }
        }
        return null;
    }
    $.ajax({
                url:"personAction_getPerson",
                type:"get",
                dataType:"json",
                success:function(data){
                     
                   alert(data);   //看看有没有值
                     
                }
            });
还是一样 依然走erro
Poom_soFt 2017-12-22
  • 打赏
  • 举报
回复
引用 8 楼 qq_38936718 的回复:
[quote=引用 7 楼 Vicale_soFt 的回复:] [quote=引用 6 楼 qq_38936718 的回复:]
List list= new ArrayList();
	        
	        Person p1 = new Person("张三","123");
	        Person p2 = new Person("lisi","123");
	        Person p3 = new Person("王五","123");
	        list.add(p1);
	        list.add(p2);
	        list.add(p3);
	        
	         
	        System.out.println(list);
	        String json = JSONArray.fromObject(list).toString();
说的很明确啊 用JSONArray
我之前用的array,前台没值,也没报错,主要是不知道为啥前面不显示[/quote] 现在行了吧[/quote] 不行啊 不报错,不显示
sky993 2017-12-22
  • 打赏
  • 举报
回复

 return success("保存成功",JSONArray.fromObject(集合));

protected String success(String message,Object data) {
	   JSONObject json=new JSONObject();
	   json.put("status", status);
	   json.put("data", data);
	   return ajaxJsonObject(json);
   }
protected String ajaxJsonObject(JSONObject json) {
       return ajax(json, "text/html");
   }
protected String ajax(Object content, String type) {
        HttpServletResponse response=null;
        try {
            response=getResponse();
            response.setContentType(type + ";charset=UTF-8");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.getWriter().print(content);
        } catch (IOException e) {
            LOG.error(e.getMessage());
        }
        finally{
            try {
                response.getWriter().flush();
                response.getWriter().close();
            } catch (Exception e) {
                LOG.error(e.getMessage());
            }
        }
        return null;
    }
    $.ajax({
                url:"personAction_getPerson",
                type:"get",
                dataType:"json",
                success:function(data){
                     
                   alert(data);   //看看有没有值
                     
                }
            });
Poom_soFt 2017-12-21
  • 打赏
  • 举报
回复
引用 6 楼 qq_38936718 的回复:
List list= new ArrayList();
	        
	        Person p1 = new Person("张三","123");
	        Person p2 = new Person("lisi","123");
	        Person p3 = new Person("王五","123");
	        list.add(p1);
	        list.add(p2);
	        list.add(p3);
	        
	         
	        System.out.println(list);
	        String json = JSONArray.fromObject(list).toString();
说的很明确啊 用JSONArray
我之前用的array,前台没值,也没报错,主要是不知道为啥前面不显示
疼碍 2017-12-21
  • 打赏
  • 举报
回复
List list= new ArrayList();
	        
	        Person p1 = new Person("张三","123");
	        Person p2 = new Person("lisi","123");
	        Person p3 = new Person("王五","123");
	        list.add(p1);
	        list.add(p2);
	        list.add(p3);
	        
	         
	        System.out.println(list);
	        String json = JSONArray.fromObject(list).toString();
说的很明确啊 用JSONArray
Poom_soFt 2017-12-21
  • 打赏
  • 举报
回复
引用 2 楼 sositesine 的回复:
第一个问题楼上解答了 第二个问题,你debug看看set方法的时候有值么。 或者代码发来,不然很难说什么原因
用array原因是用object报错
net.sf.json.JSONException: 'object' is an array. Use JSONArray instead
我把所有代码都发出来,大佬们看一下 前台jsp======
<script type="text/javascript">
		$(document).ready(function(){
			$.ajax({
				url:"personAction_getPerson",
				type:"get",
				dataType:"json",
				success:function(data){
					
					$("#show").empty();
					
					$.each(data,function(i,item){
						$("#show").append("<tr><td>"+item.id+"</td><td>"+item.name+"</td><td>"+item.sex+"</td><td>"+item.age+"</td><td>"+item.address+"</td></tr>");	
					});
					
				}
			});
		});
		
	</script>
action:======
public String getPerson(){
		List<Person> list=ps.getPerson();
		
		JSONObject json = JSONObject.fromObject(list);
		result = json.toString();
		try {
			response.getWriter().write(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
struts.xml
<package name="person" extends="struts-default,json-default">
		<action name="personAction_*" class="com.action.PersonAction" method="{1}">
			<result name="s">/success.jsp</result>
			<result  type="json">
				<param name="list">result</param>
			</result>
		</action>
		
	</package>
debug List是有值的
疼碍 2017-12-21
  • 打赏
  • 举报
回复
引用 7 楼 Vicale_soFt 的回复:
[quote=引用 6 楼 qq_38936718 的回复:]
List list= new ArrayList();
	        
	        Person p1 = new Person("张三","123");
	        Person p2 = new Person("lisi","123");
	        Person p3 = new Person("王五","123");
	        list.add(p1);
	        list.add(p2);
	        list.add(p3);
	        
	         
	        System.out.println(list);
	        String json = JSONArray.fromObject(list).toString();
说的很明确啊 用JSONArray
我之前用的array,前台没值,也没报错,主要是不知道为啥前面不显示[/quote] 现在行了吧
hp961218 2017-12-18
  • 打赏
  • 举报
回复
引用 3 楼 hp961218 的回复:
ApplicationContext.getRequest().getValueStack().setAttribute(集合名,集合值);应该没有记错,好像是这样子的
好像写错了,是ActionContext.getContext().getValueStack().setAttribute(集合名,集合值);页面通过OGNL获取集合值
hp961218 2017-12-18
  • 打赏
  • 举报
回复
ApplicationContext.getRequest().getValueStack().setAttribute(集合名,集合值);应该没有记错,好像是这样子的
强长黑粗硬 2017-12-16
  • 打赏
  • 举报
回复
第一个问题楼上解答了 第二个问题,你debug看看set方法的时候有值么。 或者代码发来,不然很难说什么原因
MM_morning 2017-12-15
  • 打赏
  • 举报
回复
JSONObject jsonObject = JSONObject.fromObject(data); String rep_result = jsonObject.toString(); response().getWriter().write(rep_result);

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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