Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render multiple views within a single request

I'm trying to return multiple views within a single request, returning them all in a JSON string.

Example:

@RequestMapping(value = "my-request")
public void myRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    Map<String,Object> model1 = new Hashtable<String,Object>();
    model1.put(...);
    ModelAndView modelAndView1 = new ModelAndView("path/to/view1", model1);
    // Render modelAndView1 in some way, in order to obtain the rendered HTML as a String

    Map<String,Object> model2 = new Hashtable<String,Object>();
    model2.put(...);
    ModelAndView modelAndView2 = new ModelAndView("path/to/view2", model2);
    // Render modelAndView2 in some way, in order to obtain the rendered HTML as a String

    // Now write a JSON String to the HttpServletResponse containing both the rendered views (strings).
    // (this is not part of my problem, I'm able to do it as long as I have the two strings)
}

I'm using Spring MVC with Tiles 2.

Can anyone help me?

Update 1 - View names are resolved using a ViewResolver:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/tiles/" />
    <property name="suffix" value=".jsp" />
</bean>

Update 2 - I've created a github repository containing a very small example reproducing the problem.

like image 378
satoshi Avatar asked Mar 14 '12 15:03

satoshi


1 Answers

UPDATE: There are some approaches for solving your issue. I'd try this one, but maybe there are cleaner ways.

@Component
public class JsonMultipleViewFactory {

    @Autowired private List<ViewResolver> viewResolverList;

    public View getView(List<ModelAndView> mavList) {
        for (ModelAndView mav : mavList) {
            if (mav.getView()==null) {
                mav.setView(resolve(mav.getViewName()));
            }
        }
        return new JsonMultipleView(mavList);
    }

    private View resolve(String viewName) {
        for (ViewResolver vr : viewResolverList) {
            View view = vr.resolve(viewName, LocaleContextHolder.getLocale());
            if (view!=null) {
                return view;
            }
        }
        return null;
    }
}

public class JsonMultipleView implements View {

    private final List<ModelAndView> mavList;

    public JsonMultipleView(List<ModelAndView> mavList) {
        this.mavList = mavList;
    }

    public String getContentType() { 
        return "application/json"; 
    }

    public void render(Map<String,?> model, HttpServletRequest request, HttpServletResponse response) {
        Json json = new Json(); // You can use several Json libraries here
        for (ModelAndView mav : mavList) {
            MockHttpServletResponse mockResponse = new MockHttpServletResponse();
            mav.getView().render(mav.getModel(), request, mockResponse);
            json.add(mav.getViewName(), mockResponse.getContentAsString());
        }
        json.write(response.getOutputStream());
        response.getOutputStream().close();
    }
}

And can be used like this:

@Autowired private JsonMultipleViewFactory jsonMultipleViewFactory;

@RequestMapping(value = "my-request")
public View myRequest() {
    ...
    List<ModelAndView> mavList = new ArrayList<ModelAndView>();
    mavList.add(modelAndView1);
    mavList.add(modelAndView2);
    return jsonMultipleViewFactory.getView(mavList);
}
like image 139
sinuhepop Avatar answered Oct 19 '22 01:10

sinuhepop