我試圖在控制器中重新使用一些控制器,這些控制器向客戶端返回一個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