1

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??

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36
  • **http://localhost:8080/Tiendaya_Project_Final/currencies**-->(action value) how I execute my action class . It gives me empty json result. **{}-only** – Madushan Perera Nov 02 '15 at 03:12
  • Aren't you using that JSON result any where ? .. How could you say JSON is empty ? I suspect .. In your JSP or somewhere you are referring to wrong Json List .. – Rookie007 Nov 02 '15 at 03:14
  • Before I get result to my JSP , I just check the json object I 'm getting from the server. As I have mentioned above it is empty. So even If I set it to jsp it should gives me an empty html element.(Ex: think about setting it to a choice box ) – Madushan Perera Nov 02 '15 at 03:26
  • `_currency\\[\\d+\\]\\..*` what you want to do with this ? – Rookie007 Nov 02 '15 at 04:05
  • `_currency\\[\\d+\\]\\.*` did you try with single dot `.` any way you dont need to use `includeProperties` actually it will serialize all properties in action by default – Rookie007 Nov 02 '15 at 04:12
  • with that regular expression I trying to refer all the member variables of **TiendayaCurrencies class** instead of doing `currency\\[\\d+\\]\\.title,currency\\[\\d+\\]\\.id,currency\\[\\d+\\]\\.code,currency\\[\\d+\\]\\.symbolLeft,` ... etc. I think I should use **includeProperties** to get the json object. – Madushan Perera Nov 02 '15 at 05:01
  • I tried with given answer [link](http://stackoverflow.com/questions/4648288/problem-with-json-plugin-in-struts-2/5665243#5665243) . but still getting empty json. – Madushan Perera Nov 02 '15 at 05:08
  • I tried with **private List _currency=new ArrayList<>();** but still the same. Dont know what goes wrong here.Everything looks fine for me.. – Madushan Perera Nov 02 '15 at 05:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93942/discussion-between-madushan-perera-and-babel). – Madushan Perera Nov 02 '15 at 05:20
  • its not mandatory to use `includeProperties` .. `includeProperties` is intended to specify particular properties to serialize .. which is like improving performance if you are not concern of performance try with out `prams` – Rookie007 Nov 02 '15 at 06:08
  • please check my edit – Madushan Perera Nov 02 '15 at 06:35

1 Answers1

2

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"})
})
Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • 2
    Thank u so much man ... You save another day of mine... I was wasting so much time , looking for a solution to this stupid **_currency** word error.Thank you again for the answer. I gonna up vote it again if I could.. – Madushan Perera Nov 02 '15 at 11:33