Search code examples
jqueryjspstruts2jqgridognl

Struts 2 accessing class properties with JQuery Grid


I'm trying to learn about the struts2-jQuery-plugin, more specifically the struts2-jQuery-grid-plugin. For now I've replicated the tutorial provided by the developers HERE. I've been successful in displaying inside the grid such a simple class as Customer (in their example), which I can reference just fine from the mentioned grid inside the JSP and works perfectly fine:

 <sjg:grid
    id="gridtable"
    caption="Customer Examples"
    dataType="json"
    href="%{remoteurl}"
    pager="true"
    gridModel="gridModel"
    rowList="10,15,20"
    rowNum="15"
    rownumbers="true">

    <sjg:gridColumn name="id" index="id" title="ID" formatter="integer" sortable="false"/>
    <sjg:gridColumn name="name" index="name" title="Name" sortable="true"/>
    <sjg:gridColumn name="country" index="country" title="Country" sortable="false"/>
    <sjg:gridColumn name="city" index="city" title="City" sortable="false"/>
    <sjg:gridColumn name="creditLimit" index="creditLimit" title="Credit Limit" formatter="currency" sortable="false"/>
</sjg:grid>

But things start to complicate whenever I try to use not this simple Customer class but another from my own, for example let's say class Reservation (I'm posting examples as I dont want to complicate the question with my own project code):

public class Reservation {

 private Customer customer;
 private Car vehicle;

 public setCustomer(Customer customer){
 this.customer = customer;
 }
 public setCar(Car vehicle){
 this.vehicle = vehicle;
 }

}

So how do I access now the properties/fields inside Customer, which is also inside Reservation? Coming from Struts2 and using <s:property /> tags before using this jQuery plugin, I've been trying to do so like this, as the grid inside the JSP receives a JSON containing all the data:

<sjg:gridColumn name="customer.city" index="city" title="City" sortable="false"/>

But it's not working as my grid columns show empty..

Also is it possible to apply any OGNL expresion to the fields displayed? Let's say I want to uppercase the content of a column, before I started using jQuery Grid, I could manage this by applying .toUpperCase() to the property I displayed, but now using this plugin I'm a bit lost.

UPDATE

I've been able to confirm that it's possible to access the "complex" object using the notation I displayed (tested with other "complex" class). So I think my problem lays inside the JSON generation of my specific class (the one I didn't write down so I don't make the question more complex)... I'll try to take a look inside the generated JSON passed to the Grid and let you know what happens there.

SOLVED: Found a nullpointer inside the JSON generation,so I fixed it. The problem layed there. Accepted answer goes to @Andrea as he pointed out how to access the inner objects of an object, which was my initial question (although I was doing it properly but failed due to the nullpointer issue).


Solution

  • how do I access now the properties/fields inside Customer, which is also inside Reservation?

    Exactly the way you are doing. If it doesn't work, you are doing something wrong.

    Try changing the index to the main object, btw:

    <sjg:gridColumn name="customer.city" index="customer" title="City" sortable="false"/>
    

    And ensure you have a List<Reservation>, and that all your objects (Reservation, Customer, Car, etc...) have a no-args constructor (either implicit or declared), and that every getter and setter is correctly declared.