使用 Spring MVC 和 Apache Tiles 将视图渲染为字符串

发布于 2024-10-10 21:14:32 字数 756 浏览 4 评论 0原文

我正在尝试在控制器中重用一些图块,该控制器向客户端返回 json 响应。我想返回类似于以下格式的 json 响应:

{ 
 'success': <true or false>,
 'response': <the contents of an apache tile>
}

在我的控制器中,我想执行类似于此伪代码的逻辑:

boolean valid = validator.validate(modelObj)
String response = ""
if(valid){
     response = successView.render() // im looking for a way to actually accomplish        
                                     // this, where the successView is the apache tiles view.
                                     // I would also need to pass a model map to the view somehow.
}else{
     response = errorView.render() 
}
writeJsonResponse(httpResponse, /* a Map whose json representation looks like the one I described above. */)

I am trying to reuse some of my tiles in a controller which is returning a json response to the client. I would like to return a json response similar to the following format:

{ 
 'success': <true or false>,
 'response': <the contents of an apache tile>
}

In my controller I would like to perform logic similar to this pseudocode:

boolean valid = validator.validate(modelObj)
String response = ""
if(valid){
     response = successView.render() // im looking for a way to actually accomplish        
                                     // this, where the successView is the apache tiles view.
                                     // I would also need to pass a model map to the view somehow.
}else{
     response = errorView.render() 
}
writeJsonResponse(httpResponse, /* a Map whose json representation looks like the one I described above. */)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放肆 2024-10-17 21:14:32

我相信您想要实现一个视图类,它将 jsp 的输出包装在 json 中。有问题的类可能是org.springframework.web.servlet.view.tiles2.TilesView

另一种选择可能是扩展 JSON 转换器。 org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

I belive that you want to implement a view class that will wrap the output of a jsp in json. The class in question may be org.springframework.web.servlet.view.tiles2.TilesView.

Another option may be to extend the JSON converter. org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

一梦等七年七年为一梦 2024-10-17 21:14:32

如果您需要使用 Apache Tiles 2 渲染视图,则必须使用

org.springframework.web.servlet.view.tiles2.TilesViewResolver

请参阅此处的示例教程:http://krams915.blogspot.com/2010/12/spring-mvc-3-tiles-2-integration.html

如果您需要将响应呈现为 JSON,您可以使用 @ResponseBody,它需要 Jackson 在您的类路径中。请参阅此处的示例 http://krams915.blogspot .com/2011/01/spring-mvc-3-and-jquery-integration.html(控制器返回 JSON)。您还可以在 http://krams915.blogspot.com/2010/12/jqgrid-and-spring-3-mvc-integration.html

If you need to render the view using Apache Tiles 2, you must use

org.springframework.web.servlet.view.tiles2.TilesViewResolver

See the example tutorial here: http://krams915.blogspot.com/2010/12/spring-mvc-3-tiles-2-integration.html

If you need to render the response as JSON, you can use the @ResponseBody which requires Jackson in your classpath. See the example here http://krams915.blogspot.com/2011/01/spring-mvc-3-and-jquery-integration.html (The controller returns JSON). You can also see a similar example of the @ResponseBody at http://krams915.blogspot.com/2010/12/jqgrid-and-spring-3-mvc-integration.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文