我试图在控制器中重新使用一些控制器,这些控制器向客户端返回一个json响应。我想返回类似以下格式的JSON响应:使用Spring MVC和Apache Tiles将字符串渲染为字符串
{
'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. */)
我认为在json中发送html代码不是一个好主意。如果您只想知道请求是否成功,您可以查看响应状态代码(并将其更改为服务器中的任何值),并像往常一样从服务器发送视图。 – Javi 2011-01-06 19:11:10