Struts 2 从 DAO 得到 Json 进入行动 class
Struts 2 get Json from DAO into the action class
我有一些独特的可能理解问题。我有 jQuery Ajax 调用调用 Struts2 操作 class,后者又调用 DAO 来获取一些数据。但是由于某种原因,我没有得到任何数据或任何错误,当我在操作中添加 DAO 操作时只有一个空白响应 class。出了点问题,我需要一些眼睛来告诉我哪里出了问题,
这是我的 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 Class,
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;
}
这是我的操作 class,
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 等一样工作,但是当我取消注释那部分时,我得到空白 response.So 我猜可能是我的 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
不是一个函数,它是一个语句,参见定义和示例用法 here。您需要修改查询
rs = stmt.executeQuery("select Distinct city from cities_extended WHERE state_code = '" + state_code + "'");
在此更改之后,一切都应该像 log4j2 等一样工作。
我有一些独特的可能理解问题。我有 jQuery Ajax 调用调用 Struts2 操作 class,后者又调用 DAO 来获取一些数据。但是由于某种原因,我没有得到任何数据或任何错误,当我在操作中添加 DAO 操作时只有一个空白响应 class。出了点问题,我需要一些眼睛来告诉我哪里出了问题,
这是我的 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 Class,
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;
}
这是我的操作 class,
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 等一样工作,但是当我取消注释那部分时,我得到空白 response.So 我猜可能是我的 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
不是一个函数,它是一个语句,参见定义和示例用法 here。您需要修改查询
rs = stmt.executeQuery("select Distinct city from cities_extended WHERE state_code = '" + state_code + "'");
在此更改之后,一切都应该像 log4j2 等一样工作。