1

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).

Roman C
  • 49,761
  • 33
  • 66
  • 176
JorgeGRC
  • 1,032
  • 2
  • 18
  • 37

1 Answers1

1

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.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thanks for your response @Andrea . First I ensured I had a List inside my gridModel when the action executes, and all getters and setters are placed. I also tried changing the index but it didn't work either. Also checked I havn't forgotten the empty constructor, nor any getters or setters... It's weird because Im testing within the same action with the two cases, first using just the "simple" class and when I see it shows correctly all data, I try to switch to the "complex" class but it shows nothing.. :( – JorgeGRC Dec 02 '14 at 15:47
  • Maybe there's some error while generating the JSON for the grid and I'm not noticing it, and that would explain why it doesn't show anything. In the past I had some issues with parsing null values with JSON (`Vehicle`in `Reservation`could be null). – JorgeGRC Dec 02 '14 at 15:51
  • `a List inside my gridModel` :| the list must be THE model itself, not being inside it – Andrea Ligios Dec 02 '14 at 15:52
  • XD yes, sorry for my rusty english, inside my action I've got a `List gridModel` :P. Apologies again :) – JorgeGRC Dec 02 '14 at 15:54
  • could you tell me something about my tiny second question? I wondered if it is possible to apply OGNL to any of the data rows inside the grid, for example for uppercasing a field, or do I have to manipulate data earlier inside the action?. – JorgeGRC Dec 03 '14 at 09:07
  • No, you can't use OGNL on JSON... you can do it through a formatter: http://stackoverflow.com/a/22408093/1654265 (maybe read my question too, to understand the answer). Uppercase, show hide, enabled disabled... handle them with formatters, both out-of-the-box (ready to use) or, like in my case, custom created – Andrea Ligios Dec 03 '14 at 09:44
  • Okay many thanks, I'll take a look.. but I feel like I won't be able to archieve the same things I could do with OGNL now using JQuery Grid :( – JorgeGRC Dec 03 '14 at 09:47
  • If my (linked) answer is not enough, feel free to ask a new question about this formatting problems. – Andrea Ligios Dec 03 '14 at 09:48
  • P.S: also consider upvoting if you find something useful, thanks :) – Andrea Ligios Dec 03 '14 at 09:49