我想在發生ajax請求時更改佈局。所以我設置爲一個過濾器:Grails:Ajax響應爲空
class AjaxFilters {
def filters = {
ajaxify(controller: '*', action: '*') {
after = { Map model ->
if(model==null) {
model = new HashMap()
}
// only intercept AJAX requests
if (!request.xhr) {
model.put("layout", "mainUsers")
return true
}
// find our controller to see if the action is ajaxified
def artefact = grailsApplication
.getArtefactByLogicalPropertyName("Controller", controllerName)
if (!artefact) { return true }
// check if our action is ajaxified
def isAjaxified = artefact.clazz.declaredFields.find {
it.name == 'ajaxify'
} != null
def ajaxified = isAjaxified ? artefact.clazz?.ajaxify : []
if (actionName in ajaxified || '*' in ajaxified) {
model.put("layout", "ajax")
return false
}
return true
}
}
}
}
這將創建一個名爲「佈局」,它應該確定使用何種佈局視圖模型。
這裏是使用佈局模型的示例圖:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="${layout}"/>
<title>Profile</title>
</head>
<body>
<h3>Edit Profile</h3>
</body>
</html>
這是控制器:
class SettingsController {
def springSecurityService
static ajaxify = ["profile", "account"]
def profile() {
User user = springSecurityService.currentUser
UserProfile profile = UserProfile.findByUser(user)
if(profile == null) {
flash.error="Profile not found."
return
}
[profile: profile, user: user]
}
}
正常請求按預期方式工作,但是當我嘗試一個AJAX一個響應完全是空的。只發送標題。
我不想返回JSON,我想呈現爲正常的HTML視圖 – Chris