Struts 2从DAO中获取Json到action类中

时间:2015-05-29 15:13:58

标签: java jquery sql json struts2

我有一些独特的理解问题。我有调用Struts2动作类的jQuery Ajax调用,后者又调用DAO来获取一些数据。但是由于某种原因,我没有得到任何数据或任何错误,只是在动作类中添加DAO操作时的空白响应。出了点问题,我需要一些眼睛告诉我出了什么问题,

这是我的ajax电话,

 $.ajax({
            url: "S2",
            //force to handle it as text
            dataType: 'json',
            type: 'GET',
            cache: false,
            contentType: "application/json;charset=utf-8",
            data:{state_code:state_code },
            beforeSend: function (xhr, setting) {
                var url = setting.url;
                url = url.replace("&_=", "&t=");
                setting.url = url;
                },
            success: function (data) {              
                console.log(JSON.stringify(data));
            }
        });

这是我的DAO课程,

public List<BnGetCitiesbyStateCodeBn> GetCitiesbyStateCode(String state_code) throws SQLException {
        List<BnGetCitiesbyStateCodeBn> cities = new LinkedList<>();
        if (dbConnection != null) {
            Statement stmt = dbConnection.createStatement();

            try {
                rs = stmt.executeQuery("select Distinct(city) from cities_extended WHERE state_code = '" + state_code + "'");
                while (rs.next()) {
                    BnGetCitiesbyStateCodeBn city = null;
                    city = new BnGetCitiesbyStateCodeBn();
                    city.setState_code(rs.getString("state_code"));
                    city.setCity(rs.getString("city"));
                    cities.add(city);
                    logger.info("Cities retreived are " + cities);
                    System.out.println(cities);
                }
            } catch (Exception e) {
                logger.info("Error retreving Cities " + e);
            } finally {
                dbConnection.close();
            }
        }
        return cities;

    }

这是我的Action类,

public class S2 extends ActionSupport {

    private static final long serialVersionUID = 5686197289029560661L;
    private static final Logger logger = LogManager.getLogger(S2.class);
    private String state_code;
    private String t;
    private List<BnGetCitiesbyStateCodeBn> cities;

    public S2() {
    }

    public void setState_code(String state_code) {
        this.state_code = state_code;
    }

    public String getT() {
        return t;
    }

    public void setT(String t) {
        this.t = t;
    }

    public List<BnGetCitiesbyStateCodeBn> getCities() {
        return cities;
    }

    public void setCities(List<BnGetCitiesbyStateCodeBn> cities) {
        this.cities = cities;
    }

    public String getState_code() {
        logger.info("State code is " + state_code);
        return state_code;
    }

    @Override
    public String execute() {
        try {
            GetCitiesbyStateCode citydao = new GetCitiesbyStateCode();
            cities = citydao.GetCitiesbyStateCode(state_code);
            System.out.println(cities);
        } catch (SQLException ex) {
            logger.error(ex);
            System.out.println(ex);
        }
        logger.info("log4j2 works for Struts Method.Inside Execute Method of S2 Action Class");
         System.out.println("Inside action class");
        return "success";
    }
}

这是我的struts xml,

 <package name="json" namespace="/" extends="json-default">  
     <action name="S2" class="json.S2" method="execute">
            <result type="json"></result>
        </action>
    </package>

这是我的豆,

public class BnGetCitiesbyStateCodeBn {
    private String city;
    private String state_code;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState_code() {
        return state_code;
    }

    public void setState_code(String state_code) {
        this.state_code = state_code;
    }
}

每当我注释掉对DAO的调用时,我都会得到json响应,并且所有内容都像log4j2等一样,但是当我取消注释那部分时,我得到空白的响应。所以我猜我的json配置可能缺少某些东西

感谢有人可以看看并告诉我。

好的我觉得我找到了原因,

logger.info("state code in dao class is " + state_code);
                try {
                    rs = stmt.executeQuery("select Distinct city,state_code from cities_extended WHERE state_code = 'AL'");
                } catch (SQLException ex) {
                    logger.error("SQL Exception executing query" +ex);
                }

当我从ui传递state_code时,它变得混乱,当我像AL一样对值进行硬编码时,它给我的结果非常好。参数名称中的下划线是否会造成任何伤害?

1 个答案:

答案 0 :(得分:1)

你应该在

行获得SQL异常
rs = stmt.executeQuery("select Distinct(city) from cities_extended WHERE state_code = '" + state_code + "'");

distinct不是函数,它是一个语句,请参阅定义和示例用法here。您需要修改查询

rs = stmt.executeQuery("select Distinct city from cities_extended WHERE state_code = '" + state_code + "'");

在这次改变之后,一切都应该像log4j2等一样工作。