Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Dojo Grid sort with Spring and JSON

So I've got a webapp that uses Dojo and Spring. I've got some grids in the app that are loaded using Json dojo stores. Things seem alright, but I'm now trying to implement sorting. According to this link dojo does not sort grids from Stores, instead it leaves that up to the server. Ok, I assume I can work with that, however I'm running into problems actually doing it.

To get data into my grids, I have them call a path which is caught by my controller. For example, to load my Job grid, dojo creats the store and calls /job/data. Here is my controller code for that:

@RequestMapping(value="/job/data", method=RequestMethod.GET,
        headers="Accept=application/json")
public @ResponseBody List<Job> getData() {
    return jobService.getAll();
}

I'm using the Jackson Json processor so this works well for returning the data formatted in Json. Dojo gets the data and displays it with no problems. When a user clicks on a column header to sort though, Dojo appends onto the path that is called creating something like this (when sorting on column programName for instance): /job/data?sort(+programName)

My problem is that while this code still calls the above method, I'm not sure how to gain access to this tacked on part. I tried using the @RequestParam parameter but that doesn't work and infact stops the method from working altogether. Here is what I tried:

@RequestMapping(value="/job/data", method=RequestMethod.GET,
        headers="Accept=application/json")
public @ResponseBody List<Job> getData(@RequestParam("sort") String sort) {
    log.info("Not getting here anymore");
}

Anyone have any ideas? I'm wondering if part of the problem is that the tacked on part is not following the normal syntax of ?sort=something. Either way, as I said, adding @RequestParam doesn't work anyway with this method. By that I mean that even if the method is just called as /job/data again with no tacked on parameters, I get an error. Please let me know if you need anyting more from me as this problem is for work and I could really use some insight. Thanks.

UPDATE

I did find a link after tons of searching that almost addresses this issue. But of course the Spring guy himself encounters my problem and his code doesn't fix it. He does mention:

JsonStore sends its "sort" parameter in the form of "sort(+fieldName1,+fieldName2,-fieldName3,...)", thus the entire thing becomes the parameter key. This is inconvenient to handle in Spring MVC as it requires dropping down to HttpServletRequest and iterating through parameter keys to find the one beginning with "sort", as opposed to being able to use the @RequestParam handler argument annotation.

So would someone perhaps know how I would do that?

UPDATE 2

Anyone? I'm just asking if someone can point me in the right direction of how to implement the solution I already found. How do I access the HttpServletRequest object from a spring controller method so that I can gain access to the screwed up sort parameter that Dojo sends?

like image 777
cardician Avatar asked Nov 14 '22 13:11

cardician


1 Answers

If you include HttpServletRequest request to the controller method (it gets automatically injected by spring MVC), you can access the different parameters in the following way:

Set parameters = request.getParameterMap().keySet();
for(Object param : parameters){
  //Here you can process the params to retrieve the names and ordering direction
}
like image 54
Pau Giner Avatar answered Dec 09 '22 21:12

Pau Giner