Render multiple views within a single request

*爱你&永不变心* 提交于 2019-12-07 05:59:35

问题


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.


回答1:


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);
}


来源:https://stackoverflow.com/questions/9705293/render-multiple-views-within-a-single-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!