1

I'm using Spring MVC, and I need return in the Controller, a Json object that contains the view, by example, the related jsp page.

return: { name: "fragment-form", other-info:"other-info", view: view}

where "view" should be the JSP page linked to your ModelAndView

I read other post, but I not find the solution, because I need that controller to the work, if it's posible.

Sugestions?

EDIT:

I have a form with your values, and the submit, from javascript execute this follow code:

@RequestMapping(method = RequestMethod.POST)
public Object create(@Valid @RequestBody PatientForm form, HttpServletResponse response) {
  ModelMap map = new ModelMap();
  map.put("form", form);

return new ModelAndView("addPatientForm", map); }

I need return a Json where the "ModelAndView("addPatientForm", map)" processed within the json that is returned.

Cristian Rinaldi
  • 329
  • 4
  • 15
  • Linked to which ModelAndView? The one that is tied to the page from which you are executing the JSON request? – Beau Grantham Apr 25 '12 at 13:26
  • I edit the post with changes. – Cristian Rinaldi Apr 25 '12 at 17:37
  • 1
    It's still not really clear what you want your controller method to return. Just JSON? Why would you include a Spring MVC view name in the returned JSON? Do you have a custom View implementation that's going to process the JSON you return from your controller before returning a response to the client or something? – sdouglass Apr 25 '12 at 19:32
  • It sounds like the OP wants to render a normal view (jsp/html) but then return this as one part of a JSON response... maybe? – beerbajay Apr 25 '12 at 20:38
  • Yes beerbajay, that is the goal, return a normal view as part of a Json. By example: { name: "fragment-form", other-info:"other-info", view: "

    HOLA

    "} where "

    HOLA

    " is my JSP view.
    – Cristian Rinaldi Apr 26 '12 at 12:13

1 Answers1

0

Would something like this work for you:

@RequestMapping(value="/somepage", method=RequestMethod.POST)
    public ResponseEntity<String> generateViewasJSON(){
        JSONObject json = new JSONObject();
json.put("name","fragment-form");
        ...
        HttpHeaders headers = new HttpHeaders();
        headers.set(  "Content-Type", "application/json" );
        return new ResponseEntity<String>( json.toString(), headers, HttpStatus.OK );
    }
Prashant Saraswat
  • 838
  • 1
  • 8
  • 20