您好我一直在尝试为自定义Grails应用程序实现向下钻取类型Breadcrumb导航,但似乎无法弄清楚为什么Crumb对象未保存在Arraylist中。
创建了Breadcrumb并将其添加到列表中,但每当我导航到另一个页面时,列表都会重置。
我知道有一个Grails面包屑插件但是我无法将它用于这个特定的应用程序。
以下是代码的一些示例
域类:
class User {
transient springSecurityService
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
ArrayList <Crumb> BreadCrumbs = new ArrayList<Crumb>();
static auditable = true
static constraints = {
username blank: false, unique: true
password blank: false
enabled(default:true)
}
class Crumb {
String itemName
String url
static belongsTo = User
static constraints = {
itemName(nullable: false)
url(nullable: false)
}
def printData(){
println("Crumb Data PrintOut: \nName: " + itemName +"\nUrl: " + url)
}
}
class BreadCrumbService {
Crumb makeCrumb(String objectType, String name, long objectId){
def ext = "/its/dcmd/" + objectType + "/show?id=" + objectId;
def type = objectType;
Crumb c = new Crumb(itemName:name,url:ext)
return c
}
控制器:
class PersonController{
def setBreadCrumbForCurrentItem = {
println("Action was called to set breadcrumb for current page")
def user = User.load(SecurityContextHolder.context.authentication.principal.id)
def username = user.username
def userId = user.id
println("User Id: " + user.id)
println("User Version: " + user.version)
def objectType = params.pageType
println("objectType: " + objectType)
def itsName = ""
def objectId = null;
if (objectType.equals('asset')){
Asset assetInstance = params.instance
itsName = assetInstance.itsId
objectId = assetInstance.id
println("asset name: " + itsName)
println("asset id: " + objectId)
}
else if (objectType.equals('host')){
Host hostInstance = Host.get(params.id)
itsName = hostInstance.hostname
objectId = hostInstance.id
println("host name:" + itsName)
println("host id: " + objectId)
}
else if (objectType.equals('application')){
Application appInstance = new Application(params)
itsName = appInstance.applicationTitle
objectId = appInstance.id
println("application name:" + itsName)
println("application id: " + objectId)
}
else if (objectType.equals('service')) {
Service service = new Service(params)
itsName = service.serviceTitle
objectId = service.id
println("service name:" + itsName)
println("service id: " + objectId)
}
Crumb c = breadCrumbService.makeCrumb(objectType,itsName,objectId);
c.save(failOnError: true, flush: true)
if (!c.save()) {
user.errors.each {
println it
}
}
c.printData()
println("current BreadCrumbs saved...")
for (int j = 0; j < user.BreadCrumbs.size(); j++){
println(user.BreadCrumbs.get(j).toString())
}
user.save(failOnError: true, flush: true)
println("User Version: " + user.version)
if (!user.save()) {
user.errors.each {
println it
}
}
}
视图:
<html>
<head>
<g:set var="assetType" value="${fieldValue(bean:assetInstance, field: 'assetType')}" />
%{--<g:set var="objectId" value="{assetId}" />--}%
<meta content="main" name="layout" />
<title><g:message code="default.dcmd.label" /></title>
<jqDT:resources jqueryUi="true" type="js" />
<r:require modules="ui,menu"/>
<script language="javascript" type="text/javascript" src="../js/mustache.js"></script>
<script type="text/javascript">
<g:include controller="person" action="setBreadCrumbForCurrentItem" params="[pageType: 'asset', instance :assetInstance]"/>
</script>
</head>
<body>
<div id="container">
<s:info/>
<g:render template="../show_secondary" model="[pageType:'asset', objectId:assetId, action:'show', assetType: assetType, assetName: assetInstance]" />
<g:render template="../breadcrumbs" model="[pageType:'asset', action:'show']"/>
任何反馈都非常感谢! 感谢
答案 0 :(得分:0)
从您的控制器......
Crumb c = breadCrumbService.makeCrumb(objectType,itsName,objectId);
c.save(failOnError: true, flush: true)
if (!c.save()) {
user.errors.each {
println it
}
}
c.printData()
除非我遗漏某些内容或您列出的代码不完整,否则在保存之前,您永远不会在User和Crumb之间创建关系!
另外,你应该
if(!c.save(failOnError:true, flush:true))
你拥有它的方式,你节省了两倍!