2013-08-21 17 views
1

你好,我一直在試圖實現自定義的Grails應用程序下鑽式痕跡導航,但似乎無法弄清楚,爲什麼克拉姆對象不保存在ArrayList中。Grails的麪包屑列表是不節能

創建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']"/> 

任何反饋得多在ADVAN理解CE! 感謝

回答

0

從你的控制器......

Crumb c = breadCrumbService.makeCrumb(objectType,itsName,objectId); 
c.save(failOnError: true, flush: true) 
if (!c.save()) { 
    user.errors.each { 
     println it 
    } 
} 
c.printData() 

除非我失去了一些東西或者您列出的代碼是不完整的,但是,你保存之前從未創建用戶和麪包屑之間的關係!

此外,你應該有

if(!c.save(failOnError:true, flush:true)) 

你的方式,你節省了兩次!