Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 无法在jtable的选项字段中填充数据_Json_Jsp_Servlets_Jquery Jtable - Fatal编程技术网

Json 无法在jtable的选项字段中填充数据

Json 无法在jtable的选项字段中填充数据,json,jsp,servlets,jquery-jtable,Json,Jsp,Servlets,Jquery Jtable,我有一个jsp页面,在其中实现了J-Table。我在jtable中有一个名为ClassID的字段,我想将其作为下拉列表。那么,我是如何做到这一点的: $('#UserTableContainer').jtable({ title : 'Table of Users', actions : { listAction : 'CRUDController?action=list', fields :

我有一个jsp页面,在其中实现了J-Table。我在jtable中有一个名为ClassID的字段,我想将其作为下拉列表。那么,我是如何做到这一点的:

$('#UserTableContainer').jtable({
            title : 'Table of Users',
            actions : {
                listAction : 'CRUDController?action=list',
            fields : {

    ClassID : {
    title : 'ClassID',
    list : true,
    width : '50%',
    edit : true,
    option:'CRUDController?action=getClassID'
    },
我的模型,即BEAN类是:

private int id;
    private String name;
    private String classID;

public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

public String getClassID() {
        return classID;
    }
    public void setClassID(String classID) {
        this.classID = classID;
    }
当调用listAction:'CRUDController?action=list'时,我从servlet向jsp获取jtable中填充的数据

但是当控件到达这一行时:选项:'CRUDController?action=getClassID'它转到servlet类的dopost方法,搜索action=getClassID,然后创建jsonArray。但是当控件返回到jsp页面时,它无法填充JTable中的下拉列表

我的servlet代码是:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (request.getParameter("action") != null) {
    List<UserModel> lstUser = new ArrayList<UserModel>();
    String action = (String) request.getParameter("action");
    Gson gson = new Gson();
    response.setContentType("application/json");
        if (action.equals("list")) {
        try {
        // Fetch Data from User Table
        lstUser = daoForMat.getAllUserList();
        // Convert Java Object to Json
        JsonElement element = gson.toJsonTree(lstUser, new TypeToken<List<UserModel>>() {
        }.getType());
        JsonArray jsonArray = element.getAsJsonArray();
        String listData = jsonArray.toString();
        // Return Json in the format required by jTable plugin
        listData = "{\"Result\":\"OK\",\"Records\":" + listData + "}";
        response.getWriter().print(listData);
        } catch (Exception ex) {
        String error = "{\"Result\":\"ERROR\",\"Message\":" + ex.getMessage() + "}";
        response.getWriter().print(error);
        ex.printStackTrace();
        }


        else if(action.equals("getClassID") ){
        System.out.println("I came to action getClassID");
        List<String> lstClassID = new ArrayList<String>();
    //Here i am able to get the List containing classID
        lstClassID = GetClassList();
        JsonElement element = gson.toJsonTree(lstClassID , new TypeToken<List<String>>() {
        }.getType());
        JsonArray jsonArray = element.getAsJsonArray();
        String listData = jsonArray.toString();
        // Return Json in the format required by jTable plugin
        listData = "{\"Result\":\"OK\",\"Records\":" + listData + "}";
        response.getWriter().print(listData );
        //return jsonArray;
        }
      }
    }
protectedvoiddopost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
if(request.getParameter(“操作”)!=null){
List lstUser=new ArrayList();
String action=(String)request.getParameter(“action”);
Gson Gson=新的Gson();
setContentType(“应用程序/json”);
if(action.equals(“列表”)){
试一试{
//从用户表中获取数据
lstUser=daoForMat.getAllUserList();
//将Java对象转换为Json
JsonElement元素=gson.toJsonTree(lstUser,new-TypeToken()){
}.getType());
JsonArray JsonArray=element.getAsJsonArray();
String listData=jsonArray.toString();
//以jTable插件所需的格式返回Json
listData=“{\”结果\“:\”确定\“,\”记录\“:“+listData+”}”;
response.getWriter().print(listData);
}捕获(例外情况除外){
字符串错误=“{\”结果\“:\”错误\“,\”消息\“:“+ex.getMessage()+”}”;
response.getWriter().print(错误);
例如printStackTrace();
}
else if(action.equals(“getClassID”)){
System.out.println(“我开始行动getClassID”);
List lstClassID=newarraylist();
//在这里,我可以得到包含classID的列表
lstClassID=GetClassList();
JsonElement元素=gson.toJsonTree(lstClassID,newtypetoken()){
}.getType());
JsonArray JsonArray=element.getAsJsonArray();
String listData=jsonArray.toString();
//以jTable插件所需的格式返回Json
listData=“{\”结果\“:\”确定\“,\”记录\“:“+listData+”}”;
response.getWriter().print(listData);
//返回jsonArray;
}
}
}
我做错了什么?我为action.equals(“getClassID”)编写的代码与为if(action.equals(“list”)编写的代码相同**对于后面的条件,我可以填充Jtable,但是对于这个条件-action.equals(“getClassID”),我无法填充Jtable中的下拉列表

唯一的区别是当
action=list
我正在为响应编写一个BEAN类列表时。例如,
list lstUser=new ArrayList();
正如您在servlet代码中看到的那样 当
action=getClassID
我正在向响应写入一个字符串列表,即
list lstClassID=new ArrayList();

我应该在jsp页面中JTABLE的选项字段中写些什么,以便填充下拉列表?**期待您的解决方案。提前感谢您,您可以尝试一下

声明一个字段,您将使用它作为下拉列表,如下所示

                     Location:
                          {
                            title: 'Location',
                            width: '12%',
                            list: true,
                            options: '/JTablePractice.aspx/GetContinentalOptions',
                        },
把这个写在你的前端

 public static object GetContinentalOptions()
    {
        using (var db = new ASPPracticesEntities1())
        try
        {

            var numbers = db.Members.Select(c => new { DisplayText = c.Location, Value = c.Location }).ToList();
            return new { Result = "OK", Options = numbers };
        }
        catch (Exception ex)
        {
            return new { Result = "ERROR", Message = ex.Message };
        }
    }

Donno如何用java编写它,但希望它能帮助你

至少有人能给我一个例子,用JSPServlet填充jQueryJTable的下拉列表吗?