执行 struts 2 操作 class 时给出空 json 结果
Giving an empty json result while executing struts 2 action class
我正在尝试使用休眠 ORM 从数据库中检索数据,并使用 Struts2 将输出作为 json 结果。
一切都在努力从数据库中检索数据,
但是对于 json 结果我只得到 {}
。
我想我的编码有问题。但是需要一些帮助才能弄清楚。
Here is my Action class :
@ParentPackage("json-default")
public class SocialIconsAction extends ActionSupport {
private List<TiendayaCurrencies> _currency;
public List<TiendayaCurrencies> getCurrency() {
return _currency;
}
public void setCurrency(List<TiendayaCurrencies> _currency) {
this._currency = _currency;
}
@Action(value = "currencies", results = {
@Result(name = "success", type = "json", params = {"includeProperties",
"_currency\[\d+\]\..*"})})
@Override
public String execute() {
_currency = loadCurrencies();
/*Nothing wrong with the DB results.Just to test everything works fine.*/
//for (TiendayaCurrencies _currency1 : _currency) {
// System.out.println("Title - "+_currency1.getTitle());
// }
return SUCCESS;
}
private List<TiendayaCurrencies> loadCurrencies() {
Session session = com.tiendaya.connection.HibernateUtil.
getSessionFactory().openSession();
List<TiendayaCurrencies> cList = session.
createCriteria(TiendayaCurrencies.class).list();
return cList;
}
}
Pojo class :
public class TiendayaCurrencies{
private Integer id;
private String title;
private String code;
private String symbolLeft;
private String symbolRight;
private char decimalPlace;
...
includeProperties 有什么问题吗?(只有我能想到的地方..)任何人都可以提出一种方法..我已经尝试了一切...
Edit :
public class SocialIconsAction extends ActionSupport {
private List<TiendayaCurrencies> _currency=new ArrayList<>();
private String sample="working";
public String getSample() {
return sample;
}
public void setSample(String sample) {
this.sample = sample;
}
...
@Action(value = "currencies", results = {
@Result(name = "success", type = "json", params = {"includeProperties", "sample"})})
...
作为 json 输出它给我: {"sample":"working"} 这意味着它工作正常。那么为什么它不能与 ArrayList 一起使用??
Struts2 JSON 插件将序列化您的整个操作,包括所有(非瞬态)属性 和 getter。
因为你隐藏了你的变量(绝对不是最佳实践,特别是因为它迫使你手动编写每个 getter 和 setter... brr),并且你有不同的名称变量和 getter,你指向变量,但是 你应该指向 getter(然后 currency
而不是 _currency
):
@Action(value = "currencies", results = {
@Result(name = "success",
type = "json",
params = {"includeProperties","currency\[\d+\]\..*"})
})
另请注意,您可以指定一个根对象,这通常优于 includeProperties
技术,如 described here:
@Action(value = "currencies", results = {
@Result(name = "success",
type = "json",
params = {"root","currency"})
})
我正在尝试使用休眠 ORM 从数据库中检索数据,并使用 Struts2 将输出作为 json 结果。
一切都在努力从数据库中检索数据,
但是对于 json 结果我只得到 {}
。
我想我的编码有问题。但是需要一些帮助才能弄清楚。
Here is my Action class :
@ParentPackage("json-default")
public class SocialIconsAction extends ActionSupport {
private List<TiendayaCurrencies> _currency;
public List<TiendayaCurrencies> getCurrency() {
return _currency;
}
public void setCurrency(List<TiendayaCurrencies> _currency) {
this._currency = _currency;
}
@Action(value = "currencies", results = {
@Result(name = "success", type = "json", params = {"includeProperties",
"_currency\[\d+\]\..*"})})
@Override
public String execute() {
_currency = loadCurrencies();
/*Nothing wrong with the DB results.Just to test everything works fine.*/
//for (TiendayaCurrencies _currency1 : _currency) {
// System.out.println("Title - "+_currency1.getTitle());
// }
return SUCCESS;
}
private List<TiendayaCurrencies> loadCurrencies() {
Session session = com.tiendaya.connection.HibernateUtil.
getSessionFactory().openSession();
List<TiendayaCurrencies> cList = session.
createCriteria(TiendayaCurrencies.class).list();
return cList;
}
}
Pojo class :
public class TiendayaCurrencies{
private Integer id;
private String title;
private String code;
private String symbolLeft;
private String symbolRight;
private char decimalPlace;
...
includeProperties 有什么问题吗?(只有我能想到的地方..)任何人都可以提出一种方法..我已经尝试了一切...
Edit :
public class SocialIconsAction extends ActionSupport {
private List<TiendayaCurrencies> _currency=new ArrayList<>();
private String sample="working";
public String getSample() {
return sample;
}
public void setSample(String sample) {
this.sample = sample;
}
...
@Action(value = "currencies", results = {
@Result(name = "success", type = "json", params = {"includeProperties", "sample"})})
...
作为 json 输出它给我: {"sample":"working"} 这意味着它工作正常。那么为什么它不能与 ArrayList 一起使用??
Struts2 JSON 插件将序列化您的整个操作,包括所有(非瞬态)属性 和 getter。
因为你隐藏了你的变量(绝对不是最佳实践,特别是因为它迫使你手动编写每个 getter 和 setter... brr),并且你有不同的名称变量和 getter,你指向变量,但是 你应该指向 getter(然后 currency
而不是 _currency
):
@Action(value = "currencies", results = {
@Result(name = "success",
type = "json",
params = {"includeProperties","currency\[\d+\]\..*"})
})
另请注意,您可以指定一个根对象,这通常优于 includeProperties
技术,如 described here:
@Action(value = "currencies", results = {
@Result(name = "success",
type = "json",
params = {"root","currency"})
})