1

您好我一直在尝试为自定义 Grails 应用程序实现向下钻取类型的面包屑导航,但似乎无法弄清楚为什么 Crumb 对象未保存在 Arraylist 中。

面包屑被创建并添加到列表中,但是每当我导航到另一个页面时,列表就会被重置。

我知道有一个 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']"/>

任何反馈都非常感谢提前!谢谢

4

1 回答 1

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))

你拥有它的方式,你节省了两倍!

于 2013-08-22T01:48:15.360 回答