ricardojou
2013-01-24 01:24
采纳率: 66.7%
浏览 502
已采纳

Struts2和返回JSON的数据为null,浏览器提示TypeError:data is null

我在action里面打印了一下数据,都是正确的。
[color=red]这是我的action[/color]
package action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import pojo.Dept;
import pojo.User;

import com.opensymphony.xwork2.ActionSupport;

import dao.Dao;

public class JasonAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = -5138636457167442118L;
private List list = new ArrayList();
private Set users = new HashSet();
private Dao dao = new Dao();

public String getUserByDept(){
    users = dao.getUserByDept();
    return SUCCESS;
}
public String getDept(){
    list = dao.getAllDept();
    Dept dept = list.get(0);
    System.out.println(dept.getDname());
    users = dept.getUsers();
    System.out.println(users.size());
    return "list";
}
public List<Dept> getList() {
    return list;
}
public void setList(List<Dept> list) {
    this.list = list;
}
public Set<User> getUsers() {
    return users;
}
public void setUsers(Set<User> users) {
    this.users = users;
}

}
[color=red]这是struts.xml文件[/color]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">




results




[color=red]这是jsp页面[/color]
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">



software
hardware


username:
</select>


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • goodluck_wgw 2013-01-24 13:07
    已采纳

    应该这样写吧
    users


    前台拿到数据遍历$.each(data,function(i,value){

    alert(value.username);
    $("#username").html(""+value.username+"")

    }

    点赞 评论
  • results results应该是你要返回给前台的数据吧,可视后台并没有results应该是users吧

    点赞 评论
  • scriptguy 2013-01-24 19:14

    action里没有results属性的getter/setter方法

    点赞 评论

相关推荐 更多相似问题