使用 Spring MVC 和 Apache Tiles 将视图渲染为字符串
我正在尝试在控制器中重用一些图块,该控制器向客户端返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您想要实现一个视图类,它将 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
如果您需要使用 Apache Tiles 2 渲染视图,则必须使用
请参阅此处的示例教程: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
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