struts2从DAO获取Json到动作类中



我有一些独特的可能理解问题。我有jQuery Ajax调用调用Struts2动作类,这反过来调用DAO来获得一些数据。然而,由于某种原因,我没有得到任何数据或任何错误,当我在action类中添加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一样硬编码值时,它会给我一个完美的结果。

你应该得到SQL异常在行

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

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

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

在此更改之后,一切都应该像log4j2等一样工作

相关内容

  • 没有找到相关文章

最新更新