1

I have a JSONP service where I want to return several HTML fragments along with some other info. I'd like to generate those fragments with an HTML template system.

The response might look something like this:

parseResponse({
    "id": "a684a87ea3sd8",
    "status": "OK",
    "some_list": "<ul><li>item one</li><li>item two</li></ul>",
    "summary": "<p>You have two items</p><p>You're so cool</p>",
    "another_list": "<ul><li>uno</li><li>dos</li><li>microsoft</li></ul>",
    "more_non_html_info": "And he stepped on the ball"
});

So using Spring 3 MVC, in my controller I'd like to be able to feed an object/context into my template system and get an HTML string back. I want to do that a dozen or so times each time the controller is invoked.

I can't find a way to do this using JSP. What's the most straight-forward way to accomplish this?

0

1 Answer 1

0

In your controller you should create a function building your json string

@Controller
public class MyController {
    ....
    public String myfunction(ModelMap model) {
        String jsonReply = "{"
              +"'id':'blabla',"
              +"'status':'blabla',"
              ....
              +"}";


       model.put('json',jsonReply);
       return 'myview';
}  

Then create a view called 'myview.jsp' and in it only display the json object in your model

${json}

This is not the best way to do it but you can start here.

2
  • Thanks for the reply, but I don't see how I can generate and add the HTML fragments from the JSP using this approach. Nov 22, 2011 at 16:31
  • Make an ajax call to you Controller to generate the json string. Then implement the parseResponse function in javascript to add the fragments in your calling JSP.
    – Thomas
    Nov 22, 2011 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.