在ExtJs和Servlets中使用postgresql中的Json填充组合框的问题

时间:2014-01-06 07:11:08

标签: json postgresql servlets extjs combobox

在我的Java EE Web应用程序中,表单面板中有一个组合框。我试图使用JsonReader从我的postgresql数据库填充这个组合框。我正在使用ExtJs 3.0和JSP& Sevlets。 这是我的用于组合框和JsonReader的ExtJs代码段。

var vehicleStore = new Ext.data.Store({

proxy : new Ext.data.HttpProxy({
    url : "http://" + host + ":" + port + "/" + projectName + "/"
            + "DeliveryVehicle"
}),
reader : new Ext.data.JsonReader({
    idProperty : 'vCode',
    root : 'vehicles',
    fields : [ {
        name : 'vCode'
    }, {
        name : 'vRegNum'
    } ]
}),
autoLoad : true
});



{
        xtype : 'combo',
        name : 'include_type',
        fieldLabel : 'Delivery Vehicle',
        emptyText : 'Delivery Vehicle',
        editable : false,
        hideLabel : true,
        style : 'marginleft:10px',
        displayField : 'vRegNum',
        valueField : 'vCode',
        store : vehicleStore,
        flex : 1
    }

这是我的DeliveryVehicle.java servlet。

package com.xontworld.ornox.Geotrack;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xontworld.ornox.connection.ConnectionManager;


public class DeliveryVehicle extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public DeliveryVehicle() {
    //super();
    // TODO Auto-generated constructor stub
}

@Override
public void init() throws ServletException {
    // TODO Auto-generated method stub
    super.init();
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}

protected void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    ServletContext context = getServletContext();

    String dbName = context.getInitParameter("ConnectionDB");
    String connectionHost = context.getInitParameter("ConnectionHost");
    String connectionUser = context.getInitParameter("ConnectionUser");
    String connectionPassword = context.getInitParameter("ConnectionPassword");
    String port = "5433";

    Statement statement = null;
    ResultSet vehicleResultSet = null;
    Connection pgConnection = null;
    String lineString = "";

    try {
        pgConnection = ConnectionManager.getPostgresConnection(
                connectionHost, connectionUser, connectionPassword,
                dbName, port);
        out.println(connectionHost+","+ connectionUser+","+ connectionPassword+","+ dbName);
        statement = pgConnection.createStatement();
        out.print(pgConnection);

        String sql = "";


        sql = "select distinct vehiclecode, registrationnumber from hoobtvehicles v WHERE v.status='1'";

        vehicleResultSet = statement.executeQuery(sql);

        String jsonData = "{'vehicles':[";

        while (vehicleResultSet.next()) {
            jsonData += "{ 'vCode' : '";
            jsonData += vehicleResultSet.getString(1).trim();
            jsonData += "', ";
            jsonData += "'vRegNum' : '";
            jsonData += vehicleResultSet.getString(2).trim();

            if (vehicleResultSet.isLast()) {
                jsonData += "' }  ";
            } else {
                jsonData += "' } , ";
            }
        }

        jsonData += "]}";
        out.print(jsonData);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        out.println(e.toString());
        e.printStackTrace();

    }

}

}

这是我的Json数据。

{'vehicles':[{ 'vCode' : '1001', 'vRegNum' : 'XY-100-123' } , { 'vCode' : '1002', 'vRegNum' : 'GY-122-120' } , { 'vCode' : '1000000001', 'vRegNum' : 'XY-100-123' } ]}

问题是它没有加载组合框,甚至在我的eclipse控制台中都没有打印任何内容。并且它也没有在firebug控制台中显示任何错误消息。我使用警报来检查 vehicleStore 的大小。它是零。任何人都知道这里发生了什么。

如果有人愿意解释我的代码有什么问题以及我该如何纠正它,我将非常感激?

非常感谢

0 个答案:

没有答案