如何手动将Spring MVC视图呈现为html? [英] How to manually render Spring MVC view to html?

查看:420
本文介绍了如何手动将Spring MVC视图呈现为html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在我的控制器映射方法中将我的视图渲染为html,以便我可以将渲染的html作为我的json对象的一部分返回?

Is it possible to render my view into html in my controller mapping method, so that i can return the rendered html as a part of my json object ?

示例我常用的控制器方法:

Example of my usual controller method :

@RequestMapping(value={"/accounts/{accountId}"}, method=RequestMethod.GET)
public String viewAcc(final HttpServletRequest req, 
        final HttpServletResponse resp, final Model model,
        @PathVariable("accountId") final String docId) {

    // do usual processing ...

    // return only a STRING value, 
    //   which will be used by spring MVC to resolve into myview.jsp or myview.ftl
    //   and populate the model to the template to result in html
    return "myview";
}

我的期望:

@RequestMapping(value={"/accounts/{accountId}"}, method=RequestMethod.GET)
public String viewAcc(final HttpServletRequest req, 
        final HttpServletResponse resp, final Model model,
        @PathVariable("accountId") final String docId) {

    // do usual processing ...

    // manually create the view
    ModelAndView view = ... ? (how)

    // translate the view to the html
    //   and get the rendered html from the view
    String renderedHtml = view.render .. ? (how)

    // create a json containing the html
    String jsonString = "{ 'html' : " + escapeForJson(renderedHtml) + "}"

    try {
        out = response.getWriter();
        out.write(jsonString);
    } catch (IOException e) {
        // handle the exception somehow
    }

    return null;
}

我想知道创建视图和渲染视图的正确方法是什么在控制器方法中手动执行html。

I wonder what is the right way to create the view and render the view into html manually within the controller method.

---------更新---------

--------- update ---------

以下是接受答案指南中的工作示例:

Here's the working example from the accepted answer's guidance :

View resolvedView = thiz.viewResolver.resolveViewName("myViewName", Locale.US);
MockHttpServletResponse mockResp = new MockHttpServletResponse();
resolvedView.render(model.asMap(), req, mockResp);
System.out.println("rendered html : " + mockResp.getContentAsString());


推荐答案

尝试自动装配ViewResolver然后调用 resolveViewName(myview,Locale.US)获取视图。

Try autowiring the ViewResolver then calling resolveViewName("myview", Locale.US) to get the View.

然后调用 render()在视图上,传递一个模拟HTTP响应,其响应具有ByteArrayOutputStream,并从ByteArrayOutputStream获取HTML。

Then call render() on the view, passing it a "mock" HTTP response that has a ByteArrayOutputStream for its output, and get the HTML from the ByteArrayOutputStream.

这是从问题中复制的工作示例。 (所以代码实际上是答案)

Here's the working example, copied from the question. (so the code is actually with the answer)

View resolvedView = thiz.viewResolver.resolveViewName("myViewName", Locale.US);
MockHttpServletResponse mockResp = new MockHttpServletResponse();
resolvedView.render(model.asMap(), req, mockResp);
System.out.println("rendered html : " + mockResp.getContentAsString());

这篇关于如何手动将Spring MVC视图呈现为html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆