Announcement Announcement Module
Collapse
No announcement yet.
Model as parameter of Json method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Model as parameter of Json method

    Hello everyone. I've been following this suggestion and I generated the proper json methods to achieve it.
    Currently, I'm looking at this autogenerated non-json method:

    Code:
        @RequestMapping(produces = "text/html")
        public String JavaTecEvtController.list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
            if (page != null || size != null) {
                int sizeNo = size == null ? 10 : size.intValue();
                final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
                uiModel.addAttribute("javatecevts", JavaTecEvt.findJavaTecEvtEntries(firstResult, sizeNo));
                float nrOfPages = (float) JavaTecEvt.countJavaTecEvts() / sizeNo;
                uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
            } else {
                uiModel.addAttribute("javatecevts", JavaTecEvt.findAllJavaTecEvts());
            }
            addDateTimeFormatPatterns(uiModel);
            return "javatecevts/list";
        }
    I have added a json similar counterpart, which works just fine:

    Code:
        @RequestMapping(value = "/ajax", headers = "Accept=application/json")
        @ResponseBody
        public ResponseEntity<String> listJsonWithDelimiters(@RequestParam("page") BigDecimal page, @RequestParam("size") BigDecimal size) {
        	HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json; charset=utf-8");
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            List<JavaTecEvt> result = JavaTecEvt.findJavaTecEvtEntries(firstResult, sizeNo);
            float nrOfPages = (float) JavaTecEvt.countJavaTecEvts() / sizeNo;
            return new ResponseEntity<String>(JavaTecEvt.toJsonArray(result), headers, HttpStatus.OK);
        }
    Now, I want to add to my json version the maxPage handling of the non-json one, more specifically:

    Code:
    uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
    To do that, I changed the above method to:

    Code:
        @RequestMapping(value = "/ajax", headers = "Accept=application/json")
        @ResponseBody
        public ResponseEntity<String> listJsonWithDelimiters(@RequestParam("page") BigDecimal page, @RequestParam("size") BigDecimal size, Model uiModel) {
        	HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json; charset=utf-8");
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            List<JavaTecEvt> result = JavaTecEvt.findJavaTecEvtEntries(firstResult, sizeNo);
            float nrOfPages = (float) JavaTecEvt.countJavaTecEvts() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
    	return new ResponseEntity<String>(JavaTecEvt.toJsonArray(result), headers, HttpStatus.OK);
        }
    Basically adding the missing line to add "maxPages" to the Model, and the Model as an input parameter of the method.

    In my javascript code inside list.jspx I need to access that value, for which I looked around the net and found these two solutions:

    Code:
    var maxpagenumber = "${maxPages}";
    Code:
    var maxpagenumber = '<c:out value="${maxPages}"/>'; (having added the c tag to my jspx namespaces)
    Useless to say, I'm missing something, because it is not getting the value in the javascript end.
    Can anyone tell me what I'm doing wrong?
    Thanks as always for the major help in improving my Spring (roo) knowledge,
    Roby

  • #2
    Your problem is storing things in the Model and expecting the output to contain the model itself. I don't think it does. @ResponseEntity<String> takes over and whatever you stuff in there goes.

    Create a Roo Spring Bean that contains your event response AND the maxPages element. Add a @RooWebJson to it. Serialize that one.

    BTW, as of Spring 3.2, and Roo 1.2.3 - maybe see the new Spring MVC @RequestMapping attributes consumes = and produces = - if you use consumes="application/json" you will be accepting JSON coming IN from the page. If you are SENDING json, you'd use produces="application/json". Makes it easier to supply auto-generated JSON output and convert the input.

    Ken

    Comment

    Working...
    X