在单个请求中呈现多个视图

时间:2012-03-14 15:50:16

标签: json spring spring-mvc tiles

我正在尝试在单个请求中返回多个视图,并将它们全部返回给JSON字符串。

示例:

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

我正在使用带有Tiles 2的Spring MVC。

任何人都可以帮助我吗?

更新1 - 使用ViewResolver解析视图名称:

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

更新2 - 我创建了一个github repository,其中包含一个重现问题的小例子。

1 个答案:

答案 0 :(得分:6)

更新:有一些方法可以解决您的问题。我试试这个,但也许有更清洁的方法。

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

可以像这样使用:

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