Im trying to retrieve data from DB using hibernate ORM and get the out-put as json result using Struts2.
Everything work up to retrieving data from DB,
but for the json result I get only {}
.
I think I have done something wrong with my coding. But need some help to figure it out.
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;
...
Is there anything wrong with the includeProperties?(Only place I can think of..) Can any one suggest a way.. I 've tried everything...
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"})})
...
As json output it gives me : {"sample":"working"} which means it works fine. So why it is not working with the ArrayList??
Struts2 JSON plugin will serialize your whole action, including all the (non-transient) properties with a getter.
Since you are hiding your variables (definitely not a best practice, especially because it forces you to manually write every getter and setter... brr), and you have different names for the variable and for the getter, you are pointing the variable, but you should point the getter (then currency
instead of _currency
):
@Action(value = "currencies", results = {
@Result(name = "success",
type = "json",
params = {"includeProperties","currency\\[\\d+\\]\\..*"})
})
Also note that you can specify a root object, that is often preferred to the includeProperties
technique, as described here:
@Action(value = "currencies", results = {
@Result(name = "success",
type = "json",
params = {"root","currency"})
})
来源:https://stackoverflow.com/questions/33469986/giving-an-empty-json-result-while-executing-struts-2-action-class